Class: LMDocstache::Condition

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

Constant Summary collapse

InvalidOperator =
Class.new(StandardError)
ALLOWED_OPERATORS =
%w(== ~=).freeze
STARTING_QUOTES =
%w(' " )
ENDING_QUOTES =
%w(' " )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left_term:, right_term:, operator:, negation: false, original_match: nil) ⇒ Condition

Returns a new instance of Condition.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lm_docstache/condition.rb', line 11

def initialize(left_term:, right_term:, operator:, negation: false, original_match: nil)
  @left_term = left_term
  @right_term = remove_quotes(right_term)
  @operator = operator
  @negation = negation
  @original_match = original_match

  unless ALLOWED_OPERATORS.include?(operator)
    raise InvalidOperator, "Operator '#{operator}' is invalid"
  end
end

Instance Attribute Details

#left_termObject (readonly)

Returns the value of attribute left_term.



9
10
11
# File 'lib/lm_docstache/condition.rb', line 9

def left_term
  @left_term
end

#negationObject (readonly)

Returns the value of attribute negation.



9
10
11
# File 'lib/lm_docstache/condition.rb', line 9

def negation
  @negation
end

#operatorObject (readonly)

Returns the value of attribute operator.



9
10
11
# File 'lib/lm_docstache/condition.rb', line 9

def operator
  @operator
end

#original_matchObject (readonly)

Returns the value of attribute original_match.



9
10
11
# File 'lib/lm_docstache/condition.rb', line 9

def original_match
  @original_match
end

#right_termObject (readonly)

Returns the value of attribute right_term.



9
10
11
# File 'lib/lm_docstache/condition.rb', line 9

def right_term
  @right_term
end

Instance Method Details

#truthy?(value) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
# File 'lib/lm_docstache/condition.rb', line 23

def truthy?(value)
  result = value.to_s.send(operator, right_term)
  negation ? !result : result
end