wikk_configuration 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 787c6bbe19f13b0cf0408094dcee74c3e294be25
4
- data.tar.gz: 7ef971f09fbcc75da50230d76b554fa26842465e
3
+ metadata.gz: 4487d7e6c0dd986e50ea7a8350b1b7acd94c3edd
4
+ data.tar.gz: 7f14ad1c249a25ff06ee1ee8195bcde18db12262
5
5
  SHA512:
6
- metadata.gz: 440bc4b32e6e439db0c608b04272229a3f7b6f89a50815b2d8a9eda0c9bdc22e4a4af2ad0603acbc493eeb8166147dcbd04f746f2c3df1ff46b62abdfca683bf
7
- data.tar.gz: 5a3b08024476a983125783e85e89cb0bc7419066650a1ef5bd668da2594dd4447bf887312f8ea2cea26d328b984d60b0e9017a8cd810453c3ec404dcd8dc2fca
6
+ metadata.gz: 9019fb2bc80c67ed5a4bcb16acc76840485062cd9150c3be64c1fb43571a5584d7242858413086c3a197dd91c14a1abab9490068de18ef380494a89667489315
7
+ data.tar.gz: 98fba6d08a695bd77debc1cf6ed92ebb2ecbf134c636fe7ad339d11a14e96b38778cb8a1663a15a1543b507ac04eb5c74f3232230f2384af92e7757bd8445b59
@@ -3,15 +3,22 @@ module WIKK
3
3
 
4
4
  #Reads json configuration and provides access to the configuration data
5
5
  #as method calls.
6
+ # @attr_accessor pjson [Hash] Raw hash created from reading the json file
6
7
  class Configuration
7
- VERSION = '0.1.1'
8
+ VERSION = '0.1.2'
8
9
 
10
+ attr_accessor :pjson
11
+
9
12
  #Creates an instance of Configuration from a json file
10
- # @param [String] filename The Json file
13
+ # @param [String|Hash] filename The Json file or a Ruby Hash, equivalent to the json
11
14
  # @return [Configuration]
12
15
  def initialize(filename="#{File.dirname(__FILE__)}/../conf/config.json")
13
- json = File.read(filename)
14
- @pjson = JSON.parse(json)
16
+ if filename.class == Hash
17
+ @pjson = filename
18
+ else
19
+ json = File.read(filename)
20
+ @pjson = JSON.parse(json)
21
+ end
15
22
  end
16
23
 
17
24
  #Provides a test for a method named after a json configuration item exists
@@ -20,7 +27,7 @@ module WIKK
20
27
  # @param include_private [Boolean] Extend the test to private methods
21
28
  # @return [Boolean] true if the method exists
22
29
  def respond_to?(symbol, include_private = false)
23
- (@pjson[symbol.to_s] != nil) || super(symbol, include_private)
30
+ (@pjson[s = symbol.to_s] != nil) || (s[-1,1] == '=' && @pjson[s[0..-2]] != nil) || super(symbol, include_private)
24
31
  end
25
32
 
26
33
  #Default handler to map json configuration names to method names
@@ -33,6 +40,8 @@ module WIKK
33
40
  s = symbol.to_s
34
41
  if @pjson[s] != nil
35
42
  return @pjson[s]
43
+ elsif s[-1,1] == "="
44
+ @pjson[s[0..-2]] = args[0]
36
45
  else
37
46
  super
38
47
  end
data/test/manualtest.rb CHANGED
@@ -10,8 +10,13 @@ def test(config_file)
10
10
  puts "creating config from '#{config_file}'"
11
11
  config = Configuration.new(config_file)
12
12
 
13
+ puts "****** Dump config using to_s *********"
14
+ pp config
15
+ puts
16
+ puts
13
17
  puts "******Should be there*********"
14
18
  puts "base_directories is defined? #{config.respond_to?(:base_directory)}"
19
+ puts "base_directories= is defined? #{config.respond_to?(:base_directory=)}"
15
20
  puts "base_directories is: '#{config.base_directory}'"
16
21
  puts "******Should NOT be there*********"
17
22
  puts "not_there is defined? #{config.respond_to?(:not_there)}"
@@ -30,8 +35,32 @@ def test(config_file)
30
35
  puts "******Numeric*********"
31
36
  puts "numeric is of class: #{config.numeric.class}"
32
37
  puts "config.numeric => #{config.numeric}"
38
+ puts
39
+ puts "******Alter Numeric value and fetch again*********"
40
+ config.numeric = 1001
41
+ puts "config.numeric => #{config.numeric}"
42
+ puts config.pjson
43
+ puts
44
+ puts
45
+ puts
46
+ hash_source = { #Ruby equivalent of the json.
47
+ "base_directory" => "/usr/local/random",
48
+ "hello" => [ 0, 1, 2, 3 , 4 ],
49
+ "world" => { "0" => 0, "1" => 1, "2" => 2 },
50
+ "boolean" => true,
51
+ "string" => "string",
52
+ "numeric" => 1.2345,
53
+ "deep" => { "array" => [ 0, 1, 2, 3 , 4 ], "hash" => { "0" => 0, "1" => 1, "2" => 2 } },
54
+ "deeper" => { "hash" => { "array" => [ 0, 1, 2, 3 , 4 ] } }
55
+ }
56
+ puts "creating config from hash"
57
+ config_hash_source = Configuration.new(hash_source)
33
58
  puts "****** Dump config using to_s *********"
34
59
  pp config
60
+ puts "******Numeric from hashed source *********"
61
+ puts "numeric is of class: #{config_hash_source.numeric.class}"
62
+ puts "config_hash_source.numeric => #{config_hash_source.numeric}"
63
+
35
64
  end
36
65
 
37
66
  puts "Start"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wikk_configuration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Burrowes
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-19 00:00:00.000000000 Z
11
+ date: 2016-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hoe-yard