Class: Hashie::Dash
- Includes:
- PrettyInspect
- Defined in:
- 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 Attribute Summary collapse
-
.defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
.properties ⇒ Object
readonly
Returns the value of attribute properties.
Class Method Summary collapse
- .inherited(klass) ⇒ Object
-
.property(property_name, options = {}) ⇒ Object
Defines a property on the Dash.
-
.property?(name) ⇒ 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 = {}, &block) ⇒ 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 = {}, &block) ⇒ Dash
You may initialize a Dash with an attributes hash just like you would many other kinds of data objects.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/hashie/dash.rb', line 73 def initialize(attributes = {}, &block) super(&block) self.class.defaults.each_pair do |prop, value| self.send("#{prop}=", value) end attributes.each_pair do |att, value| self.send("#{att}=", value) end if attributes end |
Class Attribute Details
.defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
53 54 55 |
# File 'lib/hashie/dash.rb', line 53 def defaults @defaults end |
.properties ⇒ Object (readonly)
Returns the value of attribute properties.
53 54 55 |
# File 'lib/hashie/dash.rb', line 53 def properties @properties end |
Class Method Details
.inherited(klass) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/hashie/dash.rb', line 58 def self.inherited(klass) super (@subclasses ||= Set.new) << klass klass.instance_variable_set('@properties', self.properties.dup) klass.instance_variable_set('@defaults', self.defaults.dup) 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 42 43 44 45 46 47 48 49 50 |
# File 'lib/hashie/dash.rb', line 26 def self.property(property_name, = {}) property_name = property_name.to_sym self.properties << property_name if [:default] or self.defaults[property_name] self.defaults[property_name] = [:default] end unless instance_methods.map { |m| m.to_s }.include?("#{property_name}=") class_eval <<-ACCESSORS def #{property_name} _regular_reader(#{property_name.to_s.inspect}) end def #{property_name}=(value) _regular_writer(#{property_name.to_s.inspect}, value) end ACCESSORS end if defined? @subclasses @subclasses.each { |klass| klass.property(property_name, ) } end end |
.property?(name) ⇒ Boolean
Check to see if the specified property has already been defined.
67 68 69 |
# File 'lib/hashie/dash.rb', line 67 def self.property?(name) properties.include? name.to_sym 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).
91 92 93 94 |
# File 'lib/hashie/dash.rb', line 91 def [](property) assert_property_exists! property super(property.to_s) end |
#[]=(property, value) ⇒ Object
Set a value on the Dash in a Hash-like way. Only works on pre-existing properties.
98 99 100 101 |
# File 'lib/hashie/dash.rb', line 98 def []=(property, value) assert_property_exists! property super(property.to_s, value) end |