Module: Apportion::Util
- Included in:
- Apportion
- Defined in:
- lib/apportion/util.rb
Overview
Utility methods
Class Method Summary collapse
-
.file_read_json_to_hash(path_to_directory, json_file_name) ⇒ Hash
Reads a JSON file and returns a hash with symbols as keys.
-
.hash_values_sum(hash) ⇒ Numeric
Sums all of the values in a hash.
-
.symbolize_keys(hash) ⇒ Hash
Returns a new hash with symbols as keys.
Class Method Details
.file_read_json_to_hash(path_to_directory, json_file_name) ⇒ Hash
Reads a JSON file and returns a hash with symbols as keys
15 16 17 18 |
# File 'lib/apportion/util.rb', line 15 def file_read_json_to_hash(path_to_directory, json_file_name) hash = JSON.parse(File.read(File.join(path_to_directory, json_file_name))) symbolize_keys(hash) end |
.hash_values_sum(hash) ⇒ Numeric
Sums all of the values in a hash
28 29 30 |
# File 'lib/apportion/util.rb', line 28 def hash_values_sum(hash) hash.values.reduce(0) { |acc, v| acc + v } end |
.symbolize_keys(hash) ⇒ Hash
Returns a new hash with symbols as keys
42 43 44 |
# File 'lib/apportion/util.rb', line 42 def symbolize_keys(hash) transform_keys(hash) { |key| key.to_sym rescue key } end |