Class: Dply::BaseConfig
- Inherits:
-
Object
- Object
- Dply::BaseConfig
- Defined in:
- lib/dply/base_config.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#opts_struct ⇒ Object
readonly
Returns the value of attribute opts_struct.
Class Method Summary collapse
- .build(path = nil, &block) ⇒ Object
- .define_opt(opts) ⇒ Object
- .define_opt_setter(opt) ⇒ Object
- .define_opts(&block) ⇒ Object
- .new_struct_klass(opts) ⇒ Object
- .struct_klass ⇒ Object
Instance Method Summary collapse
- #default_config ⇒ Object
-
#initialize ⇒ BaseConfig
constructor
A new instance of BaseConfig.
- #read(path, optional: false) ⇒ Object
- #set(opt, value, &block) ⇒ Object
Constructor Details
permalink #initialize ⇒ BaseConfig
Returns a new instance of BaseConfig.
84 85 86 87 |
# File 'lib/dply/base_config.rb', line 84 def initialize @opts_struct = self.class.struct_klass.new default_config end |
Instance Attribute Details
permalink #opts_struct ⇒ Object (readonly)
Returns the value of attribute opts_struct.
82 83 84 |
# File 'lib/dply/base_config.rb', line 82 def opts_struct @opts_struct end |
Class Method Details
permalink .build(path = nil, &block) ⇒ Object
[View source]
73 74 75 76 77 78 |
# File 'lib/dply/base_config.rb', line 73 def self.build(path = nil, &block) i = new i.read(path) if path i.instance_eval(&block) if block_given? i.opts_struct end |
permalink .define_opt(opts) ⇒ Object
[View source]
15 16 17 18 19 |
# File 'lib/dply/base_config.rb', line 15 def self.define_opt(opts) opts.define_singleton_method(:opt) do |name, type: Object| self[name] = type end end |
permalink .define_opt_setter(opt) ⇒ Object
[View source]
63 64 65 66 67 |
# File 'lib/dply/base_config.rb', line 63 def self.define_opt_setter(opt) define_method(opt) do |value = nil, &block| set opt, value, &block end end |
permalink .define_opts(&block) ⇒ Object
[View source]
5 6 7 8 9 10 11 12 13 |
# File 'lib/dply/base_config.rb', line 5 def self.define_opts(&block) opts = {} define_opt(opts) opts.instance_eval(&block) @struct_klass = new_struct_klass(opts) opts.each do |opt, _| define_opt_setter(opt) end end |
permalink .new_struct_klass(opts) ⇒ Object
[View source]
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dply/base_config.rb', line 21 def self.new_struct_klass(opts) Class.new do def initialize @proc_map = {} end def set_proc(opt, pr) @proc_map[opt] = pr ivar = "@#{opt}" remove_instance_variable(ivar) if instance_variable_defined?(ivar) end opts.each do |opt, type| define_method "#{opt}=" do |value| assert_type(opt, value, type) instance_variable_set("@#{opt}", value) end define_method opt do if instance_variable_defined?("@#{opt}") instance_variable_get("@#{opt}") elsif @proc_map.key?(opt) value = instance_eval(&@proc_map.fetch(opt)) public_send("#{opt}=", value) else instance_variable_set("@#{opt}", nil) end end end opts = nil private def assert_type(opt, value, type) if not value.is_a?(type) raise Error, "opt(#{opt}) has value '#{value}' of type '#{value.class}' (Expected: '#{type}')" end end end end |
permalink .struct_klass ⇒ Object
[View source]
69 70 71 |
# File 'lib/dply/base_config.rb', line 69 def self.struct_klass @struct_klass end |
Instance Method Details
permalink #default_config ⇒ Object
[View source]
89 90 |
# File 'lib/dply/base_config.rb', line 89 def default_config end |
permalink #read(path, optional: false) ⇒ Object
[View source]
100 101 102 103 104 105 106 107 108 |
# File 'lib/dply/base_config.rb', line 100 def read(path, optional: false) if not File.readable?(path) return if optional raise Error, "config #{path} not readable" end instance_eval(File.read(path), path) rescue NoMethodError => e raise Error, "invalid option used in config: #{e.name}" end |
permalink #set(opt, value, &block) ⇒ Object
[View source]
92 93 94 95 96 97 98 |
# File 'lib/dply/base_config.rb', line 92 def set(opt, value, &block) if block @opts_struct.set_proc(opt, block) else @opts_struct.public_send("#{opt}=", value) end end |