Class: Card::Query

Inherits:
Object show all
Defined in:
lib/card/query.rb,
lib/card/query/ref_spec.rb,
lib/card/query/card_spec.rb,
lib/card/query/value_spec.rb

Defined Under Namespace

Classes: CardSpec, RefSpec, Spec, SqlCond, SqlStatement, ValueSpec

Constant Summary collapse

MODIFIERS =
{}
OPERATORS =
%w{ != = =~ < > in ~ }.inject({}) {|h,v| h[v]=nil; h }.merge({
  :eq    => '=',   :gt => '>',    :lt      => '<',
  :match => '~',   :ne => '!=',   'not in' => nil
}.stringify_keys)

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Query

Returns a new instance of Query.



17
18
19
# File 'lib/card/query.rb', line 17

def initialize query
  @card_spec = CardSpec.build query
end

Instance Method Details

#queryObject



21
22
23
# File 'lib/card/query.rb', line 21

def query
  @card_spec.query
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/card/query.rb', line 29

def run
#    puts "~~~~~~~~~~~~~~\nCARD SPEC =\n#{@card_spec.rawspec}\n\n-----\n\nSQL=\n#{sql}"
  rows = ActiveRecord::Base.connection.select_all( sql )
  retrn = query[:return].present? ? query[:return].to_s : 'card'
  case retrn 
  when 'card'
    rows.map do |row|
      card=
        if query[:prepend] || query[:append]
          cardname = [query[:prepend], row['name'], query[:append]].compact.join('+')
          Card.fetch cardname, :new=>{}
        else
          Card[ row['name'] ]
        end
      card.nil? ? Card.find_by_name_and_trash(row['name'],false).repair_key : card
    end
  when 'count'
    rows.first['count'].to_i
  when 'raw'
    rows
  else
    rows.map { |row| row[retrn] }
  end
end

#sqlObject



25
26
27
# File 'lib/card/query.rb', line 25

def sql
  @sql ||= @card_spec.to_sql
end