Class: Opencdd::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/opencdd/condition.rb

Constant Summary collapse

SET_PATTERN =
/\A\{(?<body>.*)\}\z/m.freeze
EXPRESSION_PATTERN =
/\A
  (?<left>[^=!<>]+?)
  \s*
  (?<op>==|!=)
  \s*
  (?<right>.+)
\z/x.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, operator, right) ⇒ Condition

Returns a new instance of Condition.



18
19
20
21
22
23
24
25
# File 'lib/opencdd/condition.rb', line 18

def initialize(left, operator, right)
  @left = left.to_s.strip
  unless %w[== !=].include?(operator.to_s)
    raise ArgumentError, "unsupported condition operator: #{operator.inspect}"
  end
  @operator = operator.to_s
  @right = right
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



16
17
18
# File 'lib/opencdd/condition.rb', line 16

def left
  @left
end

#operatorObject (readonly)

Returns the value of attribute operator.



16
17
18
# File 'lib/opencdd/condition.rb', line 16

def operator
  @operator
end

#rightObject (readonly)

Returns the value of attribute right.



16
17
18
# File 'lib/opencdd/condition.rb', line 16

def right
  @right
end

Class Method Details

.parse(raw) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/opencdd/condition.rb', line 71

def parse(raw)
  return nil if raw.nil? || raw.to_s.strip.empty?
  s = raw.to_s.strip
  if s =~ EXPRESSION_PATTERN
    left = Regexp.last_match(:left).strip
    op   = Regexp.last_match(:op)
    raw_right = Regexp.last_match(:right).strip
    new(left, op, parse_rhs(raw_right))
  else
    raise ArgumentError, "invalid condition expression: #{s.inspect}"
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



58
59
60
61
62
63
# File 'lib/opencdd/condition.rb', line 58

def ==(other)
  other.is_a?(Opencdd::Condition) &&
    @left == other.left &&
    @operator == other.operator &&
    normalize_for_eq(@right) == normalize_for_eq(other.right)
end

#hashObject



66
67
68
# File 'lib/opencdd/condition.rb', line 66

def hash
  [@left, @operator, normalize_for_eq(@right)].hash
end

#satisfied_by?(bindings) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/opencdd/condition.rb', line 31

def satisfied_by?(bindings)
  hash = bindings.is_a?(Hash) ? bindings : bindings.to_h
  actual = hash[@left] || hash[@left.to_sym]
  return false if actual.nil?

  if set?
    contained = @right.any? { |r| matches?(actual, r) }
    @operator == "==" ? contained : !contained
  else
    equal = matches?(actual, @right)
    @operator == "==" ? equal : !equal
  end
end

#set?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/opencdd/condition.rb', line 27

def set?
  @right.is_a?(Set)
end

#to_sObject Also known as: inspect



45
46
47
48
49
50
51
52
53
54
# File 'lib/opencdd/condition.rb', line 45

def to_s
  rhs = if set?
          "{ #{@right.to_a.join(", ")} }"
        elsif quoted_literal?(@right)
          "\"#{@right}\""
        else
          @right.to_s
        end
  "#{@left} #{@operator} #{rhs}"
end