Permalink
Browse files

should support value aggregates as well

  • Loading branch information...
1 parent 89b36e5 commit f37dd5f0c5ac6c709095a7c40f4c9c2de2ef9a72 @davidhq davidhq committed Jul 26, 2009
Showing with 15 additions and 1 deletion.
  1. +8 −1 lib/asbestos.rb
  2. +7 −0 spec/asbestos_spec.rb
View
@@ -80,6 +80,7 @@ def method_missing(method, *args)
end
else
raise ArgumentError, "don't know what to do with attributes" if attrs
+ method = method.pluralize if _aggregates.include?(method)
_write_pair(method, value)
end
end
@@ -96,7 +97,13 @@ def _aggregates
end
def _write_pair(key, value)
- @target[key.to_s.gsub('-', '_')] = value
+ key = key.to_s.gsub('-', '_')
+ if @target[key].nil?
+ @target[key] = value
+ else
+ @target[key] = [@target[key]] unless @target[key].is_a?(Array)
+ @target[key] << value
+ end
end
def _new_hash
@@ -77,4 +77,11 @@ def to_json
end
to_json.should == '{"foos": [{"bar": "baz"}, {"bar": "qux"}]}'
end
+
+ it "should support value aggregates" do
+ @json = described_class.new(:aggregate => ['foo'])
+ @json.tag!(:foo, 'bar')
+ @json.tag!(:foo, 'bar')
+ to_json.should == '{"foos": ["bar", "bar"]}'
+ end
end

0 comments on commit f37dd5f

Please sign in to comment.