Class: Hashie::Dash
- Includes:
- PrettyInspect
- Defined in:
- lib/pancake/vendor/hashie/lib/hashie/dash.rb
Overview
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.
Direct Known Subclasses
Class Method Summary collapse
-
.defaults ⇒ Object
The default values that have been set for this Dash.
-
.properties ⇒ Object
Get a String array of the currently defined properties on this Dash.
-
.property(property_name, options = {}) ⇒ Object
Defines a property on the Dash.
-
.property?(prop) ⇒ Boolean
Check to see if the specified property has already been defined.
Instance Method Summary collapse
-
#[](property) ⇒ Object
Retrieve a value from the Dash (will return the property’s default value if it hasn’t been set).
-
#[]=(property, value) ⇒ Object
Set a value on the Dash in a Hash-like way.
-
#initialize(attributes = {}) ⇒ Dash
constructor
You may initialize a Dash with an attributes hash just like you would many other kinds of data objects.
Methods included from PrettyInspect
Methods inherited from Hash
Methods included from HashExtensions
#hashie_stringify_keys, #hashie_stringify_keys!, included, #to_mash
Constructor Details
#initialize(attributes = {}) ⇒ Dash
You may initialize a Dash with an attributes hash just like you would many other kinds of data objects.
76 77 78 79 80 81 82 83 84 |
# File 'lib/pancake/vendor/hashie/lib/hashie/dash.rb', line 76 def initialize(attributes = {}) self.class.properties.each do |prop| self.send("#{prop}=", self.class.defaults[prop.to_sym]) end attributes.each_pair do |att, value| self.send("#{att}=", value) end end |
Class Method Details
.defaults ⇒ Object
The default values that have been set for this Dash
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pancake/vendor/hashie/lib/hashie/dash.rb', line 63 def self.defaults properties = {} ancestors.each do |elder| if elder.instance_variable_defined?("@defaults") properties.merge! elder.instance_variable_get("@defaults") end end properties end |
.properties ⇒ Object
Get a String array of the currently defined properties on this Dash.
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pancake/vendor/hashie/lib/hashie/dash.rb', line 45 def self.properties properties = [] ancestors.each do |elder| if elder.instance_variable_defined?("@properties") properties << elder.instance_variable_get("@properties") end end properties.flatten.map{|p| p.to_s} end |
.property(property_name, options = {}) ⇒ Object
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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pancake/vendor/hashie/lib/hashie/dash.rb', line 26 def self.property(property_name, = {}) property_name = property_name.to_sym (@properties ||= []) << property_name (@defaults ||= {})[property_name] = .delete(:default) class_eval <<-RUBY def #{property_name} self[:#{property_name}] end def #{property_name}=(val) self[:#{property_name}] = val end RUBY end |
.property?(prop) ⇒ Boolean
Check to see if the specified property has already been defined.
58 59 60 |
# File 'lib/pancake/vendor/hashie/lib/hashie/dash.rb', line 58 def self.property?(prop) properties.include?(prop.to_s) end |
Instance Method Details
#[](property) ⇒ Object
Retrieve a value from the Dash (will return the property’s default value if it hasn’t been set).
88 89 90 |
# File 'lib/pancake/vendor/hashie/lib/hashie/dash.rb', line 88 def [](property) super(property.to_sym) if property_exists? property end |
#[]=(property, value) ⇒ Object
Set a value on the Dash in a Hash-like way. Only works on pre-existing properties.
94 95 96 |
# File 'lib/pancake/vendor/hashie/lib/hashie/dash.rb', line 94 def []=(property, value) super if property_exists? property end |