Class: MR::FakeRecord::AttributeSet
- Inherits:
-
Object
- Object
- MR::FakeRecord::AttributeSet
- Includes:
- Enumerable
- Defined in:
- lib/mr/fake_record/attributes.rb
Instance Method Summary collapse
- #add(record_class, name, type, options = nil) ⇒ Object
- #batch_write(new_attributes, record) ⇒ Object
- #each(&block) ⇒ Object
- #find(name) ⇒ Object
-
#initialize ⇒ AttributeSet
constructor
A new instance of AttributeSet.
- #read_all(record) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ AttributeSet
Returns a new instance of AttributeSet.
64 65 66 |
# File 'lib/mr/fake_record/attributes.rb', line 64 def initialize @attributes = {} end |
Instance Method Details
#add(record_class, name, type, options = nil) ⇒ Object
90 91 92 93 94 |
# File 'lib/mr/fake_record/attributes.rb', line 90 def add(record_class, name, type, = nil) attribute = Attribute.new(name, type, ) attribute.define_on(record_class) @attributes[name.to_s] = attribute end |
#batch_write(new_attributes, record) ⇒ Object
82 83 84 |
# File 'lib/mr/fake_record/attributes.rb', line 82 def batch_write(new_attributes, record) new_attributes.each{ |name, value| find(name).write(value, record) } end |
#each(&block) ⇒ Object
72 73 74 |
# File 'lib/mr/fake_record/attributes.rb', line 72 def each(&block) @attributes.values.each(&block) end |
#find(name) ⇒ Object
68 69 70 |
# File 'lib/mr/fake_record/attributes.rb', line 68 def find(name) @attributes[name.to_s] || raise(NoAttributeError.new(name)) end |
#read_all(record) ⇒ Object
76 77 78 79 80 |
# File 'lib/mr/fake_record/attributes.rb', line 76 def read_all(record) @attributes.values.inject({}) do |h, attribute| h.merge(attribute.name => attribute.read(record)) end end |
#to_a ⇒ Object
86 87 88 |
# File 'lib/mr/fake_record/attributes.rb', line 86 def to_a @attributes.values.sort end |