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.



94
95
96
97
98
99
# File 'lib/claws/base_rule.rb', line 94

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

#configuration ⇒ Object

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

#on_job ⇒ Object

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_step ⇒ Object

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_workflow ⇒ Object

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



65
66
67
# File 'lib/claws/base_rule.rb', line 65

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

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/claws/base_rule.rb', line 83

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



61
62
63
# File 'lib/claws/base_rule.rb', line 61

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

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



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

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



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

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



69
70
71
# File 'lib/claws/base_rule.rb', line 69

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, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/claws/base_rule.rb', line 4

def self.parse_rule(rule) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  ExpressionParser.parse_expression(rule).tap do |expression|
    expression.instance_eval do
      def ctx # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
        @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 },
            dig: lambda { |object, path, default = nil|
              # sometimes we might want to traverse the object as if it were a hash
              # sometimes we might want to traverse it as a Ruby object
              # annoying up front, but the edge cases are few and keeps expressions simple
              path.to_s.split(".").reduce(object) do |current, part|
                return default if current.nil?

                if current.is_a?(Hash)
                  # Prefer exact string key, then symbol key
                  if current.key?(part)
                    current[part]
                  elsif current.key?(part.to_sym)
                    current[part.to_sym]
                  else
                    default
                  end
                else
                  current.respond_to?(part) ? current.public_send(part) : default
                end
              end
            }
          }
        )
      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

#data ⇒ Object



113
114
115
# File 'lib/claws/base_rule.rb', line 113

def data
  {}
end

#inspect ⇒ Object



105
106
107
# File 'lib/claws/base_rule.rb', line 105

def inspect
  to_s
end

#name ⇒ Object



101
102
103
# File 'lib/claws/base_rule.rb', line 101

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

#to_s ⇒ Object



109
110
111
# File 'lib/claws/base_rule.rb', line 109

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