Class: MR::FakeRecord::AttributeSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mr/fake_record/attributes.rb

Instance Method Summary collapse

Constructor Details

#initializeAttributeSet

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, options = nil)
  attribute = Attribute.new(name, type, options)
  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_aObject



86
87
88
# File 'lib/mr/fake_record/attributes.rb', line 86

def to_a
  @attributes.values.sort
end