Module: Icss::ReceiverModel::ActsAsHash
- Included in:
- Meta::NamedSchema, Meta::RecordField, Meta::RecordSchema, Icss::ReceiverModel
- Defined in:
- lib/icss/receiver_model/acts_as_hash.rb
Overview
Makes an Icss::RecordModel behave mostly like a hash.
By default, the hashlike methods iterate over the fields: instance #keys delegates to self.class.field_names. All methods are defined naturally on [], []= and has_key? so the interface is well-guarded.
in addition to the below, by including Enumerable, this also adds
:each_cons, :each_entry, :each_slice, :each_with_index, :each_with_object,
:map, :collect, :collect_concat, :entries, :to_a, :flat_map, :inject, :reduce,
:group_by, :chunk, :cycle, :partition, :reverse_each, :slice_before, :drop,
:drop_while, :take, :take_while, :detect, :find, :find_all, :find_index, :grep,
:all?, :any?, :none?, :one?, :first, :count, :zip :max, :max_by, :min, :min_by,
:minmax, :minmax_by, :sort, :sort_by
As opposed to hash, does not define
default, default=, default_proc, default_proc=, shift, flatten, compare_by_identity
compare_by_identity? rehash
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Hashlike#[].
-
#[]=(key, val) ⇒ Object
Hashlike#[]= Hashlike#store.
-
#attributes ⇒ Hash
Returns a hash with each key set to its associated value.
-
#delete(key, &block) ⇒ Object
Hashlike#delete.
-
#keys ⇒ Array
Hashlike#keys.
-
#to_hash ⇒ Object
Returns a hash with each key set to its associated value.
Class Method Details
.included(base) ⇒ Object
162 163 164 165 166 |
# File 'lib/icss/receiver_model/acts_as_hash.rb', line 162 def self.included(base) base.class_eval do include Gorillib::Hashlike end end |
Instance Method Details
#[](key) ⇒ Object
Hashlike#[]
Element Reference – Retrieves the value stored for key.
In a normal hash, a default value can be set; none is provided here.
Delegates to self.send(key)
45 46 47 48 |
# File 'lib/icss/receiver_model/acts_as_hash.rb', line 45 def [](key) key = convert_key(key) self.send(key) end |
#[]=(key, val) ⇒ Object
Hashlike#[]= Hashlike#store
Element Assignment – Associates the value given by val with the key given by key.
key should not have its value changed while it is in use as a key. In a normal hash, a String passed as a key will be duplicated and frozen. No such guarantee is provided here
Delegates to self.send(“key=”, val)
75 76 77 78 79 |
# File 'lib/icss/receiver_model/acts_as_hash.rb', line 75 def []=(key, val) key = convert_key(key) self.send("#{key}=", val) if respond_to?("#{key}=") val end |
#attributes ⇒ Hash
Returns a hash with each key set to its associated value.
145 146 147 148 149 150 151 |
# File 'lib/icss/receiver_model/acts_as_hash.rb', line 145 def attributes {}.tap do |hsh| each_pair do |key, val| hsh[key] = val.respond_to?(:to_hash) ? val.to_hash : val end end end |
#hsh.delete(key) ⇒ Object, Nil #hsh.delete(key) {|Object| ... } ⇒ Object, Nil
Hashlike#delete
Deletes and returns the value from hsh whose key is equal to key. If the optional code block is given and the key is not found, pass in the key and return the result of block.
In a normal hash, a default value can be set; none is provided here.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/icss/receiver_model/acts_as_hash.rb', line 106 def delete(key, &block) key = convert_key(key) if has_key?(key) val = self[key] self.send(:remove_instance_variable, "@#{key}") val elsif block_given? block.call(key) else nil end end |
#keys ⇒ Array
Hashlike#keys
Returns a new array populated with the keys from this hashlike.
131 132 133 |
# File 'lib/icss/receiver_model/acts_as_hash.rb', line 131 def keys self.class.field_names & instance_variables.map{|s| convert_key(s[1..-1]) } end |
#to_hash ⇒ Object
Returns a hash with each key set to its associated value.
Delegates to #attributes
158 159 160 |
# File 'lib/icss/receiver_model/acts_as_hash.rb', line 158 def to_hash attributes end |