Module: MotionModel::ArrayModelAdapter::PublicClassMethods
- Defined in:
- lib/motion_model/adapters/array_model_adapter.rb
Instance Method Summary collapse
-
#all ⇒ Object
Returns query result as an array.
- #collection ⇒ Object
-
#delete_all ⇒ Object
Deletes all rows in the model – no hooks are called and deletes are not cascading so this does not affected related data.
-
#find(*args, &block) ⇒ Object
(also: #where)
Finds row(s) within the data store.
- #insert(object) ⇒ Object
- #length ⇒ Object (also: #count)
- #order(field_name = nil, &block) ⇒ Object
Instance Method Details
#all ⇒ Object
Returns query result as an array
67 68 69 |
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 67 def all collection end |
#collection ⇒ Object
15 16 17 |
# File 'lib/motion_model/adapters/array_model_adapter.rb', line 15 def collection @collection ||= [] end |
#delete_all ⇒ Object
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 |
#length ⇒ Object 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 |