Module: Card::Query::CardQuery::Run

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

Overview

run CQL queries

Instance Method Summary collapse

Instance Method Details

#alter_result(name) ⇒ Object



90
91
92
93
# File 'lib/card/query/card_query/run.rb', line 90

def alter_result name
  name_parts = [statement[:prepend], name, statement[:append]].compact
  Card::Name[name_parts]
end

#alter_results?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/card/query/card_query/run.rb', line 99

def alter_results?
  statement[:prepend] || statement[:append]
end

#card_result(record, _field) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/card/query/card_query/run.rb', line 60

def card_result record, _field
  if alter_results?
    Card.fetch alter_result(record["name"]), new: {}
  else
    fetch_or_instantiate record
  end
end

#count_result(results, _field) ⇒ Object



40
41
42
# File 'lib/card/query/card_query/run.rb', line 40

def count_result results, _field
  results.first["count"].to_i
end

#default_result(record, field) ⇒ Object



44
45
46
# File 'lib/card/query/card_query/run.rb', line 44

def default_result record, field
  record[field]
end

#fetch_or_instantiate(record) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/card/query/card_query/run.rb', line 68

def fetch_or_instantiate record
  card = Card.retrieve_from_cache record["key"]
  unless card
    card = Card.instantiate record
    Card.write_to_cache card
  end
  card.include_set_modules
  card
end

#id_result(record, field) ⇒ Object



48
49
50
# File 'lib/card/query/card_query/run.rb', line 48

def id_result record, field
  record[field].to_i
end

#large_list(length) ⇒ Object



27
28
29
# File 'lib/card/query/card_query/run.rb', line 27

def large_list length
  Rails.logger.info "#{length} records returned by #{@statement}"
end

#name_result(record, pattern) ⇒ Object



56
57
58
# File 'lib/card/query/card_query/run.rb', line 56

def name_result record, pattern
  process_name record["name"], pattern
end

#process_name(name, pattern) ⇒ Object



83
84
85
86
87
88
# File 'lib/card/query/card_query/run.rb', line 83

def process_name name, pattern
  name = pattern.to_name.absolute(name) if pattern =~ /_\w+/
  return name unless alter_results?

  alter_result name
end

#raw_result(record, _field) ⇒ Object



52
53
54
# File 'lib/card/query/card_query/run.rb', line 52

def raw_result record, _field
  record
end

#result_method(retrn) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/card/query/card_query/run.rb', line 31

def result_method retrn
  case
  when respond_to?(:"#{retrn}_result") then :"#{retrn}_result"
  when (retrn =~ /id$/)                then :id_result
  when (retrn =~ /_\w+/)               then :name_result
  else                                      :default_result
  end
end

#return_list(sql_results, retrn) ⇒ Array

Returns:

  • (Array)


20
21
22
23
24
25
# File 'lib/card/query/card_query/run.rb', line 20

def return_list sql_results, retrn
  large_list sql_results.length if sql_results.length > 1000
  sql_results.map do |record|
    return_simple record, retrn
  end
end

#return_simple(sql_result, retrn) ⇒ (not an Array)

Returns:

  • ((not an Array))


15
16
17
# File 'lib/card/query/card_query/run.rb', line 15

def return_simple sql_result, retrn
  send result_method(retrn), sql_result, retrn
end

#runArray

run the current query

Returns:

  • (Array)

    of card objects by default



8
9
10
11
12
# File 'lib/card/query/card_query/run.rb', line 8

def run
  retrn = statement[:return].present? ? statement[:return].to_s : "card"
  return_method = :"return_#{simple_result?(retrn) ? :simple : :list}"
  send return_method, run_sql, retrn
end

#run_sqlObject



78
79
80
81
# File 'lib/card/query/card_query/run.rb', line 78

def run_sql
  # puts "\nstatement = #{@statement}"
  ActiveRecord::Base.connection.select_all(sql)
end

#simple_result?(retrn) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/card/query/card_query/run.rb', line 95

def simple_result? retrn
  retrn == "count"
end