Sha256: 1fe3d1231bbe55aa0089b52f0451654cd21e41f7b696928522e3d7e6b792ecb2
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
# frozen_string_literal: true module Upgrow # Base class for Repositories. It offers a basic API for the state all # Repositories should have, as well as the logic on how to materialize data # into Models. class BasicRepository attr_reader :base, :model_class # Sets the Basic Repositorie's state. # # @param base [Object] the base object to be used internally to retrieve the # persisted data. For example, a base class in which queries can be # performed for a relational database adapter. Defaults to `nil`. # # @param model_class [Class] the Model class to be used to map and return # the materialized data as instances of the domain. Defaults to a constant # derived from the Repository class' name. For example, a `UserRepository` # will have its default Model class set to `User`. def initialize(base: default_base, model_class: default_model_class) @base = base @model_class = model_class end # Represents the raw Hash of data attributes as a Model instance from the # Repositorie's Model class. # # @param attributes [Hash<Symbol, Object>] the list of attributes the Model # will have. # # @return [Model] the Model instance populated with the given attributes. def to_model(attributes = {}) model_class.new(**attributes.transform_keys(&:to_sym)) end private def default_base; end def default_model_class model_class_name = self.class.name[/\A(.+)Repository\z/, 1] Object.const_get(model_class_name) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
upgrow-0.0.2 | lib/upgrow/basic_repository.rb |