Skip to content

Commit

Permalink
Refactored FieldSet
Browse files Browse the repository at this point in the history
  • Loading branch information
jwaldrip committed Jan 7, 2013
1 parent 46b0725 commit 8305b1b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/memory_model/base/attributable.rb
Expand Up @@ -21,14 +21,14 @@ def has_attribute?(key)

def inspect
inspection = if @attributes
self.class.fields.reduce([]) { |array, name|
fields.reduce([]) { |array, name|
array << "#{name}: #{attribute_for_inspect(name)}" if has_attribute?(name)
array
}.join(", ")
else
"not initialized"
end
"#<#{self.class} #{inspection}>"
"#<#{[self.class, inspection].join(' ')}>"
end

def read_attribute(name)
Expand Down
4 changes: 4 additions & 0 deletions lib/memory_model/base/fieldable/field.rb
Expand Up @@ -31,4 +31,8 @@ def to_s
@name.to_s
end

def inspect
":#{@name}"
end

end
34 changes: 25 additions & 9 deletions lib/memory_model/base/fieldable/field_set.rb
@@ -1,34 +1,41 @@
require 'active_support/core_ext/hash/keys'
require 'set'

class MemoryModel::Base::Fieldable::FieldSet < Array
class MemoryModel::Base::Fieldable::FieldSet

Field = MemoryModel::Base::Fieldable::Field

attr_reader :fields
delegate :include?, to: :fields

def initialize
@fields = []
end

def [](name)
find { |f| f.name == name.to_sym }
@fields.find { |f| f.name == name.to_sym }
end

def <<(attr)
attr = Field.new(attr) unless attr.is_a? Field
super(attr)
@fields << attr
end

def add(attr, options={ })
delete_if { |f| f == attr }
self << Field.new(attr, options)
@fields.delete_if { |f| f == attr }
@fields << Field.new(attr, options)
end

def comparable
select(&:comparable?).map(&:to_sym)
@fields.select(&:comparable?).map(&:to_sym)
end

def inspect
to_a.inspect
end

def default_values(model, attributes={ })
reduce(attributes.symbolize_keys) do |attrs, field|
@fields.reduce(attributes.symbolize_keys) do |attrs, field|
raise MemoryModel::ReadonlyFieldError if attrs[field.name].present? && field.readonly?
default = field.default.is_a?(Symbol) ? field.default.to_proc : field.default
attrs[field.name] ||= if default.nil?
Expand All @@ -51,8 +58,17 @@ def default_values(model, attributes={ })
end

def to_a
map(&:name)
@fields.map(&:to_sym)
end

private

def method_missing(m, *args, &block)
if to_a.respond_to? m
to_a.send m, *args, &block
else
super
end
end
alias :names :to_a

end
7 changes: 7 additions & 0 deletions spec/memory_model/base/attributable_spec.rb
Expand Up @@ -61,6 +61,13 @@
instance.inspect.should match /#<#{instance.class}/
end

it 'should contain the attributes' do
instance.attributes.each do |name, value|
value = instance.send :attribute_for_inspect, name
instance.inspect.should include "#{name}: #{value}" if instance.has_attribute? name
end
end

it 'should read not initialized' do
model.allocate.inspect.should match /not initialized/
end
Expand Down
8 changes: 4 additions & 4 deletions spec/memory_model/base/fieldable/field_set_spec.rb
Expand Up @@ -7,7 +7,7 @@

describe '.new' do
it "should have an empty array of fields" do
field_set.should be_a Array
field_set.instance_variable_get(:@fields).should be_a Array
field_set.size.should == 0
end
end
Expand All @@ -22,18 +22,18 @@

describe '#<<' do
it 'should add a field with the symbol' do
expect { field_set << :foo }.to change { field_set }
expect { field_set << :foo }.to change { field_set.fields }
end
end

describe '#add' do
it "should add a field" do
expect { field_set.add(:foo) }.to change { field_set }
expect { field_set.add(:foo) }.to change { field_set.fields }
end

it "should add a field with options" do
options = { foo: :bar }
expect { field_set.add(:foo, options) }.to change { field_set }
expect { field_set.add(:foo, options) }.to change { field_set.fields }
field_set[:foo].options[:foo].should == :bar
end
end
Expand Down

0 comments on commit 8305b1b

Please sign in to comment.