Class: KeyParts

Inherits:
Object show all
Defined in:
lib/nested_hash_tricks/key_parts.rb

Class Method Summary collapse

Class Method Details

.mult?(str) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/nested_hash_tricks/key_parts.rb', line 15

def self.mult?(str)
  str.to_s.split('.').size > 1
end

.single?(str) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/nested_hash_tricks/key_parts.rb', line 18

def self.single?(str)
  !mult?(str)
end

.with_parts(str, ops = {}) {|start, lst, mult| ... } ⇒ Object

Yields:

  • (start, lst, mult)


2
3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/nested_hash_tricks/key_parts.rb', line 2

def self.with_parts(str,ops={})
  parts = str.kind_of?(String) ? str.to_s.split('.') : str
  if parts.size > 1
    start = ops[:array] ? parts[0..-2] : parts[0..-2].join(".")
    lst = parts[-1]
    mult = true
  else
    start = str
    lst = nil
    mult = false
  end
  yield(start,lst,mult)
end