Class: Hashie::Dash
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.
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_name) ⇒ 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.
Methods included from HashExtensions
#hashie_stringify_keys, #hashie_stringify_keys!, included, #to_mash
Class Method Details
.defaults ⇒ Object
The default values that have been set for this Dash
52 53 54 |
# File 'lib/hashie/dash.rb', line 52 def self.defaults @defaults end |
.properties ⇒ Object
Get a String array of the currently defined properties on this Dash.
41 42 43 |
# File 'lib/hashie/dash.rb', line 41 def self.properties @properties.collect{|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.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hashie/dash.rb', line 22 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.
47 48 49 |
# File 'lib/hashie/dash.rb', line 47 def self.property?(prop) properties.include?(prop.to_s) end |
Instance Method Details
#[](property_name) ⇒ Object
Retrieve a value from the Dash (will return the property’s default value if it hasn’t been set).
58 59 60 |
# File 'lib/hashie/dash.rb', line 58 def [](property_name) super(property_name.to_sym) || self.class.defaults[property_name.to_sym] end |
#[]=(property, value) ⇒ Object
Set a value on the Dash in a Hash-like way. Only works on pre-existing properties.
64 65 66 67 68 69 70 |
# File 'lib/hashie/dash.rb', line 64 def []=(property, value) if self.class.property?(property) super else raise NoMethodError, 'You may only set pre-defined properties.' end end |