Class: KDoc::Settings
- Inherits:
-
Object
- Object
- KDoc::Settings
- Includes:
- KLog::Logging
- Defined in:
- lib/k_doc/settings.rb
Overview
Builds up key/value settings from the block
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#decorators ⇒ Object
readonly
Returns the value of attribute decorators.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#add_getter_or_param_method(name) ⇒ Object
Handles Getter method and method with single parameter object.my_name object.my_name(‘david’).
-
#add_setter_method(name) ⇒ Object
Handles Setter method object.my_name = ‘david’.
- #context ⇒ Object
- #debug ⇒ Object
- #fire_eval ⇒ Object
- #get_value(name) ⇒ Object
-
#initialize(parent, data, key = nil, **opts, &block) ⇒ Settings
constructor
A new instance of Settings.
-
#internal_data ⇒ Object
Return these settings which are attached to a data container using :key internal_data is a bad name, but it is unlikely to interfere with any setting names Maybe I rename this to raw_data.
-
#method_missing(name, *args, &_block) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #respond_to_missing?(name, *_args, &_block) ⇒ Boolean
Constructor Details
#initialize(parent, data, key = nil, **opts, &block) ⇒ Settings
Returns a new instance of Settings.
14 15 16 17 18 19 20 21 |
# File 'lib/k_doc/settings.rb', line 14 def initialize(parent, data, key = nil, **opts, &block) @parent = parent @data = data @key = (key || FakeOpinion.new.default_settings_key).to_s @data[@key] = {} @decorators = build_decorators(opts) # TODO: add tests for decorators @block = block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &_block) ⇒ Object
rubocop:disable Metrics/AbcSize
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/k_doc/settings.rb', line 57 def method_missing(name, *args, &_block) # puts "method_missing: #{name}" # puts "args.length : #{args.length}" if name != :type && !@parent.nil? && @parent.respond_to?(name) puts "Settings.method_missing - NAME: #{name}" return @parent.public_send(name, *args, &block) end raise KDoc::Error, 'Multiple setting values is not supported' if args.length > 1 add_getter_or_param_method(name) add_setter_method(name) send(name, args[0]) if args.length == 1 # name.end_with?('=') super unless self.class.method_defined?(name) end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
12 13 14 |
# File 'lib/k_doc/settings.rb', line 12 def block @block end |
#decorators ⇒ Object (readonly)
Returns the value of attribute decorators.
11 12 13 |
# File 'lib/k_doc/settings.rb', line 11 def decorators @decorators end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
10 11 12 |
# File 'lib/k_doc/settings.rb', line 10 def key @key end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
9 10 11 |
# File 'lib/k_doc/settings.rb', line 9 def parent @parent end |
Instance Method Details
#add_getter_or_param_method(name) ⇒ Object
Handles Getter method and method with single parameter object.my_name object.my_name(‘david’)
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/k_doc/settings.rb', line 79 def add_getter_or_param_method(name) # log.progress(1, 'add_getter_or_param_method') self.class.class_eval do # log.progress(2, 'add_getter_or_param_method') name = name.to_s.gsub(/=$/, '') # log.progress(3, 'add_getter_or_param_method') define_method(name) do |*args| # log.progress(4, 'add_getter_or_param_method') # log.kv 'add_getter_or_param_method', name raise KDoc::Error, 'Multiple setting values is not supported' if args.length > 1 if args.length.zero? get_value(name) else send("#{name}=", args[0]) end end end end |
#add_setter_method(name) ⇒ Object
Handles Setter method object.my_name = ‘david’
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/k_doc/settings.rb', line 101 def add_setter_method(name) # log.progress(1, 'add_setter_method') self.class.class_eval do # log.progress(2, 'add_setter_method') name = name.to_s.gsub(/=$/, '') # log.progress(3, 'add_setter_method') # log.kv 'add_setter_method', name define_method("#{name}=") do |value| # log.progress(4, 'add_setter_method') # log.kv 'value', value internal_data[name.to_s] = value end end end |
#context ⇒ Object
37 38 39 |
# File 'lib/k_doc/settings.rb', line 37 def context parent.context end |
#debug ⇒ Object
120 121 122 |
# File 'lib/k_doc/settings.rb', line 120 def debug puts JSON.pretty_generate(internal_data) end |
#fire_eval ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/k_doc/settings.rb', line 23 def fire_eval return unless block instance_eval(&block) run_decorators # rubocop:disable Style/RescueStandardError rescue => e # rubocop:enable Style/RescueStandardError log.error("Standard error while running settings for key: #{@key}") log.error(e.) raise end |
#get_value(name) ⇒ Object
116 117 118 |
# File 'lib/k_doc/settings.rb', line 116 def get_value(name) internal_data[name.to_s] end |
#internal_data ⇒ Object
Return these settings which are attached to a data container using :key internal_data is a bad name, but it is unlikely to interfere with any setting names Maybe I rename this to raw_data
44 45 46 |
# File 'lib/k_doc/settings.rb', line 44 def internal_data @data[@key] end |
#respond_to_missing?(name, *_args, &_block) ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'lib/k_doc/settings.rb', line 48 def respond_to_missing?(name, *_args, &_block) # puts 'respond_to_missing?' # puts "respond_to_missing: #{name}" n = name.to_s n = n[0..-2] if n.end_with?('=') internal_data.key?(n.to_s) || (!@parent.nil? && @parent.respond_to?(name, true)) || super end |