Sha256: 4dd0a12acd6f08efff232917463da97954cdfd41b4d8d1e8405165f9ef1840ad

Contents?: true

Size: 1.26 KB

Versions: 54

Compression:

Stored size: 1.26 KB

Contents

module Dugway
  module Filters
    module ComparisonFilters
      def num_gt(input, operand)
        to_number(input) > to_number(operand)
      end

      def num_lt(input, operand)
        to_number(input) < to_number(operand)
      end

      def num_eq(input, operand)
        to_number(input) == to_number(operand)
      end

      def num_lte(input, operand)
        num_eq(input, operand) || num_lt(input, operand)
      end

      def num_gte(input, operand)
        num_eq(input, operand) || num_gt(input, operand)
      end

      private

      def to_number(obj)
        case obj
        when Numeric
          obj
        when String
          (obj.strip =~ /^\d+\.\d+$/) ? obj.to_f : obj.to_i
        else
          0
        end
      end
    end
  end
end

Liquid::Condition.send(:include, Dugway::Filters::ComparisonFilters)

Liquid::Condition.class_eval do
  operators['num_lt']  = lambda { |cond, left, right| cond.num_lt(left, right) }
  operators['num_lte'] = lambda { |cond, left, right| cond.num_lte(left, right) }
  operators['num_gt']  = lambda { |cond, left, right| cond.num_gt(left, right) }
  operators['num_gte'] = lambda { |cond, left, right| cond.num_gte(left, right) }
  operators['num_eq']  = lambda { |cond, left, right| cond.num_eq(left, right) }
end

Version data entries

54 entries across 54 versions & 1 rubygems

Version Path
dugway-0.11.2 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.11.1 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.11.0 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.10.5 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.10.4 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.10.3 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.10.2 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.10.1 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.10.0 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.9.0 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.8.4 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.8.3 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.8.2 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.8.1 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.8.0 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.7.1 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.7.0 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.6.7 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.6.6 lib/dugway/liquid/filters/comparison_filters.rb
dugway-0.6.5 lib/dugway/liquid/filters/comparison_filters.rb