Module: Dbee::Util::MakeKeyedBy
- Included in:
- Model, Model::Relationships
- Defined in:
- lib/dbee/util/make_keyed_by.rb
Overview
Provides a “make_keyed_by” method which extends the Hashable gem’s concept of a “make” method.
Instance Method Summary collapse
-
#make_keyed_by(key_attrib, spec_hash) ⇒ Object
Given a hash of hashes or a hash of values of instances of this class, a hash is returned where all of the values are instances of this class and the keys are the string versions of the original hash.
Instance Method Details
#make_keyed_by(key_attrib, spec_hash) ⇒ Object
Given a hash of hashes or a hash of values of instances of this class, a hash is returned where all of the values are instances of this class and the keys are the string versions of the original hash.
An ArgumentError is raised if the value’s key_attrib
is not equal to the top level hash key. This ensures that the key_attrib
is the same in the incoming hash and the value.
This is useful for cases where it makes sense in the configuration (YAML) specification to represent certain objects in a hash structure instead of a list.
26 27 28 29 30 31 32 33 |
# File 'lib/dbee/util/make_keyed_by.rb', line 26 def make_keyed_by(key_attrib, spec_hash) # Once Ruby 2.5 support is dropped, this can just use the block form of # #to_h. spec_hash.map do |key, spec| string_key = key.to_s [string_key, make_value_checking_key_attib!(key_attrib, string_key, spec)] end.to_h end |