Module: Opto::Model::AttributeDeclaration::ClassMethods

Defined in:
lib/opto/model/attribute_declaration.rb

Instance Method Summary collapse

Instance Method Details

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/opto/model/attribute_declaration.rb', line 14

def attribute(name, type, arguments = {})
  if attribute_definitions.find { |d| d[:name] == name }
    raise RuntimeError, "Duplicate attribute '#{name}'"
  end
  if type.to_sym == :boolean
    arguments.merge!(as: "boolean")
  end

  attribute_definitions << arguments.merge(name: name, type: type)

  define_method "#{name}_handler" do
    collection[name].handler
  end

  if type.to_sym == :boolean
    define_method "#{name}?" do
      collection[name].value
    end
  end

  define_method name do
    collection[name].value
  end

  define_method "#{name}=" do |value|
    collection[name].set(value)
  end
end

#attribute_definitionsObject



10
11
12
# File 'lib/opto/model/attribute_declaration.rb', line 10

def attribute_definitions
  @attribute_definitions ||= []
end