Module: Nocode::Util::Optionable
- Included in:
- Step
- Defined in:
- lib/nocode/util/optionable.rb
Overview
Add on a DSL for classes. The DSL allows for a new class-level keyword called ‘option’ which can be used to describe what metadata values are important. Then instances can reference those option’s values using magic _option methods. For example:
class Animal
include Optionable
option :type
attr_writer :options
end
animal = Animal.new animal.options = { ‘type’ => ‘dog’ }
animal.type_option # -> should return ‘dog’
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- OPTION_PREFIX =
'_option'
Class Method Summary collapse
Instance Method Summary collapse
- #method_missing(name, *args, &block) ⇒ Object
- #options ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/nocode/util/optionable.rb', line 49 def method_missing(name, *args, &block) key = option_key(name) if name.to_s.end_with?(OPTION_PREFIX) && self.class..include?(key) [key] else super end end |
Class Method Details
.included(klass) ⇒ Object
20 21 22 |
# File 'lib/nocode/util/optionable.rb', line 20 def self.included(klass) klass.extend(ClassMethods) end |
Instance Method Details
#options ⇒ Object
45 46 47 |
# File 'lib/nocode/util/optionable.rb', line 45 def @options || {} end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
59 60 61 62 63 |
# File 'lib/nocode/util/optionable.rb', line 59 def respond_to_missing?(name, include_private = false) key = option_key(name) (name.to_s.end_with?(OPTION_PREFIX) && self.class..include?(key)) || super end |