module Kungfuig::InstanceMethods

Public Instance Methods

kungfuig(hos = nil) { |__options__| ... } click to toggle source

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

is passed, it is processed with @options instance.

@param hos [String|Hash|Hashie::Mash] the input data to merge into options

# File lib/kungfuig.rb, line 45
def kungfuig hos = nil
  MX.synchronize {
    merge_hash_or_string! hos
    yield __options__ if block_given?
    options
  }
end
option(*keys) click to toggle source

Accepts:

option :foo, :bar, 'baz'
option [:foo, 'bar', 'baz']
option 'foo.bar.baz'
option 'foo::bar::baz'
# File lib/kungfuig.rb, line 69
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) click to toggle source

Accepts:

option! [:foo, 'bar', 'baz'], value
option! 'foo.bar.baz', value
option! 'foo::bar::baz', value
# File lib/kungfuig.rb, line 86
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) click to toggle source
# File lib/kungfuig.rb, line 99
def option? *keys
  !option(*keys).nil?
end
options() click to toggle source
# File lib/kungfuig.rb, line 60
def options
  __options__.dup
end

Private Instance Methods

__options__() click to toggle source

Options getter @return [Hashie::Mash] options

# File lib/kungfuig.rb, line 55
def __options__
  @options ||= Hashie::Mash.new
end
merge_hash_or_string!(hos) click to toggle source

@param hos [Hash|String] the new values taken from hash,

mash or string (when string, should be either valid YAML file name or
string with valid YAML)
# File lib/kungfuig.rb, line 106
def merge_hash_or_string! hos
  __options__.deep_merge! Kungfuig.load_stuff hos
end