module Datamappify::Repository::QueryMethods

Public Class Methods

included(klass) click to toggle source
# File lib/datamappify/repository/query_methods.rb, line 8
def self.included(klass)
  klass.class_eval do
    include QueryMethod::Callbacks
  end
end

Public Instance Methods

all() click to toggle source

Returns a collection of all the entities in the repository

@return [Array<Entity>]

# File lib/datamappify/repository/query_methods.rb, line 42
def all
  QueryMethod::FindMultiple.new(query_options, {}).perform
end
count() click to toggle source

@return [Integer]

# File lib/datamappify/repository/query_methods.rb, line 120
def count
  QueryMethod::Count.new(query_options).perform
end
create(entity) click to toggle source

@param entity [Entity]

an entity or a collection of entities

@return [Entity, false]

# File lib/datamappify/repository/query_methods.rb, line 50
def create(entity)
  run_callbacks entity, :save, :create do
    QueryMethod::Create.new(query_options, entity).perform
  end
end
create!(entity) click to toggle source

@param (see create)

@raise [Data::EntityNotSaved]

@return [Entity]

# File lib/datamappify/repository/query_methods.rb, line 61
def create!(entity)
  create(entity) || raise(Data::EntityNotSaved)
end
destroy(entity) click to toggle source

@param entity [Entity]

@return [void, false]

# File lib/datamappify/repository/query_methods.rb, line 104
def destroy(entity)
  run_callbacks entity, :destroy do
    QueryMethod::Destroy.new(query_options, entity).perform
  end
end
destroy!(entity) click to toggle source

@param (see destroy)

@raise [Data::EntityNotDestroyed]

@return [void]

# File lib/datamappify/repository/query_methods.rb, line 115
def destroy!(entity)
  destroy(entity) || raise(Data::EntityNotDestroyed)
end
exists?(entity) click to toggle source

Does the entity exist already in the repository?

@param entity [Entity]

@return [Boolean]

# File lib/datamappify/repository/query_methods.rb, line 19
def exists?(entity)
  QueryMethod::Exists.new(query_options, entity).perform
end
find(criteria) click to toggle source

@param criteria [Integer, String]

an entity id or a hash containing criteria

@return [Entity, nil]

# File lib/datamappify/repository/query_methods.rb, line 27
def find(criteria)
  QueryMethod::Find.new(query_options, criteria).perform
end
save(entity) click to toggle source

@param entity [Entity]

an entity or a collection of entities

@return [Entity, false]

# File lib/datamappify/repository/query_methods.rb, line 88
def save(entity)
  exists?(entity) ? update(entity) : create(entity)
end
save!(entity) click to toggle source

@param (see save)

@raise [Data::EntityNotSaved]

@return [Entity]

# File lib/datamappify/repository/query_methods.rb, line 97
def save!(entity)
  exists?(entity) ? update!(entity) : create!(entity)
end
update(entity) click to toggle source

@param entity [Entity]

an entity or a collection of entities

@return [Entity, false]

# File lib/datamappify/repository/query_methods.rb, line 69
def update(entity)
  run_callbacks entity, :save, :update do
    QueryMethod::Update.new(query_options, entity).perform
  end
end
update!(entity) click to toggle source

@param (see update)

@raise [Data::EntityNotSaved]

@return [Entity]

# File lib/datamappify/repository/query_methods.rb, line 80
def update!(entity)
  update(entity) || raise(Data::EntityNotSaved)
end
where(criteria) click to toggle source

@param criteria [Hash]

a hash containing criteria

@return [Entity]

# File lib/datamappify/repository/query_methods.rb, line 35
def where(criteria)
  QueryMethod::FindMultiple.new(query_options, criteria).perform
end

Private Instance Methods

query_options() click to toggle source

Some default, required objects passed into each query method

@return [Hash]

# File lib/datamappify/repository/query_methods.rb, line 129
def query_options
  {
    :data_mapper => data_mapper,
    :states      => states,
    :lazy_load?  => lazy_load?
  }
end