Module: Dry::Core::ClassAttributes
- Includes:
- Constants
- Defined in:
- lib/dry/core/class_attributes.rb
Overview
Internal support module for class-level settings
Constant Summary
Constants included from Constants
Dry::Core::Constants::EMPTY_ARRAY, Dry::Core::Constants::EMPTY_HASH, Dry::Core::Constants::EMPTY_OPTS, Dry::Core::Constants::EMPTY_SET, Dry::Core::Constants::EMPTY_STRING, Dry::Core::Constants::IDENTITY, Dry::Core::Constants::Undefined
Instance Method Summary collapse
-
#defines(*args, type: ::Object, coerce: IDENTITY) ⇒ Object
Specify what attributes a class will use.
Methods included from Constants
Instance Method Details
#defines(*args, type: ::Object, coerce: IDENTITY) ⇒ Object
Specify what attributes a class will use
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/dry/core/class_attributes.rb', line 68 def defines(*args, type: ::Object, coerce: IDENTITY) # rubocop:disable Metrics/PerceivedComplexity unless coerce.respond_to?(:call) raise ::ArgumentError, "Non-callable coerce option: #{coerce.inspect}" end mod = ::Module.new do args.each do |name| ivar = :"@#{name}" define_method(name) do |value = Undefined| if Undefined.equal?(value) if instance_variable_defined?(ivar) instance_variable_get(ivar) else nil end elsif type === value # rubocop:disable Style/CaseEquality instance_variable_set(ivar, coerce.call(value)) else raise InvalidClassAttributeValueError.new(name, value) end end end define_method(:inherited) do |klass| args.each { |name| klass.send(name, send(name)) } super(klass) end end extend(mod) end |