class 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.

Public Class Methods

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.

Calls superclass method Hashie::Dash.property
# File lib/hashie/trash.rb, line 17
def self.property(property_name, options = {})
  super

  if options[:from]
    translations << options[:from].to_sym
    class_eval <<-RUBY
      def #{options[:from]}=(val)
        self[:#{property_name}] = val
      end
    RUBY
  end
end

Private Class Methods

translations() click to toggle source
# File lib/hashie/trash.rb, line 42
def self.translations
  @translations ||= []
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.

Calls superclass method Hashie::Dash#[]=
# File lib/hashie/trash.rb, line 32
def []=(property, value)
  if self.class.translations.include? property.to_sym
    send("#{property}=", value)
  elsif property_exists? property
    super
  end
end

Private Instance Methods

property_exists?(property) click to toggle source

Raises an NoMethodError if the property doesn't exist

# File lib/hashie/trash.rb, line 48
def property_exists?(property)
  unless self.class.property?(property.to_sym)
    raise NoMethodError, "The property '#{property}' is not defined for this Trash."
  end
  true
end