module Card::Query::CardQuery::Normalization

normalize clause’s keys and values.

Constants

OK_VALUE_CLASSES

Private Instance Methods

clause_to_hash(clause) click to toggle source
# File lib/card/query/card_query/normalization.rb, line 27
def clause_to_hash clause
  case clause
  when Hash              then clause
  when Integer           then { id: clause }
  when String            then { id: (Card::Lexicon.id(clause) || -2) }
  when Symbol            then { id: Card::Codename.id(clause) }
  else raise Error::BadQuery, "Invalid clause: #{clause.inspect}"
  end
end
normalize_array_value(val) click to toggle source
# File lib/card/query/card_query/normalization.rb, line 46
def normalize_array_value val
  val.map { |v| normalize_value v }
end
normalize_clause(clause) click to toggle source
# File lib/card/query/card_query/normalization.rb, line 14
def normalize_clause clause
  clause = clause_to_hash clause
  clause.symbolize_keys!
  clause.each do |key, val|
    next if key.to_sym == :return

    # when return values are relative, they are relative to the name of the
    # card returned, not the context card
    clause[key] = normalize_value val
  end
  clause
end
normalize_string_value(val) click to toggle source
# File lib/card/query/card_query/normalization.rb, line 50
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) click to toggle source
# File lib/card/query/card_query/normalization.rb, line 37
def normalize_value val
  case val
  when String            then normalize_string_value val
  when Array             then normalize_array_value val
  when *OK_VALUE_CLASSES then val
  else raise Error::BadQuery, "Invalid value type: #{val.class} (#{val.inspect})"
  end
end
string_value_from_var(varname) click to toggle source
# File lib/card/query/card_query/normalization.rb, line 63
def string_value_from_var varname
  @vars[varname.to_sym].to_s.strip
end