Class: BaseRule

Inherits:
Object
  • Object
show all
Defined in:
lib/claws/base_rule.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: nil) ⇒ BaseRule

Returns a new instance of BaseRule.



73
74
75
76
77
78
# File 'lib/claws/base_rule.rb', line 73

def initialize(configuration: nil)
  @on_workflow = self.class.instance_variable_get(:@on_workflow) || []
  @on_job = self.class.instance_variable_get(:@on_job) || []
  @on_step = self.class.instance_variable_get(:@on_step) || []
  @configuration = configuration
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



2
3
4
# File 'lib/claws/base_rule.rb', line 2

def configuration
  @configuration
end

#on_jobObject

Returns the value of attribute on_job.



2
3
4
# File 'lib/claws/base_rule.rb', line 2

def on_job
  @on_job
end

#on_stepObject

Returns the value of attribute on_step.



2
3
4
# File 'lib/claws/base_rule.rb', line 2

def on_step
  @on_step
end

#on_workflowObject

Returns the value of attribute on_workflow.



2
3
4
# File 'lib/claws/base_rule.rb', line 2

def on_workflow
  @on_workflow
end

Class Method Details

.description(value) ⇒ Object



44
45
46
# File 'lib/claws/base_rule.rb', line 44

def self.description(value)
  define_method(:description) { value }
end

.extract_value(value, highlight: nil, debug: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/claws/base_rule.rb', line 62

def self.extract_value(value, highlight: nil, debug: false)
  case value
  when String
    { expression: parse_rule(value), highlight:, debug: }
  when Symbol
    value
  else
    raise "Hook must receive either a String (rule) or Symbol (method name), not: #{value.class}"
  end
end

.name(value) ⇒ Object



40
41
42
# File 'lib/claws/base_rule.rb', line 40

def self.name(value)
  define_method(:name) { value }
end

.on_job(value, highlight: nil, debug: false) ⇒ Object



52
53
54
55
# File 'lib/claws/base_rule.rb', line 52

def self.on_job(value, highlight: nil, debug: false)
  highlight = highlight.to_s unless highlight.nil?
  (@on_job ||= []) << extract_value(value, highlight:, debug:)
end

.on_step(value, highlight: nil, debug: false) ⇒ Object



57
58
59
60
# File 'lib/claws/base_rule.rb', line 57

def self.on_step(value, highlight: nil, debug: false)
  highlight = highlight.to_s unless highlight.nil?
  (@on_step ||= []) << extract_value(value, highlight:, debug:)
end

.on_workflow(value, highlight: nil, debug: false) ⇒ Object



48
49
50
# File 'lib/claws/base_rule.rb', line 48

def self.on_workflow(value, highlight: nil, debug: false)
  (@on_workflow ||= []) << extract_value(value, highlight:, debug:)
end

.parse_rule(rule) ⇒ Object

rubocop:disable Metrics/AbcSize



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/claws/base_rule.rb', line 4

def self.parse_rule(rule) # rubocop:disable Metrics/AbcSize
  ExpressionParser.parse_expression(rule).tap do |expression|
    expression.instance_eval do
      def ctx # rubocop:disable Metrics/AbcSize
        @ctx ||= Context.new(
          default: {},
          methods: {
            contains: ->(haystack, needle) { !haystack.nil? and haystack.include? needle },
            contains_any: ->(haystack, needles) { !haystack.nil? and needles.any? { |n| haystack.include? n } },
            startswith: ->(string, needle) { string.to_s.start_with? needle },
            endswith: ->(string, needle) { string.to_s.end_with? needle },
            difference: ->(arr1, arr2) { arr1.difference arr2 },
            intersection: ->(arr1, arr2) { arr1.intersection arr2 },
            get_key: ->(arr, key) { (arr || {}).fetch(key, nil) },
            count: ->(n) { n.length }
          }
        )
      end

      def eval_with(values: {})
        value(
          ctx: ctx.tap { |c| c.transient_symbols = values }
        )
      end

      def inspect
        to_s
      end

      def to_s
        "<Expression '#{input}'>"
      end
    end
  end
end

Instance Method Details

#dataObject



92
93
94
# File 'lib/claws/base_rule.rb', line 92

def data
  {}
end

#inspectObject



84
85
86
# File 'lib/claws/base_rule.rb', line 84

def inspect
  to_s
end

#nameObject



80
81
82
# File 'lib/claws/base_rule.rb', line 80

def name
  self.class.to_s.split("::").last
end

#to_sObject



88
89
90
# File 'lib/claws/base_rule.rb', line 88

def to_s
  "<Rule #{name} (#{@on_workflow.length} Workflow Rules; #{@on_job.length} Job Rules; #{@on_step.length} Step Rules)>"
end