Class: LcpRuby::Dsl::ConditionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/dsl/condition_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConditionBuilder

Returns a new instance of ConditionBuilder.



4
5
6
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 4

def initialize
  @conditions = []
end

Class Method Details

.build(&block) ⇒ Object

DSL entry point: builds a condition hash from a block



91
92
93
94
95
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 91

def self.build(&block)
  builder = new
  builder.instance_eval(&block)
  builder.to_condition
end

.lookup(model, match:, pick:) ⇒ Object

Returns a lookup value reference hash for use in condition values. Usage: field(:price).lt(ConditionBuilder.lookup(:tax_limit, match: { key: “vat_a” }, pick: :threshold)) Match values may be literals or dotted dynamic references (‘record.tax_key`, `current_user.id`, etc.).



85
86
87
88
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 85

def self.lookup(model, match:, pick:)
  normalized_match = match.transform_keys(&:to_s).transform_values { |v| v.is_a?(Hash) ? v.transform_keys(&:to_s) : v }
  { "lookup" => model.to_s, "match" => normalized_match, "pick" => pick.to_s }
end

Instance Method Details

#add_condition(condition) ⇒ Object

Adds a raw condition hash (used by FieldConditionProxy)



77
78
79
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 77

def add_condition(condition)
  @conditions << condition
end

#all(&block) ⇒ Object

AND — all child conditions must be true



14
15
16
17
18
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 14

def all(&block)
  builder = ConditionBuilder.new
  builder.instance_eval(&block)
  @conditions << { "all" => builder.to_conditions }
end

#any(&block) ⇒ Object

OR — at least one child condition must be true



21
22
23
24
25
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 21

def any(&block)
  builder = ConditionBuilder.new
  builder.instance_eval(&block)
  @conditions << { "any" => builder.to_conditions }
end

#collection(name, quantifier: :any, &block) ⇒ Object

Collection condition with quantifier



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 37

def collection(name, quantifier: :any, &block)
  builder = ConditionBuilder.new
  builder.instance_eval(&block)
  children = builder.to_conditions
  inner = children.size == 1 ? children.first : { "all" => children }
  @conditions << {
    "collection" => name.to_s,
    "quantifier" => quantifier.to_s,
    "condition" => inner
  }
end

#field(name) ⇒ Object

Adds a field condition proxy for building { field, operator, value } hashes



9
10
11
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 9

def field(name)
  FieldConditionProxy.new(name.to_s, self)
end

#not_condition(&block) ⇒ Object

NOT — negates the child condition



28
29
30
31
32
33
34
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 28

def not_condition(&block)
  builder = ConditionBuilder.new
  builder.instance_eval(&block)
  children = builder.to_conditions
  child = children.size == 1 ? children.first : { "all" => children }
  @conditions << { "not" => child }
end

#service(name, params: nil) ⇒ Object

Service condition. ‘params:` (optional) is a Hash of kwargs passed to the service via ConditionEvaluator (mirrors the YAML `params:` sibling). Param values may be literals or dotted dynamic references (`current_user.id`, `record.amount`, `current_date`, etc.).



53
54
55
56
57
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 53

def service(name, params: nil)
  entry = { "service" => name.to_s }
  entry["params"] = LcpRuby::HashUtils.stringify_deep(params) if params
  @conditions << entry
end

#to_conditionObject

Returns a single condition hash (wraps in ‘all’ if multiple)



65
66
67
68
69
70
71
72
73
74
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 65

def to_condition
  case @conditions.size
  when 0
    raise ArgumentError, "condition block is empty"
  when 1
    @conditions.first
  else
    { "all" => @conditions }
  end
end

#to_conditionsObject

Returns the built conditions as an array



60
61
62
# File 'lib/lcp_ruby/dsl/condition_builder.rb', line 60

def to_conditions
  @conditions
end