Method: Eye::Dsl::PureOpts.create_options_methods

Defined in:
lib/eye/dsl/pure_opts.rb

.create_options_methods(arr, types = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eye/dsl/pure_opts.rb', line 3

def self.create_options_methods(arr, types = nil)
  m = Module.new do
    arr.each do |opt|
      define_method("set_#{opt}") do |arg|
        key = opt.to_sym

        if (disallow_options && disallow_options.include?(key)) || (allow_options && !allow_options.include?(key))
          raise Eye::Dsl::Error, "disallow option #{key} for #{self.class.inspect}"
        end

        if types
          good_type = Array(types).any?{|type| arg.is_a?(type) } || arg.nil?
          raise Eye::Dsl::Error, "bad :#{opt} value #{arg.inspect}, type should be #{types.inspect}" unless good_type
        end

        @config[key] = arg
      end

      define_method("get_#{opt}") do
        @config[ opt.to_sym ]
      end

      define_method(opt) do |*args|
        if args.blank?
          # getter
          send "get_#{opt}"
        else
          send "set_#{opt}", *args
        end
      end

      define_method("#{opt}=") do |arg|
        send opt, arg
      end
    end
  end

  self.send :include, m
end