Sha256: edaf20f51b581dfe270433c3ed3bc0c6d7f2c043ee9cb5cca7a6f35d5493728a

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

module CloudSesame
  module Query
    module AST
      class RangeValue < Value

        STRING_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.match value)
            @data = match.captures
          else
            default_range
          end
        end

        def compile
          "#{ lb }#{ l.to_s },#{ u.to_s }#{ ub }"
        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 l
          data[1]
        end

        def u
          data[2]
        end

        private

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

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

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

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

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

      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
CloudSesame-0.3.6 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.3.5 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.3.4 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.3.3 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.3.2 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.3.1 lib/cloud_sesame/query/ast/range_value.rb
CloudSesame-0.3.0 lib/cloud_sesame/query/ast/range_value.rb