Class: Heliodor::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/heliodor/query.rb,
lib/heliodor/query_internal.rb

Overview

Query which will be executed when #finish is called

Instance Attribute Summary collapse

Query Language collapse

Instance Method Summary collapse

Constructor Details

#initialize(db, table, dat) ⇒ Query

Initializer



9
10
11
12
13
14
15
# File 'lib/heliodor/query.rb', line 9

def initialize(db, table, dat)
  @db      = db
  @table   = table
  @full    = dat
  @dat     = @full[@table]
  @actions = []
end

Instance Attribute Details

#actionsArray<Hash>

Array of all actions

Returns:

  • (Array<Hash>)

    the current value of actions



5
6
7
# File 'lib/heliodor/query.rb', line 5

def actions
  @actions
end

#dbHeliodor::DB

Query’s database

Returns:



5
6
7
# File 'lib/heliodor/query.rb', line 5

def db
  @db
end

#tableString

Name of current table

Returns:

  • (String)

    the current value of table



5
6
7
# File 'lib/heliodor/query.rb', line 5

def table
  @table
end

Instance Method Details

#createself

Creates table with current name. If it already exists - truncates

Returns:

  • (self)


32
33
34
35
36
37
# File 'lib/heliodor/query.rb', line 32

def create
  actions << {
    'type' => 'create'
  }
  self
end

#delete(dat = {}) ⇒ self

Deletes matching items

Returns:

  • (self)


130
131
132
133
134
135
136
# File 'lib/heliodor/query.rb', line 130

def delete(dat = {})
  actions << {
    'type' => 'delete',
    'dat'  => dat
  }
  self
end

#ensureself

If there’s no table with current name, creates it

Returns:

  • (self)


41
42
43
44
45
46
# File 'lib/heliodor/query.rb', line 41

def ensure
  actions << {
    'type' => 'ensure'
  }
  self
end

#filter {|i| ... } ⇒ self

Filters all items in current table using given block

Yields:

  • (i)

    Is executed for each item in table. Drops values when this block returns false value

Returns:

  • (self)


52
53
54
55
56
57
58
# File 'lib/heliodor/query.rb', line 52

def filter(&block)
  actions << {
    'type'  => 'filter',
    'block' => block
  }
  self
end

#finishArray

Ends query and processes it

Returns:

  • (Array)

    Data in table



140
141
142
143
# File 'lib/heliodor/query.rb', line 140

def finish
  _process
  @dat
end

#insert(*dat) ⇒ self

Insert given data into table

Parameters:

  • dat (Any)

Returns:

  • (self)


22
23
24
25
26
27
28
# File 'lib/heliodor/query.rb', line 22

def insert(*dat)
  actions << {
    'type' => 'insert',
    'data' => dat
  }
  self
end

#inspectObject

Inspect method



147
148
149
150
# File 'lib/heliodor/query.rb', line 147

def inspect
  %(#<Heliodor::Query:#{object_id.to_s(16)} @table='#{@table}' @db=#{@db}) +
    %( @actions.length=#{@actions.length}>)
end

#map {|i| ... } ⇒ self

Edits all items in current table

Examples:

Adding key to each hash in table

db.query('table_name').map do |i|
  i[:keyname] = nil
end.write.finish

Map-reduce method of getting table length

db.query('table_name').map{1}.reduce(&:+).finish # => length

Yields:

  • (i)

    Return updated value to update stuff

Returns:

  • (self)


69
70
71
72
73
74
75
# File 'lib/heliodor/query.rb', line 69

def map(&block)
  actions << {
    'type'  => 'map',
    'block' => block
  }
  self
end

#reduce(&block) ⇒ self

Reduces table to single element using given block

Returns:

  • (self)


79
80
81
82
83
84
85
# File 'lib/heliodor/query.rb', line 79

def reduce(&block)
  actions << {
    'type'  => 'reduce',
    'block' => block
  }
  self
end

#rename(to) ⇒ self

Renames current table

Parameters:

  • to (String)

Returns:

  • (self)


90
91
92
93
94
95
96
# File 'lib/heliodor/query.rb', line 90

def rename(to)
  actions << {
    'type' => 'rename',
    'to'   => to
  }
  self
end

#select(dat = {}) ⇒ self

Selects all values which match given hash

Returns:

  • (self)


109
110
111
112
113
114
115
# File 'lib/heliodor/query.rb', line 109

def select(dat = {})
  actions << {
    'type' => 'select',
    'dat'  => dat
  }
  self
end

#update(dat1 = {}, dat2 = {}) ⇒ self

Updates all items which match first hash by merging them with second hash

Returns:

  • (self)


119
120
121
122
123
124
125
126
# File 'lib/heliodor/query.rb', line 119

def update(dat1 = {}, dat2 = {})
  actions << {
    'type' => 'update',
    'dat1' => dat1,
    'dat2' => dat2
  }
  self
end

#writeself

Writes changes to database

Returns:

  • (self)


100
101
102
103
104
105
# File 'lib/heliodor/query.rb', line 100

def write
  actions << {
    'type' => 'write'
  }
  self
end