Class: Dredd::Query

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Query

Returns a new instance of Query.



8
9
10
# File 'lib/dredd/query.rb', line 8

def initialize(text)
  instance_eval(text)
end

Instance Attribute Details

#windowObject (readonly)

Returns the value of attribute window.



6
7
8
# File 'lib/dredd/query.rb', line 6

def window
  @window
end

Instance Method Details

#all(&block) ⇒ Object



12
13
14
15
16
# File 'lib/dredd/query.rb', line 12

def all(&block)
  @window = [:to_a]
  @filter = block
  @aggregate = Dsl::Aggregate.new
end

#execute(collection) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/dredd/query.rb', line 30

def execute(collection)
  slice = collection.send(*@window)
            .map{|k| Hashie::Mash.new(k)}
            .select{|k| @filter ? k.instance_eval(&@filter) : true }
            .map{|k| k.to_hash }
  @aggregate.execute(slice)
end

#first(num, &block) ⇒ Object



18
19
20
21
22
# File 'lib/dredd/query.rb', line 18

def first(num, &block)
  @window = [:first, num]
  @filter = block
  @aggregate = Dsl::Aggregate.new
end

#last(num, &block) ⇒ Object



24
25
26
27
28
# File 'lib/dredd/query.rb', line 24

def last(num, &block)
  @window = [:last, num]
  @filter = block
  @aggregate = Dsl::Aggregate.new
end