Module: Dieses::Support::ClassAttribute
- Included in:
- Application::Paper::Instance, Enum
- Defined in:
- lib/dieses/support/class.rb
Overview
Stolen and improved from dry-rb/dry-core
Instance Method Summary collapse
-
#define(name, default: Undefined, behave: Undefined, inherit: true, instance_reader: false) ⇒ Object
rubocop:disable Metrics/MethodLength,Layout/LineLength,Lint/RedundantCopDisableDirective.
-
#defines(*names, behave: Undefined) ⇒ Object
rubocop:enable Metrics/MethodLength,Layout/LineLength,Lint/RedundantCopDisableDirective.
Instance Method Details
#define(name, default: Undefined, behave: Undefined, inherit: true, instance_reader: false) ⇒ Object
rubocop:disable Metrics/MethodLength,Layout/LineLength,Lint/RedundantCopDisableDirective
61 62 63 64 65 66 67 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 |
# File 'lib/dieses/support/class.rb', line 61 def define(name, default: Undefined, behave: Undefined, inherit: true, instance_reader: false) ivar = :"@#{name}" behave = Value.behave(behave, default) instance_variable_set(ivar, default.dup) mod = ::Module.new do define_method(name) do |new_value = Undefined| if Undefined.equal?(new_value) return instance_variable_defined?(ivar) ? instance_variable_get(ivar) : nil end instance_variable_set( ivar, behave.( instance_variable_defined?(ivar) ? instance_variable_get(ivar) : instance_variable_set(ivar, default.dup), new_value ) ) end define_method(:inherited) do |klass| klass.send(name, (inherit ? send(name) : default).dup) super(klass) end end extend(mod) define_method(name) { self.class.send(name) } if instance_reader end |
#defines(*names, behave: Undefined) ⇒ Object
rubocop:enable Metrics/MethodLength,Layout/LineLength,Lint/RedundantCopDisableDirective
95 96 97 |
# File 'lib/dieses/support/class.rb', line 95 def defines(*names, behave: Undefined) names.each { |name| define(name, behave: behave) } end |