Class: Liquid::RangeLookup

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

Defined Under Namespace

Classes: ParseTreeVisitor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_obj, end_obj) ⇒ RangeLookup

Returns a new instance of RangeLookup.



27
28
29
30
# File 'lib/liquid/range_lookup.rb', line 27

def initialize(start_obj, end_obj)
  @start_obj = start_obj
  @end_obj   = end_obj
end

Instance Attribute Details

#end_objObject (readonly)

Returns the value of attribute end_obj.



25
26
27
# File 'lib/liquid/range_lookup.rb', line 25

def end_obj
  @end_obj
end

#start_objObject (readonly)

Returns the value of attribute start_obj.



25
26
27
# File 'lib/liquid/range_lookup.rb', line 25

def start_obj
  @start_obj
end

Class Method Details

.parse(start_markup, end_markup) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/liquid/range_lookup.rb', line 5

def self.parse(start_markup, end_markup)
  start_obj = Expression.parse(start_markup)
  end_obj   = Expression.parse(end_markup)
  if start_obj.respond_to?(:evaluate) || end_obj.respond_to?(:evaluate)
    new(start_obj, end_obj)
  else
    begin
      start_obj.to_i..end_obj.to_i
    rescue NoMethodError
      invalid_expr = start_markup unless start_obj.respond_to?(:to_i)
      invalid_expr ||= end_markup unless end_obj.respond_to?(:to_i)
      if invalid_expr
        raise Liquid::SyntaxError, "Invalid expression type '#{invalid_expr}' in range expression"
      end

      raise
    end
  end
end

Instance Method Details

#evaluate(context) ⇒ Object



32
33
34
35
36
# File 'lib/liquid/range_lookup.rb', line 32

def evaluate(context)
  start_int = to_integer(context.evaluate(@start_obj))
  end_int   = to_integer(context.evaluate(@end_obj))
  start_int..end_int
end