Module: MotionModel::ArrayModelAdapter::PublicClassMethods

Defined in:
lib/motion_model/adapters/array_model_adapter.rb

Instance Method Summary collapse

Instance Method Details

#allObject

Returns query result as an array



67
68
69
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 67

def all
  collection
end

#collectionObject



15
16
17
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 15

def collection
  @collection ||= []
end

#delete_allObject

Deletes all rows in the model – no hooks are called and deletes are not cascading so this does not affected related data.



31
32
33
34
35
36
37
38
39
40
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 31

def delete_all
  # Do each delete so any on_delete and
  # cascades are called, then empty the
  # collection and compact the array.
  bulk_update do
    collection.each{|item| item.delete}
  end
  @collection = []
  @_next_id = 1
end

#find(*args, &block) ⇒ Object Also known as: where

Finds row(s) within the data store. E.g.,

@post = Post.find(1)  # find a specific row by ID

or…

@posts = Post.find(:author).eq('bob').all


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 49

def find(*args, &block)
  if block_given?
    matches = collection.collect do |item|
      item if yield(item)
    end.compact
    return ArrayFinderQuery.new(matches)
  end

  unless args[0].is_a?(Symbol) || args[0].is_a?(String)
    target_id = args[0].to_i
    return collection.select{|element| element.id == target_id}.first
  end

  ArrayFinderQuery.new(args[0].to_sym, @collection)
end

#insert(object) ⇒ Object



19
20
21
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 19

def insert(object)
  collection << object
end

#lengthObject Also known as: count



23
24
25
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 23

def length
  collection.length
end

#order(field_name = nil, &block) ⇒ Object



71
72
73
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 71

def order(field_name = nil, &block)
  ArrayFinderQuery.new(@collection).order(field_name, &block)
end