Module: Kungfuig::InstanceMethods

Included in:
ClassMethods
Defined in:
lib/kungfuig.rb

Instance Method Summary collapse

Instance Method Details

#kungfuig(hos = nil) ⇒ Object

Configures everything by hash or yaml from string or file. Whether code block

is passed, it is processed with @options instance.

Parameters:

  • hos (String|Hash|Hashie::Mash) (defaults to: nil)

    the input data to merge into options



49
50
51
52
53
54
55
# File 'lib/kungfuig.rb', line 49

def kungfuig hos = nil
  MX.synchronize {
    merge_hash_or_string! hos
    yield __options__ if block_given?
    options
  }
end

#option(*keys) ⇒ Object

Accepts:

option :foo, :bar, 'baz'
option [:foo, 'bar', 'baz']
option 'foo.bar.baz'
option 'foo::bar::baz'


73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kungfuig.rb', line 73

def option *keys
  key = keys.map(&:to_s).join('.').gsub(/::/, '.').split('.')

  MX.synchronize {
    # options.foo!.bar!.baz!
    [key, key[1..-1]].map do |candidate|
      candidate && candidate.inject(options) do |memo, k|
        memo.public_send(k.to_s) unless memo.nil?
      end
    end.compact.first
  }
end

#option!(keys, value) ⇒ Object

Accepts:

option! [:foo, 'bar', 'baz'], value
option! 'foo.bar.baz', value
option! 'foo::bar::baz', value


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/kungfuig.rb', line 90

def option! keys, value
  key = (keys.is_a?(Array) ? keys.join('.') : keys.to_s).gsub(/::/, '.').split('.')
  last = key.pop

  MX.synchronize {
    # options.foo!.bar!.baz! = value
    build = key.inject(__options__) do |memo, k|
      memo.public_send("#{k}!")
    end
    build[last] = value
  }
end

#option?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/kungfuig.rb', line 103

def option? *keys
  !option(*keys).nil?
end

#optionsObject



64
65
66
# File 'lib/kungfuig.rb', line 64

def options
  __options__.dup
end