Module: RFunk::AttributeDefinition

Defined in:
lib/rfunk/attribute/attribute_definition.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rfunk/attribute/attribute_definition.rb', line 3

def attribute(name, type, options = {})
  AttributeVariable.new.add(instance: self,
                            name: name,
                            type: type,
                            options: options)

  define_method(name) { |value = nil|
    if value
      ErrorChecking.new.raise_expected_type(name, value, type)
      Immutable.new.create(instance: self,
                           variable_name: variable_name(name),
                           value: value)
    else
      Option(self.instance_variable_get(variable_name(name)))
    end
  }
end