Module: Transformers::ClassAttribute

Instance Method Summary collapse

Instance Method Details

#class_attribute(name, default = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/transformers/ruby_utils.rb', line 3

def class_attribute(name, default = nil)
  singleton_class.attr_writer name
  var = "@#{name}"
  instance_variable_set(var, default)
  singleton_class.define_method(name) do
    # ancestors includes current module
    ancestors.find { |c| c.instance_variable_defined?(var) }.instance_variable_get(var)
  end
  define_method(name) do
    self.class.send(name)
  end
end