Class: WIKK::Configuration

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

Overview

Reads json configuration and provides access to the configuration data as method calls.

@attr_accessor pjson [Hash] Raw hash created from reading the json file

Constant Summary collapse

VERSION =
'0.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = "#{File.dirname(__FILE__)}/../conf/config.json") ⇒ Configuration

Creates an instance of Configuration from a json file

Parameters:

  • filename (String|Hash) (defaults to: "#{File.dirname(__FILE__)}/../conf/config.json")

    The Json file or a Ruby Hash, equivalent to the json



15
16
17
18
19
20
21
22
# File 'lib/wikk_configuration.rb', line 15

def initialize(filename="#{File.dirname(__FILE__)}/../conf/config.json") 
  if filename.class == Hash
    @pjson = filename
  else
    json = File.read(filename)
    @pjson = JSON.parse(json)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args, &block) ⇒ Object

Note:

Be aware of the possibility of name conflicts between built in class methods an configuration items defined in the json file)

Default handler to map json configuration names to method names

Parameters:

  • symbol (symbol, String)

    The method name that maps to a json configuration item

  • args (Array)

    Not used, but would hold arguments to the method call. Should be zero length for our methods.

  • block (Block)

    Not used, but would be a code block supplied to the method.

Returns:

  • (Object)

    the data associated with the json name, (hence method name) in the configuration file.



39
40
41
42
43
44
45
46
47
48
# File 'lib/wikk_configuration.rb', line 39

def method_missing(symbol , *args, &block)
  s = symbol.to_s
  if @pjson[s] != nil
    return @pjson[s]
  elsif s[-1,1] == "="
    @pjson[s[0..-2]] = args[0]
  else
    super
  end     
end

Instance Attribute Details

#pjsonObject

Returns the value of attribute pjson.



10
11
12
# File 'lib/wikk_configuration.rb', line 10

def pjson
  @pjson
end

Instance Method Details

#respond_to?(symbol, include_private = false) ⇒ Boolean

Note:

We need to define respond_to? as well as method_missing to satisfy tests in some libraries.

Provides a test for a method named after a json configuration item exists

Parameters:

  • symbol (Symbol, String)

    The method name we need to test exists

  • include_private (Boolean) (defaults to: false)

    Extend the test to private methods

Returns:

  • (Boolean)

    true if the method exists



29
30
31
# File 'lib/wikk_configuration.rb', line 29

def respond_to?(symbol, include_private = false)
  (@pjson[s = symbol.to_s] != nil) || (s[-1,1] == '=' && @pjson[s[0..-2]] != nil) || super(symbol, include_private)
end

#to_sString

Returns the configuration.

Returns:

  • (String)

    the configuration



51
52
53
# File 'lib/wikk_configuration.rb', line 51

def to_s
  @pjson.to_s
end