Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert hashes to indifferent access #215

Merged
merged 2 commits into from
Aug 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions lib/active_interaction/backports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,3 @@ class Errors
protected :initialize_dup
end
end

# @private
class Hash
# Required for Rails < 4.0.0.
def transform_keys
result = {}
each_key do |key|
result[yield(key)] = self[key]
end
result
end unless method_defined?(:transform_keys)
end
11 changes: 4 additions & 7 deletions lib/active_interaction/filters/hash_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class HashFilter < Filter
def cast(value)
case value
when Hash
value = stringify_the_symbol_keys(value)
value = value.with_indifferent_access
initial = strip? ? ActiveSupport::HashWithIndifferentAccess.new : value

filters.each_with_object(strip? ? {} : value) do |(name, filter), h|
filters.each_with_object(initial) do |(name, filter), h|
clean_value(h, name.to_s, filter, value)
end.symbolize_keys
end
else
super
end
Expand Down Expand Up @@ -74,9 +75,5 @@ def raw_default
def strip?
options.fetch(:strip, true)
end

def stringify_the_symbol_keys(hash)
hash.transform_keys { |key| key.is_a?(Symbol) ? key.to_s : key }
end
end
end
2 changes: 1 addition & 1 deletion spec/active_interaction/filters/hash_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
let(:block) { proc { hash :a } }

context 'with a Hash' do
let(:value) { { a: {} } }
let(:value) { { 'a' => {} } }

it 'returns the Hash' do
expect(result).to eql value
Expand Down
4 changes: 2 additions & 2 deletions spec/active_interaction/integration/hash_interaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
before { inputs.merge!(a: a) }

it 'returns the correct value for :a' do
expect(result[:a]).to eql a.symbolize_keys
expect(result[:a]).to eql a.with_indifferent_access
end

it 'returns the correct value for :b' do
expect(result[:b]).to eql(x: {})
expect(result[:b]).to eql('x' => {})
end
end

Expand Down