Skip to content

Commit

Permalink
Fix DynamicStruct so that it allows a sort param.
Browse files Browse the repository at this point in the history
`sort` params were broken due to the fact that we
are using a Hashie::Mash as our DynamicStruct. Hash
has a sort method, so it meant that a `sort` entry
in the hash was not used for `mash.sort`.

I don't remember the full history of why I switched to
a Hashie::Mash in 8f6d2f4 but this is one downside:
more collisions with method names. We may want to
reconsider that decision at some point, but this solves
the problem for now.
  • Loading branch information
myronmarston committed Dec 11, 2012
1 parent 7c616b0 commit a3af009
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/interpol/dynamic_struct.rb
Expand Up @@ -14,8 +14,12 @@ module DynamicStruct
raise NoMethodError, "undefined method `#{key}' for #{hash.inspect}"
end

Mash = Class.new(::Hashie::Mash) do
undef sort
end

def self.new(source)
hash = Hashie::Mash.new(source)
hash = Mash.new(source)
recursively_freeze(hash)
hash
end
Expand Down
12 changes: 12 additions & 0 deletions spec/unit/interpol/dynamic_struct_spec.rb
Expand Up @@ -33,6 +33,18 @@ module Interpol
ds.a.should eq([1, 2, 3])
ds.b.map(&:c).should eq([5, 4])
end

hash_methods_allowed_as_params = [:sort]

hash_methods_allowed_as_params.each do |meth|
it 'allows params that correspond to hash method names' do
{}.should respond_to(meth)
ds = DynamicStruct.new(meth.to_s => "v1", "inner" => {
meth.to_s => "v2" })
ds.send(meth).should eq("v1")
ds.inner.send(meth).should eq("v2")
end
end
end
end

0 comments on commit a3af009

Please sign in to comment.