Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/stylish/core_ext.rb
Instance Method Summary collapse
- #lookup_path(dot_separated_path, create = false) ⇒ Object
- #make_path(dot_separated_path, blank_value, overwrite = false) ⇒ Object
- #to_mash ⇒ Object
Instance Method Details
#lookup_path(dot_separated_path, create = false) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/stylish/core_ext.rb', line 14 def lookup_path dot_separated_path, create=false parts = dot_separated_path.to_s.split('.') parts.reduce(self) do |memo, part| memo && memo[part.to_sym] ||= memo[part.to_s] || (create if create) end end |
#make_path(dot_separated_path, blank_value, overwrite = false) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/stylish/core_ext.rb', line 22 def make_path dot_separated_path, blank_value, overwrite=false dot_separated_path = dot_separated_path.to_s existing = lookup_path(dot_separated_path) return existing if existing && !overwrite parts = dot_separated_path.split(".") current = self if parts.length == 1 return self[parts.first] ||= blank_value end key = parts.pop parts.each do |part| current = current[part.to_sym] = current.delete(part.to_s) || {} end current[key.to_sym] = blank_value lookup_path(dot_separated_path, false) end |
#to_mash ⇒ Object
10 11 12 |
# File 'lib/stylish/core_ext.rb', line 10 def to_mash Hashie::Mash.new(self) end |