Class: Ensconce::HashBuilder
- Inherits:
-
Object
- Object
- Ensconce::HashBuilder
- Defined in:
- lib/ensconce/hash_builder.rb
Overview
Used to convert pairs of arrays into a hash.
hash_builder = HashBuilder.new :keys => ['a', 'b'], :values => ['1', '2']
hash_builder.hash --> {'a' => '1', 'b' => '2'}
Also allows modification of keys or values
hash_builder.keys_mod = lambda {|key| key.upcase}
hash_builder.hash --> {'A' => '1', 'B' => '2'}
hash_builder.values_mod = lambda {|value| (value.to_i * 4).to_s}
hash_builder.hash --> {'A' => '4', 'B' => '8'}
You can use a Proc to define a mod, but I’d recommend not doing so as a return statement in the Proc can cause an unexpected result (see tests).
Instance Attribute Summary collapse
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#keys_mod ⇒ Object
Returns the value of attribute keys_mod.
-
#values ⇒ Object
Returns the value of attribute values.
-
#values_mod ⇒ Object
Returns the value of attribute values_mod.
Instance Method Summary collapse
- #check_mods_are_valid ⇒ Object
- #check_required_attributes_present ⇒ Object
- #hash ⇒ Object
-
#initialize(args = {}) ⇒ HashBuilder
constructor
A new instance of HashBuilder.
- #mod_attributes ⇒ Object
- #processed_keys ⇒ Object
- #processed_values ⇒ Object
- #required_attibutes ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(args = {}) ⇒ HashBuilder
Returns a new instance of HashBuilder.
23 24 25 26 27 28 |
# File 'lib/ensconce/hash_builder.rb', line 23 def initialize(args = {}) @keys = args[:keys] @values = args[:values] @keys_mod = args[:keys_mod] @values_mod = args[:values_mod] end |
Instance Attribute Details
#keys ⇒ Object
Returns the value of attribute keys.
21 22 23 |
# File 'lib/ensconce/hash_builder.rb', line 21 def keys @keys end |
#keys_mod ⇒ Object
Returns the value of attribute keys_mod.
21 22 23 |
# File 'lib/ensconce/hash_builder.rb', line 21 def keys_mod @keys_mod end |
#values ⇒ Object
Returns the value of attribute values.
21 22 23 |
# File 'lib/ensconce/hash_builder.rb', line 21 def values @values end |
#values_mod ⇒ Object
Returns the value of attribute values_mod.
21 22 23 |
# File 'lib/ensconce/hash_builder.rb', line 21 def values_mod @values_mod end |
Instance Method Details
#check_mods_are_valid ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ensconce/hash_builder.rb', line 41 def check_mods_are_valid mod_attributes.each do |mod| mod = send mod next unless mod raise ":#{mod} must be a Proc or lambda" unless mod.kind_of? Proc end end |
#check_required_attributes_present ⇒ Object
49 50 51 52 53 |
# File 'lib/ensconce/hash_builder.rb', line 49 def check_required_attributes_present required_attibutes.each do |attribute| raise ":#{attribute} is a required attribute but was not found" unless send(attribute) end end |
#hash ⇒ Object
35 36 37 38 39 |
# File 'lib/ensconce/hash_builder.rb', line 35 def hash valid? map = [processed_keys, processed_values].transpose Hash[map] end |
#mod_attributes ⇒ Object
59 60 61 |
# File 'lib/ensconce/hash_builder.rb', line 59 def mod_attributes [:keys_mod, :values_mod] end |
#processed_keys ⇒ Object
63 64 65 |
# File 'lib/ensconce/hash_builder.rb', line 63 def processed_keys keys_mod ? keys.collect(&keys_mod) : keys end |
#processed_values ⇒ Object
67 68 69 |
# File 'lib/ensconce/hash_builder.rb', line 67 def processed_values values_mod ? values.collect(&values_mod) : values end |
#required_attibutes ⇒ Object
55 56 57 |
# File 'lib/ensconce/hash_builder.rb', line 55 def required_attibutes [:keys, :values] end |
#valid? ⇒ Boolean
30 31 32 33 |
# File 'lib/ensconce/hash_builder.rb', line 30 def valid? check_required_attributes_present check_mods_are_valid end |