Class: Mongomery::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, schema, mapped_properties, custom_operators) ⇒ Query



53
54
55
56
57
58
59
# File 'lib/mongomery.rb', line 53

def initialize(table, schema, mapped_properties, custom_operators)
  @table = table
  @schema = schema
  @mapped_properties = mapped_properties
  @custom_operators = custom_operators
  @condition = nil
end

Instance Attribute Details

#custom_operatorsObject (readonly)

Returns the value of attribute custom_operators.



51
52
53
# File 'lib/mongomery.rb', line 51

def custom_operators
  @custom_operators
end

#mapped_propertiesObject (readonly)

Returns the value of attribute mapped_properties.



51
52
53
# File 'lib/mongomery.rb', line 51

def mapped_properties
  @mapped_properties
end

#schemaObject (readonly)

Returns the value of attribute schema.



51
52
53
# File 'lib/mongomery.rb', line 51

def schema
  @schema
end

#tableObject (readonly)

Returns the value of attribute table.



51
52
53
# File 'lib/mongomery.rb', line 51

def table
  @table
end

Instance Method Details

#arelObject



66
67
68
# File 'lib/mongomery.rb', line 66

def arel
  @arel ||= build_arel
end

#countObject



119
120
121
122
123
# File 'lib/mongomery.rb', line 119

def count
  table.project('COUNT(*)').tap do |t|
    t.where(condition) if condition
  end
end

#deleteObject



140
141
142
143
144
145
# File 'lib/mongomery.rb', line 140

def delete
  Arel::DeleteManager.new(table.engine).tap do |manager|
    manager.from(table)
    manager.where(condition) if condition
  end
end

#index(col) ⇒ Object



105
106
107
# File 'lib/mongomery.rb', line 105

def index(col)
  Arel.sql(%Q[CREATE INDEX "#{table.name}_#{col}_idx" ON "#{table.name}" ((#{sql_json_exp(col)}))])
end

#insert(args) ⇒ Object



125
126
127
128
129
130
# File 'lib/mongomery.rb', line 125

def insert(args)
  Arel::InsertManager.new(table.engine).tap do |manager|
    manager.into(table)
    manager.insert(Hash[args.map {|k, v| [table[k], v] }].to_a)
  end
end

#limit(number) ⇒ Object



78
79
80
81
# File 'lib/mongomery.rb', line 78

def limit(number)
  arel.take(number)
  self
end

#skip(number) ⇒ Object



83
84
85
86
# File 'lib/mongomery.rb', line 83

def skip(number)
  arel.skip(number)
  self
end

#sort(params) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mongomery.rb', line 88

def sort(params)
  params.each do |col, val|
    order = val > 0 ? :asc : :desc
    case col.to_s
      when "_id"
        arel.order(table[:id].send(order))
      else
        arel.order(sql_json_path(col).send(order))
    end
  end
  self
end

#sql_json_exp(col) ⇒ Object



101
102
103
# File 'lib/mongomery.rb', line 101

def sql_json_exp(col)
  sql_json_path(col)
end

#to_arelObject



70
71
72
# File 'lib/mongomery.rb', line 70

def to_arel
  arel
end

#to_sqlObject



74
75
76
# File 'lib/mongomery.rb', line 74

def to_sql
  to_arel.to_sql
end

#update(args) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/mongomery.rb', line 132

def update(args)
  Arel::UpdateManager.new(table.engine).tap do |manager|
    manager.table(table)
    manager.set(Hash[args.map {|k, v| [table[k], v] }].to_a)
    manager.where(condition) if condition
  end
end

#where(args) ⇒ Object



61
62
63
64
# File 'lib/mongomery.rb', line 61

def where(args)
  @where = args
  self
end