lib/appfuel/storage/db/repository.rb in appfuel-0.2.5 vs lib/appfuel/storage/db/repository.rb in appfuel-0.2.6
- old
+ new
@@ -22,41 +22,20 @@
end
build(name: entity.domain_name, storage: db_results, type: :db)
end
+ # when key has no . assume the feature of the repository
+ #
+ #
def db_class(key)
+ unless key.include?('.')
+ key = "features.#{self.class.container_feature_name}.db.#{key}"
+ end
app_container[key]
end
- # Used when the automated query methods don't suite your use case. It
- # is assumed that the method executed will honor the same interfaces as
- # query does
- #
- # @param criteria [SpCore::Criteria]
- # @return [Dataset]
- def execute_criteria(criteria)
- query_method = "#{criteria.exec}_manual_query"
- validate_query_method(query_method)
-
- public_send(query_method, criteria)
- end
-
- # Use the criteria entity's basename as a convention to find a method
- # on the repository that returns the necessary relation (db scope) for
- # which to add conditions that will be used to complete the query.
- #
- #
- # @param criteria [SpCore::Criteria]
- # @return [ActiveRecord::Relation]
- def query_relation(criteria)
- query_method = "#{criteria.domain}_query"
- validate_query_method(query_method)
-
- public_send(query_method)
- end
-
# Handles the treatment of the relation when the recordset is empty
# based on the criteria.
#
# @param criteria [SpCore::Criteria]
# @return [SpCore::Error, SpCore::Domain::EntityNotFound, nil]
@@ -84,129 +63,82 @@
# criteria.
#
# @param criteria [SpCore::Criteria]
# @param relation [mixed]
# @return relation
- def apply_query_conditions(criteria, relation)
- relation = where(criteria, relation)
- relation = order(criteria, relation)
- relation = limit(criteria, relation)
+ 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
- # We have an interface for getting all recordsets separately because
- # this is usually done with no filters or limits.
+ # Determines which query conditions to apply to the relation
#
# @param criteria [SpCore::Criteria]
# @param relation
# @return relation
- def apply_query_all(criteria, relation)
- unless criteria.all?
- fail "This interface can only be used when the criteria :all is used"
+ def handle_query_conditions(criteria, relation, settings)
+ if settings.all?
+ return order(criteria, relation.all)
end
- order(criteria, relation.all)
+ apply_query_conditions(criteria, relation, settings)
end
- # Determines which query conditions to apply to the relation
- #
- # @param criteria [SpCore::Criteria]
- # @param relation
- # @return relation
- def handle_query_conditions(criteria, relation)
- if criteria.all?
- return apply_query_all(criteria, relation)
+ def handle_empty_relation(criteria, relation, settings)
+ unless relation.respond_to?(:blank?)
+ fail "The database relation invalid, does not implement :blank?"
end
- apply_query_conditions(criteria, relation)
- end
+ return nil unless relation.blank?
- # Factory method to create a domain entity
- #
- # @param domain_name [String]
- # @return [SpCore::Domain::EntityCollection]
- def create_entity_collection(domain_name)
- Appfuel::Domain::EntityCollection.new(domain_name)
- end
-
- # Creates a lambda to used with the entity collection
- #
- # @param criteria [SpCore::Criteria]
- # @param relation [Object]
- # @param builder [Object]
- # @return lambda
- def entity_loader(criteria, relation, builder)
- -> { load_collection(criteria, relation, builder) }
- end
-
- # A collection is usually loaded within an entity collection via
- # a lambda. It setups up pagination results and builds an entity
- # foreach record in the list
- #
- # @param criteria [SpCore::Criteria]
- # @param relation [Object]
- # @param builder [Object]
- # @return [Hash]
- def load_collection(criteria, relation, builder)
- data = { items: [] }
- unless criteria.disable_pagination?
- relation = relation.page(criteria.page).per(criteria.per_page)
- data[:pager] = create_pager_result(
- total_pages: relation.total_pages,
- current_page: relation.current_page,
- total_count: relation.total_count,
- page_limit: relation.limit_value,
- page_size: relation.size
- )
+ if criteria.error_on_empty_dataset?
+ return domain_not_found_error(criteria)
end
- relation.each do |db_item|
- data[:items] << builder.call(db_item)
+ if criteria.single?
+ return domain_not_found(criteria)
end
- data
end
- # Create an entity collection and assign the entity loader with
- # the entity builder class.
+ # 1) lookup query id in cache
+ # if found build collection from cached query ids
#
- # @param criteria [SpCore::Criteria]
- # @param relation relation
- # @return SpCore::Domain::EntityCollection
- def build_criteria_entities(criteria, relation)
- builder = create_entity_builder(criteria.domain_name)
- result = handle_empty_relation(criteria, relation)
- # when this has a result it means an empty relation has been
- # handled and ready for return otherwise it was a no op
+ # 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
+ #
+ def build_domains(criteria, relation, settings)
+ result = handle_empty_relation(criteria, relation, settings)
return result if result
- if criteria.single?
- relation_method = criteria.first? ? :first : :last
- return builder.call(relation.send(relation_method))
+
+ 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
- collection = create_entity_collection(criteria.domain_name)
- collection.entity_loader = entity_loader(criteria, relation, builder)
- collection
end
- # Query will use the database model to build a query based on the
- # given criteria. It supports where, order and limit conditions.
- #
- # @param criteria [SpCore::Criteria]
- # @return [SpCore::Domain::Entity, SpCore::Domain::EntityCollection]
- def search(criteria)
- return execute_criteria(criteria) if criteria.exec?
-
- begin
- relation = query_relation(criteria)
- relation = handle_query_conditions(criteria, relation)
- build_criteria_entities(criteria, relation)
- rescue => e
- msg = "query failed for #{criteria.domain}: #{e.class} #{e.message}"
- err = RuntimeError.new(msg)
- err.set_backtrace(e.backtrace)
- raise err
- end
+ def domain_builder(domain_name)
+ key = qualify_container_key(domain_name, 'domain_builders.db')
+ app_container[key]
end
private
def raise_error(err, message)