Module: Hashie::Extensions::Dash::PropertyTranslation::ClassMethods

Defined in:
lib/hashie/extensions/dash/property_translation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#transformsObject (readonly)

Returns the value of attribute transforms.



49
50
51
# File 'lib/hashie/extensions/dash/property_translation.rb', line 49

def transforms
  @transforms
end

#translations_hashObject (readonly)

Returns the value of attribute translations_hash.



49
50
51
# File 'lib/hashie/extensions/dash/property_translation.rb', line 49

def translations_hash
  @translations_hash
end

Instance Method Details

#inherited(klass) ⇒ Object

Ensures that any inheriting classes maintain their translations.

  • :default - The class inheriting the translations.


54
55
56
57
58
# File 'lib/hashie/extensions/dash/property_translation.rb', line 54

def inherited(klass)
  super
  klass.instance_variable_set(:@transforms, transforms.dup)
  klass.instance_variable_set(:@translations_hash, translations_hash.dup)
end

#inverse_translationsObject



119
120
121
122
123
124
125
126
127
# File 'lib/hashie/extensions/dash/property_translation.rb', line 119

def inverse_translations
  @inverse_translations ||= {}.tap do |h|
    translations_hash.each do |(property_name, property_translations)|
      property_translations.keys.each do |k|
        h[k] = property_name
      end
    end
  end
end

#permitted_input_keysObject



60
61
62
# File 'lib/hashie/extensions/dash/property_translation.rb', line 60

def permitted_input_keys
  @permitted_input_keys ||= properties.map { |property| inverse_translations.fetch property, property }
end

#property(property_name, options = {}) ⇒ Object

Defines a property on the Trash. Options are as follows:

  • :default - Specify a default value for this property, to be returned before a value is set on the property in a new Dash.
  • :from - Specify the original key name that will be write only.
  • :with - Specify a lambda to be used to convert value.
  • :transform_with - Specify a lambda to be used to convert value without using the :from option. It transform the property itself.


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hashie/extensions/dash/property_translation.rb', line 72

def property(property_name, options = {})
  super

  if options[:from]
    if property_name == options[:from]
      fail ArgumentError, "Property name (#{property_name}) and :from option must not be the same"
    end

    translations_hash[options[:from]] ||= {}
    translations_hash[options[:from]][property_name] = options[:with] || options[:transform_with]

    define_method "#{options[:from]}=" do |val|
      self.class.translations_hash[options[:from]].each do |name, with|
        self[name] = with.respond_to?(:call) ? with.call(val) : val
      end
    end
  else
    if options[:transform_with].respond_to? :call
      transforms[property_name] = options[:transform_with]
    end
  end
end

#transformation_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/hashie/extensions/dash/property_translation.rb', line 99

def transformation_exists?(name)
  transforms.key? name
end

#transformed_property(property_name, value) ⇒ Object



95
96
97
# File 'lib/hashie/extensions/dash/property_translation.rb', line 95

def transformed_property(property_name, value)
  transforms[property_name].call(value)
end

#translation_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/hashie/extensions/dash/property_translation.rb', line 103

def translation_exists?(name)
  translations_hash.key? name
end

#translationsObject



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/hashie/extensions/dash/property_translation.rb', line 107

def translations
  @translations ||= {}.tap do |h|
    translations_hash.each do |(property_name, property_translations)|
      if property_translations.size > 1
        h[property_name] = property_translations.keys
      else
        h[property_name] = property_translations.keys.first
      end
    end
  end
end