Module: RailsUtil::Util

Defined in:
lib/rails_util/util.rb

Overview

RailsUtil::Util includes class helper methods for handling nested hashes

Class Method Summary collapse

Class Method Details

.path_to_hash(path, value) ⇒ Hash

Creates a nested hash given a path



28
29
30
31
32
# File 'lib/rails_util/util.rb', line 28

def self.path_to_hash(path, value)
  parts = (path.is_a?(String) ? path.split('.') : path).reverse
  initial = { parts.shift => value }
  parts.reduce(initial) { |a, e| { e => a } }
end

.set_nested(path, value, obj = {}) ⇒ Hash

Deep merges a nested hash given a path Does not mutate the original hash



10
11
12
# File 'lib/rails_util/util.rb', line 10

def self.set_nested(path, value, obj={})
  obj.deep_merge(path_to_hash(path, value))
end

.set_nested!(path, value, obj = {}) ⇒ Hash

Deep merges a nested hash given a path Mutates the original hash



20
21
22
# File 'lib/rails_util/util.rb', line 20

def self.set_nested!(path, value, obj={})
  obj.deep_merge!(path_to_hash(path, value))
end

.underscored_class_name(obj) ⇒ String

Returns the underscored class name of an ActiveRecord object



37
38
39
# File 'lib/rails_util/util.rb', line 37

def self.underscored_class_name(obj)
  obj.class.to_s.underscore
end