module Hashie::Extensions::Dash::IndifferentAccess::ClassMethods

Class-level property and translation lookup with indifferent keys.

@example

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::IndifferentAccess
  property :name
end
klass.property?('name') # => true

Public Instance Methods

property?(name) click to toggle source

Check to see if the specified property has already been defined.

@example

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::IndifferentAccess
  property :name
end
klass.property?('name') # => true
klass.property?(:missing) # => false
# File lib/hashie/extensions/dash/indifferent_access.rb, line 84
def property?(name)
  targets = if translation_for?(name)
              Array(translations.find { |key, _| key.to_s == name.to_s }.last)
            else
              [name]
            end
  targets.all? do |target|
    properties.any? { |property| property.to_s == target.to_s }
  end
end
transformation_exists?(name) click to toggle source

@example Find a transformer regardless of key type

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  include Hashie::Extensions::Dash::IndifferentAccess
  property :age, transform_with: ->(value) { value.to_i }
end
klass.transformation_exists?('age') # => true
klass.transformation_exists?(:missing) # => false
# File lib/hashie/extensions/dash/indifferent_access.rb, line 128
def transformation_exists?(name)
  name = name.to_s
  !!transforms.keys.find { |key| key.to_s == name }
end
transformed_property(property_name, value) click to toggle source

@example Apply a transformer declared with a symbol key

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  include Hashie::Extensions::Dash::IndifferentAccess
  property :age, transform_with: ->(value) { value.to_i }
end
klass.transformed_property('age', '37') # => 37
# File lib/hashie/extensions/dash/indifferent_access.rb, line 115
def transformed_property(property_name, value)
  transform = transforms[property_name] || transforms[property_name.to_sym]
  transform.call(value)
end
translation_exists?(name) click to toggle source

@example Look up a translation with a string or symbol

klass = Class.new(Hashie::Dash) do
  include Hashie::Extensions::Dash::PropertyTranslation
  include Hashie::Extensions::Dash::IndifferentAccess
  property :name, from: :fullName
end
klass.translation_exists?('fullName') # => true
klass.translation_exists?(:missing) # => false
# File lib/hashie/extensions/dash/indifferent_access.rb, line 103
def translation_exists?(name)
  name = name.to_s
  !!translations.keys.find { |key| key.to_s == name }
end

Private Instance Methods

translation_for?(name) click to toggle source
# File lib/hashie/extensions/dash/indifferent_access.rb, line 135
def translation_for?(name)
  included_modules.include?(Hashie::Extensions::Dash::PropertyTranslation) &&
    translation_exists?(name)
end