Sha256: f5709abdd2b08a5f7b5133dac2c1eaf0ccaecdd607e9a3e91b6a943fc232f3c7

Contents?: true

Size: 1.69 KB

Versions: 12

Compression:

Stored size: 1.69 KB

Contents

class Card
  module Query
    class CardQuery
      # normalize clause's keys and values.
      module Normalization
        def normalize_clause clause
          clause = clause_to_hash clause
          clause.symbolize_keys!
          clause.each do |key, val|
            next if key.to_sym == :return
            clause[key] = normalize_value val
          end
          clause
        end

        def clause_to_hash clause
          case clause
          when Hash              then clause
          when String            then { key: clause.to_name.key }
          when Integer           then { id: clause }
          else raise Card::Error::BadQuery, "Invalid query args #{clause.inspect}"
          end
        end

        def normalize_value val
          case val
          when Integer, Float, Symbol, Hash then val
          when String                       then normalize_string_value val
          when Array                        then normalize_array_value val
          else raise Card::Error::BadQuery, "unknown WQL value type: #{val.class}"
          end
        end

        def normalize_array_value val
          val.map { |v| normalize_value v }
        end

        def normalize_string_value val
          case val.to_s
          when /^\$(\w+)$/
            # replace from @vars when value starts with dollar sign
            string_value_from_var Regexp.last_match[1]
          when /\b_/
            # absolutize based on @context when there are words beginning with "_"
            val.to_name.absolute(context)
          else
            val
          end
        end

        def string_value_from_var varname
          @vars[varname.to_sym].to_s.strip
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
card-1.96.7 lib/card/query/card_query/normalization.rb
card-1.96.6 lib/card/query/card_query/normalization.rb
card-1.96.5 lib/card/query/card_query/normalization.rb
card-1.96.4 lib/card/query/card_query/normalization.rb
card-1.96.3 lib/card/query/card_query/normalization.rb
card-1.96.2 lib/card/query/card_query/normalization.rb
card-1.96.1 lib/card/query/card_query/normalization.rb
card-1.96.0 lib/card/query/card_query/normalization.rb
card-1.95.3 lib/card/query/card_query/normalization.rb
card-1.95.2 lib/card/query/card_query/normalization.rb
card-1.95.1 lib/card/query/card_query/normalization.rb
card-1.95.0 lib/card/query/card_query/normalization.rb