Parent

Included Modules

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.

Public Class Methods

defaults() click to toggle source

The default values that have been set for this Dash

    # File lib/hashie/dash.rb, line 63
63:     def self.defaults
64:       properties = {}
65:       ancestors.each do |elder|
66:         if elder.instance_variable_defined?("@defaults")
67:           properties.merge! elder.instance_variable_get("@defaults")
68:         end
69:       end
70:       
71:       properties
72:     end
new(attributes = {}) 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 76
76:     def initialize(attributes = {})
77:       self.class.properties.each do |prop|
78:         self.send("#{prop}=", self.class.defaults[prop.to_sym])
79:       end
80: 
81:       attributes.each_pair do |att, value|
82:         self.send("#{att}=", value)
83:       end
84:     end
properties() click to toggle source

Get a String array of the currently defined properties on this Dash.

    # File lib/hashie/dash.rb, line 45
45:     def self.properties
46:       properties = []
47:       ancestors.each do |elder| 
48:         if elder.instance_variable_defined?("@properties")
49:           properties << elder.instance_variable_get("@properties")
50:         end
51:       end
52: 
53:       properties.flatten.map{|p| p.to_s}
54:     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.

    # File lib/hashie/dash.rb, line 26
26:     def self.property(property_name, options = {})
27:       property_name = property_name.to_sym
28: 
29:       (@properties ||= []) << property_name
30:       (@defaults ||= {})[property_name] = options.delete(:default)
31: 
32:       class_eval         def #{property_name}          self[:#{property_name}]        end        def #{property_name}=(val)          self[:#{property_name}] = val        end
33:     end
property?(prop) click to toggle source

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

    # File lib/hashie/dash.rb, line 58
58:     def self.property?(prop)
59:       properties.include?(prop.to_s)
60:     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 88
88:     def [](property)
89:       super(property.to_sym) if property_exists? property
90:     end
[]=(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 94
94:     def []=(property, value)
95:       super if property_exists? property
96:     end

Private Instance Methods

property_exists?(property) click to toggle source

Raises an NoMethodError if the property doesn’t exist

     # File lib/hashie/dash.rb, line 101
101:       def property_exists?(property)
102:         unless self.class.property?(property.to_sym)
103:           raise NoMethodError, "The property '#{property}' is not defined for this Dash."
104:         end
105:         true
106:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.