Class: Liquid::DecisionBlock

Inherits:
Block
  • Object
show all
Defined in:
lib/liquid/standardtags.rb

Direct Known Subclasses

Case, If

Instance Attribute Summary

Attributes inherited from Tag

#nodelist

Instance Method Summary collapse

Methods inherited from Block

#block_delimiter, #block_name, #create_variable, #end_tag, #parse, #render, #unknown_tag

Methods inherited from Tag

#initialize, #name, #parse, #render

Constructor Details

This class inherits a constructor from Liquid::Tag

Instance Method Details

#equal_variables(right, left) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/liquid/standardtags.rb', line 170

def equal_variables(right, left)
  if left.is_a?(Symbol)
    if right.respond_to?(left.to_s)
      return right.send(left.to_s) 
    else
      raise ArgumentError.new("Error in tag '#{name}' - Cannot call method #{left} on type #{right.class}}")
    end
  end

  if right.is_a?(Symbol)
    if left.respond_to?(right.to_s)
      return left.send(right.to_s) 
    else
      raise ArgumentError.new("Error in tag '#{name}' - Cannot call method #{right} on type #{left.class}}")
    end
  end

  left == right      
end

#interpret_condition(left, right, op, context) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/liquid/standardtags.rb', line 190

def interpret_condition(left, right, op, context)   

  # If the operator is empty this means that the decision statement is just 
  # a single variable. We can just poll this variable from the context and 
  # return this as the result.
  return context[left] if op == nil      
  
  left, right = context[left], context[right]
  
  operation = case op
  when '==' then return equal_variables(left, right)
  when '!=' then return !equal_variables(left, right)
  when '>'  then :>
  when '<'  then :<
  when '>=' then :>=
  when '<=' then :<=
  else
    raise ArgumentError.new("Error in tag '#{name}' - Unknown operator #{op}")        
  end
  
  if left.respond_to?(operation) and right.respond_to?(operation)
    left.send(operation, right)      
  else
    nil
  end
end