Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/scrubyt/utils/ruby_extensions.rb

Instance Method Summary collapse

Instance Method Details

#option(key, default = nil, writable = false) ⇒ Object



26
27
28
29
# File 'lib/scrubyt/utils/ruby_extensions.rb', line 26

def option(key, default=nil, writable=false)
  option_reader(key => default)
  option_writer(key) if writable
end

#option_accessor(key_default_hash) ⇒ Object



31
32
33
34
35
# File 'lib/scrubyt/utils/ruby_extensions.rb', line 31

def option_accessor(key_default_hash)
  key_default_hash.each do |key, default|
    option(key, default, true)
  end
end

#option_reader(key_default_hash) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/scrubyt/utils/ruby_extensions.rb', line 2

def option_reader(key_default_hash)
  key_default_hash.each do |key, default|
    define_method(key) {
      if @options[key].nil?
        if default.is_a? Proc
          instance_eval(&default)
        else
          default
        end
      else
        @options[key]
      end
    }
  end
end

#option_writer(*keys) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/scrubyt/utils/ruby_extensions.rb', line 18

def option_writer(*keys)
  keys.each do |key|
    define_method("#{key.to_s}=".to_sym) { |value|
      @options[key] = value
    }
  end
end