Class: OptionInitializingTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/option_initializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options, need_validation
  validate options if need_validation
  @base    = base
  @options = options
end

Instance Attribute Details

#optionsObject (readonly) Also known as: to_h

Returns the value of attribute options.



19
20
21
# File 'lib/option_initializer.rb', line 19

def options
  @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