Class: Nemo::MetaObject::AttributeCollector

Inherits:
Util::ObjectCollector show all
Defined in:
lib/nemo/metaobject/acollector.rb

Overview

Collect a list of Attribute objects using minimal syntax, Use the attribute methods (#text, #date, #boolean, etc) to add new attributes objects. All other messages will be sent to the most recently added attribute.

collector = Nemo::MetaObject::AttributeCollector
attributes = collector.new(metaobject).collect do
  text :name
    label 'Full Name'
    maxlength 30
  date :birthday
    label 'Birthday'
    read_only
 end
 attributes.inspect
 # => [<TextAttribute @label="Kevin", @maxlength=30>,
 #     <DateAttribute @label="Birthday", @read_only=true>]

Instance Attribute Summary collapse

Attributes inherited from Util::ObjectCollector

#index_error, #objects

Instance Method Summary collapse

Methods inherited from Util::ObjectCollector

collect, #collect, #method_missing, #o

Constructor Details

#initialize(metaobject) ⇒ AttributeCollector

Returns a new instance of AttributeCollector.



22
23
24
25
26
# File 'lib/nemo/metaobject/acollector.rb', line 22

def initialize(metaobject)
  super()
  @metaobject = metaobject
  @index_error = 'no attributes defined'
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Nemo::Util::ObjectCollector

Instance Attribute Details

#metaobjectObject

Returns the value of attribute metaobject.



20
21
22
# File 'lib/nemo/metaobject/acollector.rb', line 20

def metaobject
  @metaobject
end

Instance Method Details

#<<(attr) ⇒ Object

Collects to the metaobject



30
31
32
33
# File 'lib/nemo/metaobject/acollector.rb', line 30

def <<(attr)
  @metaobject << attr
  super
end

#[](type, symbol) ⇒ Object

Shortcut for creating attribute class instances

self[:text, symbol] => Nemo::MetaObject::TextAttribute.new(symbol)


39
40
41
42
# File 'lib/nemo/metaobject/acollector.rb', line 39

def [](type, symbol)
  attr = Nemo::Util.pascal_case(type)+'Attribute'
  Nemo::MetaObject.const_get(attr).new(symbol)
end

#boolean(symbol) ⇒ Object



56
57
58
# File 'lib/nemo/metaobject/acollector.rb', line 56

def boolean(symbol)
  self << self[:boolean, symbol]
end

#date(symbol) ⇒ Object



52
53
54
# File 'lib/nemo/metaobject/acollector.rb', line 52

def date(symbol)
  self << self[:date, symbol]
end

#multiple(symbol) ⇒ Object



60
61
62
# File 'lib/nemo/metaobject/acollector.rb', line 60

def multiple(symbol)
  self << self[:multiple, symbol]
end

#multiple_relationship(symbol) ⇒ Object



75
76
77
# File 'lib/nemo/metaobject/acollector.rb', line 75

def multiple_relationship(symbol)
  self << self[:multiple_relationship, symbol]
end

#password(symbol) ⇒ Object



48
49
50
# File 'lib/nemo/metaobject/acollector.rb', line 48

def password(symbol)
  self << self[:password, symbol]
end

#single(symbol) ⇒ Object

SingleAttribute is simply a MultipleAttribute with an item_limit of 1



66
67
68
69
# File 'lib/nemo/metaobject/acollector.rb', line 66

def single(symbol)
  multiple(symbol)
  item_limit 1
end

#single_relationship(symbol) ⇒ Object



71
72
73
# File 'lib/nemo/metaobject/acollector.rb', line 71

def single_relationship(symbol)
  self << self[:single_relationship, symbol]
end

#text(symbol) ⇒ Object



44
45
46
# File 'lib/nemo/metaobject/acollector.rb', line 44

def text(symbol)
  self << self[:text, symbol]
end