diff options
| author | Teddy Wing | 2017-09-19 17:58:08 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2017-09-19 17:58:08 +0200 | 
| commit | c9a997f337c9b45f62b50629de96fa95f20c1a7f (patch) | |
| tree | ce87b8ce2cf3bde2be68a36461059a4a37ba2580 /lib/model_attribute.rb | |
| parent | e1547b52a372942b34cda3b3fee4ab76e9f1b65e (diff) | |
| download | chouette-core-c9a997f337c9b45f62b50629de96fa95f20c1a7f.tar.bz2 | |
ModelAttribute: Make `@@all` a class instance variable
Robert argues that the class variable is not ideal because its value
gets shared with subclasses, violating the Liskov substitution
principle:
    17:44:23 < robert> ```133] pry(main)> class A; @@greeting = :hello end
    17:44:23 < robert> => :hello
    17:44:23 < robert> [134] pry(main)> class B < A; end
    17:44:23 < robert> => nil
    17:44:23 < robert> [135] pry(main)> B.class_variable_get '@@greeting'
    17:44:23 < robert> => :hello
Instead, go with his suggestion to use a class instance variable
instead, which gives us the same amount of availability of the `all`
list while not sharing it indiscriminately and making it easier to test.
Refs #4401
Diffstat (limited to 'lib/model_attribute.rb')
| -rw-r--r-- | lib/model_attribute.rb | 10 | 
1 files changed, 5 insertions, 5 deletions
| diff --git a/lib/model_attribute.rb b/lib/model_attribute.rb index 22c366b37..98bb54b67 100644 --- a/lib/model_attribute.rb +++ b/lib/model_attribute.rb @@ -1,12 +1,12 @@  class ModelAttribute -  cattr_reader :all - -  @@all = [] -    attr_reader :klass, :name, :data_type +  def self.all +    @__all__ ||= [] +  end +    def self.define(klass, name, data_type) -    @@all << new(klass, name, data_type) +    all << new(klass, name, data_type)    end    def initialize(klass, name, data_type) | 
