Class: BabySqueel::Compat::KeyPath

Inherits:
Object
  • Object
show all
Defined in:
lib/baby_squeel/compat.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*path) ⇒ KeyPath

Returns a new instance of KeyPath.



30
31
32
# File 'lib/baby_squeel/compat.rb', line 30

def initialize(*path)
  @path = path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object (private)



46
47
48
# File 'lib/baby_squeel/compat.rb', line 46

def method_missing(name, *)
  self.class.new(*@path, name)
end

Class Method Details

.evaluate(&block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/baby_squeel/compat.rb', line 12

def self.evaluate(&block)
  if block.arity.zero?
    unwrap new.instance_eval(&block)
  else
    unwrap yield(new)
  end
end

.unwrap(path) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/baby_squeel/compat.rb', line 20

def self.unwrap(path)
  if path.kind_of? self
    path.value
  elsif path.respond_to? :map
    path.map(&method(:unwrap))
  else
    []
  end
end

Instance Method Details

#valueObject



34
35
36
37
38
# File 'lib/baby_squeel/compat.rb', line 34

def value
  @path.reverse.reduce({}) do |acc, name|
    { name => acc }
  end
end