Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgz committed Jul 10, 2024
1 parent 180094d commit 122a705
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
1 change: 1 addition & 0 deletions lib/esse/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ class << self
require_relative 'repository/actions'
require_relative 'repository/documents'
require_relative 'repository/object_document_mapper'
require_relative 'repository/lazy_document_attributes'
end
end
49 changes: 49 additions & 0 deletions lib/esse/repository/lazy_document_attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module Esse
# Definition for the lazy document attributes
class Repository
module ClassMethods
def lazy_document_attributes
@lazy_document_attributes ||= {}.freeze
end

def lazy_document_attribute?(attr_name)
lazy_document_attributes.key?(attr_name.to_s)
end

def fetch_lazy_document_attribute(attr_name)
klass, kwargs = lazy_document_attributes.fetch(attr_name.to_s)
klass.new(**kwargs)
rescue KeyError
raise ArgumentError, format('Attribute %<attr>p is not defined as a lazy document attribute', attr: attr_name)
end

def lazy_document_attribute(attr_name, klass = nil, **kwargs, &block)
if lazy_document_attribute?(attr_name)
raise ArgumentError, format('Attribute %<attr>p is already defined as a lazy document attribute', attr: attr_name)
end

@lazy_document_attributes = lazy_document_attributes.dup
if block
klass = Class.new(Esse::DocumentLazyAttribute) do
define_method(:call, &block)
end
@lazy_document_attributes[attr_name.to_s] = [klass, kwargs]
elsif klass.is_a?(Class) && klass <= Esse::DocumentLazyAttribute
@lazy_document_attributes[attr_name.to_s] = [klass, kwargs]
elsif klass.is_a?(Class) && klass.instance_methods.include?(:call)
@lazy_document_attributes[attr_name.to_s] = [klass, kwargs]
elsif klass.nil?
raise ArgumentError, format('A block or a class that responds to `call` is required to define a lazy document attribute')
else
raise ArgumentError, format('%<arg>p is not a valid lazy document attribute. Class should inherit from Esse::DocumentLazyAttribute or respond to `call`', arg: klass)
end
ensure
@lazy_document_attributes&.freeze
end
end

extend ClassMethods
end
end
39 changes: 0 additions & 39 deletions lib/esse/repository/object_document_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,45 +150,6 @@ def documents(**kwargs)
end
end
end

def lazy_document_attributes
@lazy_document_attributes ||= {}.freeze
end

def lazy_document_attribute?(attr_name)
lazy_document_attributes.key?(attr_name.to_s)
end

def fetch_lazy_document_attribute(attr_name)
klass, kwargs = lazy_document_attributes.fetch(attr_name.to_s)
klass.new(**kwargs)
rescue KeyError
raise ArgumentError, format('Attribute %<attr>p is not defined as a lazy document attribute', attr: attr_name)
end

def lazy_document_attribute(attr_name, klass = nil, **kwargs, &block)
if lazy_document_attribute?(attr_name)
raise ArgumentError, format('Attribute %<attr>p is already defined as a lazy document attribute', attr: attr_name)
end

@lazy_document_attributes = lazy_document_attributes.dup
if block
klass = Class.new(Esse::DocumentLazyAttribute) do
define_method(:call, &block)
end
@lazy_document_attributes[attr_name.to_s] = [klass, kwargs]
elsif klass.is_a?(Class) && klass <= Esse::DocumentLazyAttribute
@lazy_document_attributes[attr_name.to_s] = [klass, kwargs]
elsif klass.is_a?(Class) && klass.instance_methods.include?(:call)
@lazy_document_attributes[attr_name.to_s] = [klass, kwargs]
elsif klass.nil?
raise ArgumentError, format('A block or a class that responds to `call` is required to define a lazy document attribute')
else
raise ArgumentError, format('%<arg>p is not a valid lazy document attribute. Class should inherit from Esse::DocumentLazyAttribute or respond to `call`', arg: klass)
end
ensure
@lazy_document_attributes&.freeze
end
end

extend ClassMethods
Expand Down

0 comments on commit 122a705

Please sign in to comment.