Module: Datamappify::Repository::QueryMethods

Defined in:
lib/datamappify/repository/query_methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



8
9
10
11
12
# File 'lib/datamappify/repository/query_methods.rb', line 8

def self.included(klass)
  klass.class_eval do
    include QueryMethod::Callbacks
  end
end

Instance Method Details

#allArray<Entity>

Returns a collection of all the entities in the repository

Returns:



38
39
40
# File 'lib/datamappify/repository/query_methods.rb', line 38

def all
  QueryMethod::FindMultiple.new(query_options, {}).perform
end

#countInteger

Returns:

  • (Integer)


116
117
118
# File 'lib/datamappify/repository/query_methods.rb', line 116

def count
  QueryMethod::Count.new(query_options).perform
end

#create(entity) ⇒ Entity, false

Parameters:

  • entity (Entity)

    an entity or a collection of entities

Returns:



46
47
48
49
50
# File 'lib/datamappify/repository/query_methods.rb', line 46

def create(entity)
  run_callbacks entity, :save, :create do
    QueryMethod::Create.new(query_options, entity).perform
  end
end

#create!(entity) ⇒ Entity

Parameters:

  • entity (Entity)

    an entity or a collection of entities

Returns:

Raises:



57
58
59
# File 'lib/datamappify/repository/query_methods.rb', line 57

def create!(entity)
  create(entity) || raise(Data::EntityNotSaved)
end

#destroy(entity) ⇒ void, false

Parameters:

Returns:

  • (void, false)


100
101
102
103
104
# File 'lib/datamappify/repository/query_methods.rb', line 100

def destroy(entity)
  run_callbacks entity, :destroy do
    QueryMethod::Destroy.new(query_options, entity).perform
  end
end

#destroy!(entity) ⇒ void

This method returns an undefined value.

Parameters:

Raises:



111
112
113
# File 'lib/datamappify/repository/query_methods.rb', line 111

def destroy!(entity)
  destroy(entity) || raise(Data::EntityNotDestroyed)
end

#exists?(entity) ⇒ Boolean

Does the entity exist already in the repository?

Parameters:

Returns:

  • (Boolean)


19
20
21
# File 'lib/datamappify/repository/query_methods.rb', line 19

def exists?(entity)
  QueryMethod::Exists.new(query_options, entity).perform
end

#find(criteria) ⇒ Entity?

Parameters:

  • criteria (Integer, Hash)

    an entity id or a hash containing criteria

Returns:



27
28
29
30
31
32
33
# File 'lib/datamappify/repository/query_methods.rb', line 27

def find(criteria)
  if criteria.is_a?(Integer)
    QueryMethod::Find.new(query_options, criteria).perform
  else
    QueryMethod::FindMultiple.new(query_options, criteria).perform
  end
end

#query_optionsHash (private)

Some default, required objects passed into each query method

Returns:

  • (Hash)


125
126
127
128
129
130
131
# File 'lib/datamappify/repository/query_methods.rb', line 125

def query_options
  {
    :data_mapper => data_mapper,
    :states      => states,
    :lazy_load?  => lazy_load?
  }
end

#save(entity) ⇒ Entity, false

Parameters:

  • entity (Entity)

    an entity or a collection of entities

Returns:



84
85
86
# File 'lib/datamappify/repository/query_methods.rb', line 84

def save(entity)
  exists?(entity) ? update(entity) : create(entity)
end

#save!(entity) ⇒ Entity

Parameters:

  • entity (Entity)

    an entity or a collection of entities

Returns:

Raises:



93
94
95
# File 'lib/datamappify/repository/query_methods.rb', line 93

def save!(entity)
  exists?(entity) ? update!(entity) : create!(entity)
end

#update(entity) ⇒ Entity, false

Parameters:

  • entity (Entity)

    an entity or a collection of entities

Returns:



65
66
67
68
69
# File 'lib/datamappify/repository/query_methods.rb', line 65

def update(entity)
  run_callbacks entity, :save, :update do
    QueryMethod::Update.new(query_options, entity).perform
  end
end

#update!(entity) ⇒ Entity

Parameters:

  • entity (Entity)

    an entity or a collection of entities

Returns:

Raises:



76
77
78
# File 'lib/datamappify/repository/query_methods.rb', line 76

def update!(entity)
  update(entity) || raise(Data::EntityNotSaved)
end