Class: Flok::HooksManifestEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/flok/hooks_compiler.rb

Overview

When HooksManifest goes line by line, a line is considered matching when this entry retruns true for its does_match? function. You pass it a name, optional parameter query and the block to call when the code should be generated for a match. The block receives a full copy of the parameters for the hook entry (the static ones). The param query is a lambda that also receives the block and it should return (next) true/false when a match is considered true. If name is just “*” (string, not symbol), then it matches all names. Passing multiple param_queries in is allowed, you just pass in multiple arrays. All of the procs passed must be true for the result to be true

E.g. - This would match //HOOK_ENTRY >HooksManifestEntry.new(“my_event”, ->(p).include? “x”) do |hook_info|

return "console.log(info = #{info["actions"]});"

end

Instance Method Summary collapse

Constructor Details

#initialize(name, param_queries = ->(p){true}, &block) ⇒ HooksManifestEntry

Returns a new instance of HooksManifestEntry.



70
71
72
73
74
75
76
77
# File 'lib/flok/hooks_compiler.rb', line 70

def initialize name, param_queries=->(p){true}, &block
  #If an array is not passed, make it an array with one element
  param_queries = [param_queries] if param_queries.class != Array

  @name = name.to_s
  @param_queries = param_queries
  @block = block
end

Instance Method Details

#code_for_line(line) ⇒ Object



89
90
91
# File 'lib/flok/hooks_compiler.rb', line 89

def code_for_line line
  return @block.call((line))
end

#does_match?(line) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
84
85
86
87
# File 'lib/flok/hooks_compiler.rb', line 79

def does_match? line
  #Unless the matching name is a *, check to see if the hook name matches
  unless @name == "*"
    return false unless line =~ /\/\/HOOK_ENTRY\[#{@name}\]/
  end

  #Now verify with the lambda
  return @param_queries.reduce(true){|r, e| r &&= e.call((line))}
end

#hook_entry_info_from_line(line) ⇒ Object

Each hook entry contains a JSON encoded set of static parameters for things like the controller name, etc. See docs for a list of parameters as it depends on the hook entry



96
97
98
99
100
101
102
103
# File 'lib/flok/hooks_compiler.rb', line 96

def  line
  json_info = line.split(/\/\/HOOK_ENTRY\[.*?\]/).last.strip 
  begin
    return JSON.parse(json_info)
  rescue => e
    raise "Couldn't parse the hooks entry JSON information, got #{json_info.inspect}: #{e.inspect}"
  end
end