Sha256: af685e2cb1237bd50ce7391d6e3200c4362c14e2da13548c52d5df6d196f5fe9

Contents?: true

Size: 1.48 KB

Versions: 7

Compression:

Stored size: 1.48 KB

Contents

class Card
  class Query
    class Value

      include Clause

      attr_reader :query, :operator, :value

      def initialize rawvalue, query
        @query = query
        @operator, @value = parse_value rawvalue
        canonicalize_operator
      end

      def parse_value rawvalue
        case rawvalue
        when String, Integer then ['=', rawvalue]
        when Array           then [rawvalue[0], rawvalue[1..-1]]
        else raise("Invalid Condition Clause #{rawvalue}.inspect}")
        end
      end

      def canonicalize_operator
        if target = OPERATORS[@operator.to_s]
          @operator = target
        else
          fail BadQuery, "Invalid Operator #{@operator}"
        end
      end


      def sqlize v
        case v
        when Query;  v.to_sql
        when Array;  "(" + v.flatten.collect {|x| sqlize(x)}.join(',') + ")"
        else quote(v.to_s)
        end
      end

      def to_sql field
        op,v = @operator, @value
        table = @query.table_alias

        field, v = case field.to_s
          when "name"
            ["#{table}.key", [v].flatten.map(&:to_name).map(&:key)]
          when "content"
            ["#{table}.db_content", v]
          else
            ["#{table}.#{safe_sql field}", v]
          end

        v = v[0] if Array===v && v.length==1 && op != 'in'
        if op=='~'
          cxn, v = match_prep(v)
          %{#{field} #{cxn.match(sqlize(v))}}
        else
          "#{field} #{op} #{sqlize(v)}"
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
card-1.17.4 lib/card/query/value.rb
card-1.17.3 lib/card/query/value.rb
card-1.17.2 lib/card/query/value.rb
card-1.17.1 lib/card/query/value.rb
card-1.17.0 lib/card/query/value.rb
card-1.16.15 lib/card/query/value.rb
card-1.16.14 lib/card/query/value.rb