Module: Card::Query::CardQuery::Normalization

Included in:
Card::Query::CardQuery
Defined in:
lib/card/query/card_query/normalization.rb

Overview

normalize clause's keys and values.

Instance Method Summary collapse

Instance Method Details

#clause_to_hash(clause) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/card/query/card_query/normalization.rb', line 16

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

#normalize_array_value(val) ⇒ Object



34
35
36
# File 'lib/card/query/card_query/normalization.rb', line 34

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

#normalize_clause(clause) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/card/query/card_query/normalization.rb', line 6

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

#normalize_string_value(val) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/card/query/card_query/normalization.rb', line 38

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

#normalize_value(val) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/card/query/card_query/normalization.rb', line 25

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

#string_value_from_var(varname) ⇒ Object



51
52
53
# File 'lib/card/query/card_query/normalization.rb', line 51

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