Module: Mingo::Properties::ClassMethods

Defined in:
lib/mingo/properties.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



12
13
14
# File 'lib/mingo/properties.rb', line 12

def properties
  @properties
end

Instance Method Details

#inherited(klass) ⇒ Object



37
38
39
40
41
# File 'lib/mingo/properties.rb', line 37

def inherited(klass)
  super
  (@subclasses ||= Array.new) << klass
  klass.instance_variable_set('@properties', self.properties.dup)
end

#property(name, options = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mingo/properties.rb', line 14

def property(name, options = nil)
  self.properties << name.to_sym

  setter_name = "#{name}="
  unless method_defined?(setter_name)
    methods = Module.new
    methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
      def #{name}(&block)
        self.[](#{name.to_s.inspect}, &block)
      end

      def #{setter_name}(value)
        self.[]=(#{name.to_s.inspect}, value)
      end
    RUBY
    include methods
  end

  if defined? @subclasses
    @subclasses.each { |klass| klass.property(property_name, options) }
  end
end