Module: Fat

Defined in:
lib/fat.rb

Constant Summary collapse

FatError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.at(hash, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fat.rb', line 4

def self.at(hash, *args)
  fields = parse_args(*args)

  fields[0..-1].each_with_index do |field, index|
    hash = hash[field]
    if index != fields.length - 1 && !hash.kind_of?(Hash)
      raise Fat::FatError, "No hash found at #{fields[0..index].join(".")}"
    end
  end

  hash
end

.parse_args(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fat.rb', line 17

def self.parse_args(*args)
  return args if args.length > 1

  fields = args.first.split(".")

  if fields.length == 1
    fields = args.first.split(":")

    if fields.length == 1
      raise FatError, "Single argument expected to be a namespace with dots (.) or colons (:)"
    else
      fields.map(&:to_sym)
    end
  else
    fields
  end
end

Instance Method Details

#at(*args) ⇒ Object



35
36
37
# File 'lib/fat.rb', line 35

def at(*args)
  Fat.at(self, *args)
end