summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordB <dblock@dblock.org>2014-08-24 10:35:11 -0400
committerdB <dblock@dblock.org>2014-08-24 10:35:11 -0400
commit57a0ffb6786057af0129ecc0a0ea06ad4be9c9e6 (patch)
treeef8b27c22c2d01aef97676642dc11d53a4bce1d7
parent70f1f897d7381572209fc955842909669c249c15 (diff)
downloadhashie-57a0ffb6786057af0129ecc0a0ea06ad4be9c9e6.tar.gz
Preserve backward-compatible translations when a single property is translated.
-rw-r--r--lib/hashie/trash.rb28
-rw-r--r--spec/hashie/trash_spec.rb6
2 files changed, 24 insertions, 10 deletions
diff --git a/lib/hashie/trash.rb b/lib/hashie/trash.rb
index 7aff6f8..dbf4dae 100644
--- a/lib/hashie/trash.rb
+++ b/lib/hashie/trash.rb
@@ -26,11 +26,11 @@ module Hashie
fail ArgumentError, "Property name (#{property_name}) and :from option must not be the same"
end
- translations[options[:from]] ||= {}
- translations[options[:from]][property_name] = options[:with] || options[:transform_with]
+ translations_hash[options[:from]] ||= {}
+ translations_hash[options[:from]][property_name] = options[:with] || options[:transform_with]
define_method "#{options[:from]}=" do |val|
- self.class.translations[options[:from]].each do |name, with|
+ self.class.translations_hash[options[:from]].each do |name, with|
self[name] = with.respond_to?(:call) ? with.call(val) : val
end
end
@@ -42,15 +42,15 @@ module Hashie
end
class << self
- attr_reader :transforms, :translations
+ attr_reader :transforms, :translations_hash
end
instance_variable_set('@transforms', {})
- instance_variable_set('@translations', {})
+ instance_variable_set('@translations_hash', {})
def self.inherited(klass)
super
klass.instance_variable_set('@transforms', transforms.dup)
- klass.instance_variable_set('@translations', translations.dup)
+ klass.instance_variable_set('@translations_hash', translations_hash.dup)
end
# Set a value on the Dash in a Hash-like way. Only works
@@ -70,7 +70,7 @@ module Hashie
end
def self.translation_exists?(name)
- translations.key? name
+ translations_hash.key? name
end
def self.transformation_exists?(name)
@@ -83,10 +83,20 @@ module Hashie
private
+ def self.translations
+ @translations ||= begin
+ h = {}
+ translations_hash.each do |(property_name, property_translations)|
+ h[property_name] = property_translations.size > 1 ? property_translations.keys : property_translations.keys.first
+ end
+ h
+ end
+ end
+
def self.inverse_translations
@inverse_translations ||= begin
h = {}
- translations.each do |(property_name, property_translations)|
+ translations_hash.each do |(property_name, property_translations)|
property_translations.keys.each do |k|
h[k] = property_name
end
@@ -108,7 +118,7 @@ module Hashie
def initialize_attributes(attributes)
return unless attributes
attributes_copy = attributes.dup.delete_if do |k, v|
- if self.class.translations.include?(k)
+ if self.class.translations_hash.include?(k)
self[k] = v
true
end
diff --git a/spec/hashie/trash_spec.rb b/spec/hashie/trash_spec.rb
index fbc421f..7bb174f 100644
--- a/spec/hashie/trash_spec.rb
+++ b/spec/hashie/trash_spec.rb
@@ -29,7 +29,7 @@ describe Hashie::Trash do
end
it 'maintains translations hash mapping from the original to the translated name' do
- expect(TrashTest.translations[:firstName]).to eq(first_name: nil)
+ expect(TrashTest.translations[:firstName]).to eq(:first_name)
end
it 'maintains inverse translations hash mapping from the translated to the original name' do
@@ -151,6 +151,10 @@ describe Hashie::Trash do
it 'translates the second key' do
expect(subject.value_b).to eq 'value in b'
end
+
+ it 'maintains translations hash mapping from the original to the translated name' do
+ expect(SomeDataModel.translations).to eq(config: [:value_a, :value_b])
+ end
end
describe 'uses with or transform_with interchangeably' do