Class: Hashie::Dash

### Home Classes Methods

### In Files

- lib/hashie/dash.rb

### Parent

Hash

### Methods

- ::inherited
- ::new
- ::property
- ::property?
- ::required?
- #[]
- #[]=
- #_regular_reader
- #_regular_writer
- #merge
- #merge!
- #replace

### Included Modules

- Hashie::PrettyInspect

### Class/Module Index

Quicksearch

- Hashie
- Hashie::Clash
- Hashie::Clash::ChainError
- Hashie::Dash
- Hashie::Extensions
- Hashie::Extensions::Coercion
- Hashie::Extensions::Coercion::ClassMethods
- Hashie::Extensions::Coercion::InstanceMethods
- Hashie::Extensions::DeepFetch
- Hashie::Extensions::DeepFetch::UndefinedPathError
- Hashie::Extensions::DeepMerge
- Hashie::Extensions::IgnoreUndeclared
- Hashie::Extensions::IndifferentAccess
- Hashie::Extensions::KeyConversion
- Hashie::Extensions::MergeInitializer
- Hashie::Extensions::MethodAccess
- Hashie::Extensions::MethodQuery
- Hashie::Extensions::MethodReader
- Hashie::Extensions::MethodWriter
- Hashie::Extensions::StringifyKeys
- Hashie::Extensions::SymbolizeKeys
- Hashie::Hash
- Hashie::HashExtensions
- Hashie::Mash
- Hashie::PrettyInspect
- Hashie::Rash
- Hashie::Trash

No matching classes.

# Hashie::Dash

A Dash is a 'defined' or 'discrete' Hash, that is, a Hash that has a set of defined keys that are accessible (with optional defaults) and only those keys may be set or read.

Dashes are useful when you need to create a very simple lightweight data object that needs even fewer options and resources than something like a DataMapper resource.

It is preferrable to a Struct because of the in-class API for defining properties as well as per-property defaults.

### Attributes

defaults[R]
properties[R]
required_properties[R]

### Public Class Methods

inherited(klass) click to toggle source

    # File lib/hashie/dash.rb, line 61
    def self.inherited(klass)
      super
      (@subclasses ||= Set.new) << klass
      klass.instance_variable_set('@properties', properties.dup)
      klass.instance_variable_set('@defaults', defaults.dup)
      klass.instance_variable_set('@required_properties', required_properties.dup)
    end

new(attributes = {}, &block) click to toggle source

You may initialize a Dash with an attributes hash just like you would many other kinds of data objects.

    # File lib/hashie/dash.rb, line 83
    def initialize(attributes = {}, &block)
      super(&block)

      self.class.defaults.each_pair do |prop, value|
        self[prop] = begin
          value.dup
        rescue TypeError
          value
        end
      end

      initialize_attributes(attributes)
      assert_required_properties_set!
    end

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

Defines a property on the Dash. 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.

- `:required` - Specify the value as required for this property, to raise an error if a value is unset in a new or existing Dash.

    # File lib/hashie/dash.rb, line 30
    def self.property(property_name, options = {})
      property_name = property_name.to_sym

      properties << property_name

      if options.key?(:default)
        defaults[property_name] = options[:default]
      elsif defaults.key?(property_name)
        defaults.delete property_name
      end

      unless instance_methods.map { |m| m.to_s }.include?("#{property_name}=")
        define_method(property_name) { |&block| self.[](property_name.to_s, &block) }
        property_assignment = property_name.to_s.concat('=').to_sym
        define_method(property_assignment) { |value| self.[]=(property_name.to_s, value) }
      end

      if defined? @subclasses
        @subclasses.each { |klass| klass.property(property_name, options) }
      end
      required_properties << property_name if options.delete(:required)
    end

property?(name) click to toggle source

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

    # File lib/hashie/dash.rb, line 71
    def self.property?(name)
      properties.include? name.to_sym
    end

required?(name) click to toggle source

Check to see if the specified property is required.

    # File lib/hashie/dash.rb, line 77
    def self.required?(name)
      required_properties.include? name.to_sym
    end

### Public Instance Methods

[](property) click to toggle source

Retrieve a value from the Dash (will return the property's default value if it hasn't been set).

    # File lib/hashie/dash.rb, line 104
    def [](property)
      assert_property_exists! property
      value = super(property.to_s)
      # If the value is a lambda, proc, or whatever answers to call, eval the thing!
      if value.is_a? Proc
        self[property] = value.call # Set the result of the call as a value
      else
        yield value if block_given?
        value
      end
    end

Also aliased as: _regular_reader
[]=(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/dash.rb, line 118
    def []=(property, value)
      assert_property_required! property, value
      assert_property_exists! property
      super(property.to_s, value)
    end

Also aliased as: _regular_writer
_regular_reader(property) click to toggle source
Alias for: []
_regular_writer(property, value) click to toggle source
Alias for: []=
merge(other_hash) click to toggle source

    # File lib/hashie/dash.rb, line 124
    def merge(other_hash)
      new_dash = dup
      other_hash.each do |k, v|
        new_dash[k] = block_given? ? yield(k, self[k], v) : v
      end
      new_dash
    end

merge!(other_hash) click to toggle source

    # File lib/hashie/dash.rb, line 132
    def merge!(other_hash)
      other_hash.each do |k, v|
        self[k] = block_given? ? yield(k, self[k], v) : v
      end
      self
    end

replace(other_hash) click to toggle source

    # File lib/hashie/dash.rb, line 139
    def replace(other_hash)
      other_hash = self.class.defaults.merge(other_hash)
      (keys - other_hash.keys).each { |key| delete(key) }
      other_hash.each { |key, value| self[key] = value }
      self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.