Class: Appfuel::Db::Repository

Inherits:
Repository::Base show all
Defined in:
lib/appfuel/storage/db/repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Repository::Base

#build, #build_criteria, #build_default_entity, cache, #create_settings, #criteria?, #execute_query_method, #exists?, #find_entity_builder, #generate_uuid, inherited, #mapper, #query, #query_setup, #timestamp, #to_entity, #to_storage, #url_token

Methods included from Application::AppContainer

#app_container, #feature_name, included, #qualify_container_key

Instance Attribute Details

#response_handlerObject (readonly)

Returns the value of attribute response_handler.



14
15
16
# File 'lib/appfuel/storage/db/repository.rb', line 14

def response_handler
  @response_handler
end

Class Method Details

.container_class_typeObject



5
6
7
# File 'lib/appfuel/storage/db/repository.rb', line 5

def container_class_type
  "#{super}.db"
end

.create_mapper(maps = nil) ⇒ Object



9
10
11
# File 'lib/appfuel/storage/db/repository.rb', line 9

def create_mapper(maps = nil)
  Mapper.new(container_root_name, maps)
end

Instance Method Details

#apply_query_conditions(criteria, relation, _settings) ⇒ Object

Apply where, order and limit clauses to the relation based on the criteria.

Parameters:

  • criteria (SpCore::Criteria)
  • relation (mixed)

Returns:

  • relation



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/appfuel/storage/db/repository.rb', line 68

def apply_query_conditions(criteria, relation, _settings)
  ap 'i am applying some query condititions'
  relation = mapper.where(criteria, relation)
  ap relation
  ap 'some where conditions'
  relation = mapper.order(criteria, relation)
  ap 'some order condition'
  relation = mapper.limit(criteria, relation)
  ap 'some limit condition'
  relation
end

#build_domains(criteria, relation, settings) ⇒ Object

1) lookup query id in cache

if found build collection from cached query ids

2) query id not found in cache

a) assign query id from criteria
b) find the domain builder for that criteria
c) loop through each item in the database relation
d) build an domain from each record in the relation
e) create cache id from the domain
f) record cache id into a list that represents query
g) assign domain into the cache with its cache id
    id is in the cache the its updated *represents a miss
h) assign the query list into the cache with its query id


123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/appfuel/storage/db/repository.rb', line 123

def build_domains(criteria, relation, settings)
  result  = handle_empty_relation(criteria, relation, settings)
  return result if result


  if settings.single?
    method   = settings.first? ? :first : :last
    db_model = relation.send(method)
    builder  = domain_builder(criteria.domain_name)
    domain   = builder.call(db_model, criteria)
    ap domain
  end

end

#create(entity, exclude = []) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/appfuel/storage/db/repository.rb', line 16

def create(entity, exclude = [])
  data = to_storage(entity, exclude: ['id'])
  db_results = []
  data.each do |db_class_key, mapped|
    db_model  = db_class(db_class_key)
    db_results << db_model.create(mapped)
  end

  build(name: entity.domain_name, storage: db_results, type: :db)
end

#create_entity_not_found(criteria) ⇒ Object

Null object used when you can not find a given entity

Parameters:

  • criteria (SpCore::Criteria)

Returns:

  • SpCore::Domain::EntityNotFound



58
59
60
# File 'lib/appfuel/storage/db/repository.rb', line 58

def create_entity_not_found(criteria)
  Appfuel::Domain::EntityNotFound.new(entity_name: criteria.domain_name)
end

#db_class(key) ⇒ Object

when key has no . assume the feature of the repository



30
31
32
33
34
35
# File 'lib/appfuel/storage/db/repository.rb', line 30

def db_class(key)
  unless key.include?('.')
    key = "features.#{self.class.container_feature_name}.db.#{key}"
  end
  app_container[key]
end

#domain_builder(domain_name) ⇒ Object



138
139
140
141
# File 'lib/appfuel/storage/db/repository.rb', line 138

def domain_builder(domain_name)
  key = qualify_container_key(domain_name, 'domain_builders.db')
  app_container[key]
end

#handle_empty_relation(criteria, relation, settings) ⇒ SpCore::Error, ...

Handles the treatment of the relation when the recordset is empty based on the criteria.

Parameters:

  • criteria (SpCore::Criteria)

Returns:

  • (SpCore::Error, SpCore::Domain::EntityNotFound, nil)


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/appfuel/storage/db/repository.rb', line 42

def handle_empty_relation(criteria, relation)
  return nil unless relation.blank?

  if criteria.error_on_empty_dataset?
    return error(criteria.domain => ["#{criteria.domain} not found"])
  end

  if criteria.single?
    return create_entity_not_found(criteria)
  end
end

#handle_query_conditions(criteria, relation, settings) ⇒ Object

Determines which query conditions to apply to the relation

Parameters:

  • criteria (SpCore::Criteria)
  • relation

Returns:

  • relation



85
86
87
88
89
90
91
# File 'lib/appfuel/storage/db/repository.rb', line 85

def handle_query_conditions(criteria, relation, settings)
  if settings.all?
    return order(criteria, relation.all)
  end

  apply_query_conditions(criteria, relation, settings)
end