Class: QueryableCollection::Worker

Inherits:
Object
  • Object
show all
Includes:
Adamantium
Defined in:
lib/queryable_collection/worker.rb

Defined Under Namespace

Classes: Query

Instance Method Summary collapse

Constructor Details

#initialize(elements, queryable_attributes) ⇒ Worker

Returns a new instance of Worker.

Raises:



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

def initialize(elements, queryable_attributes)
  @elements = Array(elements)
  raise(Error, 'Queryable attributes must be a non empty list of attributes') unless
    valid_queryable_attributes? queryable_attributes

  @queryable_attributes = queryable_attributes.map(&:to_s)
end

Instance Method Details

#allObject



17
18
19
# File 'lib/queryable_collection/worker.rb', line 17

def all
  @elements
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/queryable_collection/worker.rb', line 33

def empty?
  @elements.empty?
end

#find_by(query) ⇒ Object



29
30
31
# File 'lib/queryable_collection/worker.rb', line 29

def find_by(query)
  where(query).first
end

#firstObject



21
22
23
# File 'lib/queryable_collection/worker.rb', line 21

def first
  @elements.first
end

#to_aObject



37
38
39
# File 'lib/queryable_collection/worker.rb', line 37

def to_a
  @elements.dup
end

#where(query) ⇒ Object



25
26
27
# File 'lib/queryable_collection/worker.rb', line 25

def where(query)
  self.class.new(select(query), @queryable_attributes)
end