Class: Appfuel::WebApi::Repository

Inherits:
Repository::Base show all
Defined in:
lib/appfuel/storage/web_api/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_mapper, #create_settings, #criteria?, #execute_query_method, #exists?, #find_entity_builder, inherited, #mapper, #query, #query_setup, #to_entity, #to_storage

Methods included from Application::AppContainer

#app_container, included, #qualify_container_key

Instance Attribute Details

#response_handlerObject (readonly)

Returns the value of attribute response_handler.



10
11
12
# File 'lib/appfuel/storage/web_api/repository.rb', line 10

def response_handler
  @response_handler
end

Class Method Details

.container_class_typeObject



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

def container_class_type
  "#{super}.web_api"
end

Instance Method Details

#api_class(key) ⇒ Object

when key has no . assume the feature of the repository



28
29
30
31
32
33
# File 'lib/appfuel/storage/web_api/repository.rb', line 28

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

#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



66
67
68
# File 'lib/appfuel/storage/web_api/repository.rb', line 66

def apply_query_conditions(criteria, relation, _settings)

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


113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/appfuel/storage/web_api/repository.rb', line 113

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



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/appfuel/storage/web_api/repository.rb', line 12

def create(entity, exclude = [])
=begin
  data = to_storage(entity, exclude: ['id'])
  results = []
  data.each do |api_class_key, mapped|
    api_model = api_class(api_class_key)
    results << api_model.create(mapped)
  end

  build(name: entity.domain_name, storage: db_results, type: :web_api)
=end
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



56
57
58
# File 'lib/appfuel/storage/web_api/repository.rb', line 56

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

#domain_builder(domain_name) ⇒ Object



128
129
130
131
# File 'lib/appfuel/storage/web_api/repository.rb', line 128

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)


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/appfuel/storage/web_api/repository.rb', line 40

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



75
76
77
78
79
80
81
# File 'lib/appfuel/storage/web_api/repository.rb', line 75

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

  apply_query_conditions(criteria, relation, settings)
end