Class: Gwooks::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gwooks/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Base

Returns a new instance of Base.



66
67
68
69
70
71
72
# File 'lib/gwooks/base.rb', line 66

def initialize(payload)
  if payload.is_a? String
    @payload = JSON.parse(payload)
  else
    @payload = payload
  end
end

Class Method Details

.call(payload) ⇒ Object



7
8
9
# File 'lib/gwooks/base.rb', line 7

def call(payload)
  new(payload).call
end

.hooksObject



11
12
13
# File 'lib/gwooks/base.rb', line 11

def hooks
  @_hooks ||= []
end

.payload_matches(key, pattern, &block) ⇒ Object



15
16
17
18
# File 'lib/gwooks/base.rb', line 15

def payload_matches(key, pattern, &block)
  @_hooks ||= []
  @_hooks << [key, pattern, block]
end

.ref(pattern, &block) ⇒ Object



20
21
22
# File 'lib/gwooks/base.rb', line 20

def ref(pattern, &block)
  payload_matches("ref", pattern, &block)
end

Instance Method Details

#callObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gwooks/base.rb', line 74

def call
  self.class.hooks.each do |hook|
    key, pattern, block = *hook
    target = resolve_key(key)
    if target.is_a? Array 
      match = target.map do |t|
        match_pattern(t, pattern)
      end.compact
      matching = match.compact.size > 0
    else
      match = match_pattern(target, pattern)
      matching = !match.nil? 
    end
    instance_exec(match, &block) if matching
  end
  nil
end