Class: Hashie::Trash

### Home Classes Methods

### In Files

- lib/hashie/trash.rb

### Parent

Hashie::Dash

### Methods

- ::inherited
- ::permitted_input_keys
- ::property
- ::transformation_exists?
- ::transformed_property
- ::translation_exists?
- #[]=

### Class/Module Index

Quicksearch

- Hashie
- Hashie::Clash
- Hashie::Clash::ChainError
- Hashie::CoercionError
- Hashie::Dash
- Hashie::Extensions
- Hashie::Extensions::Coercion
- Hashie::Extensions::Coercion::ClassMethods
- Hashie::Extensions::Coercion::InstanceMethods
- Hashie::Extensions::Dash
- Hashie::Extensions::Dash::IndifferentAccess
- Hashie::Extensions::Dash::IndifferentAccess::ClassMethods
- Hashie::Extensions::DeepFetch
- Hashie::Extensions::DeepFetch::UndefinedPathError
- Hashie::Extensions::DeepFind
- Hashie::Extensions::DeepMerge
- Hashie::Extensions::IgnoreUndeclared
- Hashie::Extensions::IndifferentAccess
- Hashie::Extensions::KeyConversion
- Hashie::Extensions::Mash
- Hashie::Extensions::Mash::SafeAssignment
- Hashie::Extensions::MergeInitializer
- Hashie::Extensions::MethodAccess
- Hashie::Extensions::MethodAccessWithOverride
- Hashie::Extensions::MethodOverridingWriter
- Hashie::Extensions::MethodQuery
- Hashie::Extensions::MethodReader
- Hashie::Extensions::MethodWriter
- Hashie::Extensions::Parsers
- Hashie::Extensions::Parsers::YamlErbParser
- Hashie::Extensions::PrettyInspect
- Hashie::Extensions::StringifyKeys
- Hashie::Extensions::StringifyKeys::ClassMethods
- Hashie::Extensions::SymbolizeKeys
- Hashie::Extensions::SymbolizeKeys::ClassMethods
- Hashie::Hash
- Hashie::Mash
- Hashie::Rash
- Hashie::Trash

No matching classes.

# Hashie::Trash

A Trash is a 'translated' Dash where the keys can be remapped from a source hash.

Trashes are useful when you need to read data from another application, such as a Java api, where the keys are named differently from how we would in Ruby.

### Attributes

transforms[R]
translations_hash[R]

### Public Class Methods

inherited(klass) click to toggle source

    # File lib/hashie/trash.rb, line 50
    def self.inherited(klass)
      super
      klass.instance_variable_set('@transforms', transforms.dup)
      klass.instance_variable_set('@translations_hash', translations_hash.dup)
    end

permitted_input_keys() click to toggle source

    # File lib/hashie/trash.rb, line 80
    def self.permitted_input_keys
      @permitted_input_keys ||= properties.map { |property| inverse_translations.fetch property, property }
    end

property(property_name, options = {}) click to toggle source

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.

    # File lib/hashie/trash.rb, line 19
    def self.property(property_name, options = {})
      super

      options[:from] = options[:from] if options[:from]

      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) click to toggle source

    # File lib/hashie/trash.rb, line 76
    def self.transformation_exists?(name)
      transforms.key? name
    end

transformed_property(property_name, value) click to toggle source

    # File lib/hashie/trash.rb, line 68
    def self.transformed_property(property_name, value)
      transforms[property_name].call(value)
    end

translation_exists?(name) click to toggle source

    # File lib/hashie/trash.rb, line 72
    def self.translation_exists?(name)
      translations_hash.key? name
    end

### Public Instance Methods

[]=(property, value) click to toggle source

Set a value on the Dash in a Hash-like way. Only works on pre-existing properties.

    # File lib/hashie/trash.rb, line 58
    def []=(property, value)
      if self.class.translation_exists? property
        send("#{property}=", value)
      elsif self.class.transformation_exists? property
        super property, self.class.transformed_property(property, value)
      elsif property_exists? property
        super
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.