Sha256: 84df78b0a15b06e0f506099fedefa4f05f5880871dbd5341939ee157a9a2a7dc

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

module CloudSesame
  module Query
    module AST
      class RangeValue < Value

        RANGE_FORMAT = /\A(\[|{)(.*),(.*)(\}|\])\z/

        def initialize(value = nil)
          @data = if value.kind_of?(Range)
            range_to_array(value)
          elsif value.is_a?(String) && (match = string_format?(value))
            (matches = match.captures)[1, 2] = matches[1, 2].map do |i|
              Value.parse(i) unless i.nil? || i.empty?
            end
            @data = matches
          else
            default_range
          end
        end

        def gt(value = nil)
          data[0], data[1] = '{', Value.parse(value) if value
          return self
        end

        def gte(value = nil)
          data[0], data[1] = '[', Value.parse(value) if value
          return self
        end

        def lt(value = nil)
          data[2], data[3] = Value.parse(value), '}' if value
          return self
        end

        def lte(value = nil)
          data[2], data[3] = Value.parse(value), ']' if value
          return self
        end

        def compile
          "#{ lb }#{ l.to_s },#{ u.to_s }#{ ub }"
        end

        def ==(object)
          data == Value.parse(object).data
        end

        private

        def string_format?(string)
           RANGE_FORMAT.match string.tr(' ', '')
        end

        def range_to_array(range)
          ['[', range.begin, range.end, end_symbol(range)]
        end

        def end_symbol(value)
          value.exclude_end? ? '}' : ']'
        end

        def default_range
          ['{', nil, nil, '}']
        end

        def l
          data[1]
        end

        def u
          data[2]
        end

        def lb
          data[1] ? data[0] : '{'
        end

        def ub
          data[2] ? data[3] : '}'
        end

      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
CloudSesame-0.6.3 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.6.2 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.6.1 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.6.0 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.5.5 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.5.4 lib/cloud_sesame/query/ast/range_value.rb