-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
50 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters