Class: OptionInitializingTemplate
- Inherits:
-
Object
- Object
- OptionInitializingTemplate
- Defined in:
- lib/option_initializer.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
(also: #to_h)
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(base, options, need_validation) ⇒ OptionInitializingTemplate
constructor
A new instance of OptionInitializingTemplate.
- #merge(opts) ⇒ Object
- #new(*args, &block) ⇒ Object
- #validate(hash) ⇒ Object
Constructor Details
#initialize(base, options, need_validation) ⇒ OptionInitializingTemplate
Returns a new instance of OptionInitializingTemplate.
22 23 24 25 26 |
# File 'lib/option_initializer.rb', line 22 def initialize base, , need_validation validate if need_validation @base = base @options = end |
Instance Attribute Details
#options ⇒ Object (readonly) Also known as: to_h
Returns the value of attribute options.
19 20 21 |
# File 'lib/option_initializer.rb', line 19 def @options end |
Instance Method Details
#merge(opts) ⇒ Object
51 52 53 54 |
# File 'lib/option_initializer.rb', line 51 def merge opts validate opts self.class.new @base, @options.merge(opts), false end |
#new(*args, &block) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/option_initializer.rb', line 28 def new *args, &block args = args.dup opts = @options # Convention. Deal with it. if args.last.is_a?(Hash) validate args.last opts = opts.merge(args.last) args.pop else opts = opts.dup end opts.instance_eval do def option_validated? true end end args << opts @base.new(*args, &block) end |
#validate(hash) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/option_initializer.rb', line 56 def validate hash avals, vals = [:ARG_VALIDATORS, :VALIDATORS].map { |s| self.class.const_get(s) } hash.each do |k, v| avals[k] && avals[k].call(v) vals[k] && vals[k].call(v) vals[nil] && vals[nil].call(k, v) end end |