Class: Core::Extension::Dependency
- Inherits:
-
Object
- Object
- Core::Extension::Dependency
- Defined in:
- lib/core/extension/dependency.rb
Overview
[public] Defines a dependency that can be applied to objects.
flags - Changes how the dependencies are applied. Possible values include:
* `:definition` - Extends the definition of the including object.
* `:implementation` - Extends the implementation of the including object.
Dependencies are applied with the `:implementation` flag by default.
dependency - The dependency to be applied to objects, following the rules defined by flags.
prepend - If true, methods will be prepended.
Constant Summary collapse
- ALLOWED_FLAGS =
%i[definition implementation].freeze
- ALLOWED_FLAGS_STRING =
ALLOWED_FLAGS.map { |allowed_flag| "`#{allowed_flag.inspect}'" }.join(", ").freeze
Instance Method Summary collapse
-
#apply_extend(object) ⇒ Object
[public] Apply the defined dependencies to an object via extend.
-
#apply_include(object) ⇒ Object
[public] Apply the defined dependencies to an object via include.
-
#initialize(*flags, dependency:, prepend: false) ⇒ Dependency
constructor
A new instance of Dependency.
Constructor Details
#initialize(*flags, dependency:, prepend: false) ⇒ Dependency
Returns a new instance of Dependency.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/core/extension/dependency.rb', line 25 def initialize(*flags, dependency:, prepend: false) flags = flags.map(&:to_sym) enforce_allowed_flags(flags) @flags = flags @dependency = dependency @prepend = prepend @definition = @flags.include?(:definition) @implementation = @flags.include?(:implementation) || (!definition? && !prepend?) end |
Instance Method Details
#apply_extend(object) ⇒ Object
[public] Apply the defined dependencies to an object via extend.
38 39 40 41 42 43 44 45 46 |
# File 'lib/core/extension/dependency.rb', line 38 def apply_extend(object) return if object.singleton_class.ancestors.include?(@dependency) if prepend? object.singleton_class.prepend(@dependency) if implementation? || definition? elsif definition? || implementation? object.extend(@dependency) end end |
#apply_include(object) ⇒ Object
[public] Apply the defined dependencies to an object via include.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/core/extension/dependency.rb', line 50 def apply_include(object) return if object.ancestors.include?(@dependency) if prepend? object.prepend(@dependency) if implementation? object.singleton_class.prepend(@dependency) if definition? else object.include(@dependency) if implementation? object.extend(@dependency) if definition? end end |