Class: Opencdd::Condition
- Inherits:
-
Object
- Object
- Opencdd::Condition
- 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
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#operator ⇒ Object
readonly
Returns the value of attribute operator.
-
#right ⇒ Object
readonly
Returns the value of attribute right.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#initialize(left, operator, right) ⇒ Condition
constructor
A new instance of Condition.
- #satisfied_by?(bindings) ⇒ Boolean
- #set? ⇒ Boolean
- #to_s ⇒ Object (also: #inspect)
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
#left ⇒ Object (readonly)
Returns the value of attribute left.
16 17 18 |
# File 'lib/opencdd/condition.rb', line 16 def left @left end |
#operator ⇒ Object (readonly)
Returns the value of attribute operator.
16 17 18 |
# File 'lib/opencdd/condition.rb', line 16 def operator @operator end |
#right ⇒ Object (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 |
#hash ⇒ Object
66 67 68 |
# File 'lib/opencdd/condition.rb', line 66 def hash [@left, @operator, normalize_for_eq(@right)].hash end |
#satisfied_by?(bindings) ⇒ 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
27 28 29 |
# File 'lib/opencdd/condition.rb', line 27 def set? @right.is_a?(Set) end |
#to_s ⇒ Object 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 |