trickery 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ require 'optparse'
2
+ require 'yaml'
3
+
4
+ require File.expand_path('../hash/new_nested', __FILE__)
5
+ require File.expand_path('../hash/deep_merge', __FILE__)
6
+
7
+ module Trickery
8
+ class Configurability < Struct.new(:prefix, :fields)
9
+
10
+ def default_opts
11
+ @default_opts ||= nested_hash.tap do |opts|
12
+ fields.each do |field|
13
+ names = field[2].split('.')
14
+ value = opts
15
+ names.each do |name|
16
+ if name == names.last
17
+ value[name] = field[3]
18
+ else
19
+ value = value[name]
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def cli_opts(args)
27
+ @cli_opts ||= nested_hash.tap do |opts|
28
+ args.options do |o|
29
+ fields.each do |field|
30
+ o.on(field[0], field[1]+'=val', String) do |val|
31
+ names = field[2].split('.')
32
+ value = opts
33
+ names.each do |name|
34
+ if name == names.last
35
+ value[name] = val
36
+ else
37
+ value = value[name]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ o.parse!
43
+ end
44
+ end
45
+ end
46
+
47
+ def env_opts
48
+ @env_opts ||= nested_hash.tap do |opts|
49
+ fields.each do |field|
50
+ env = (prefix+'.'+field[2]).upcase.tr('.', '_')
51
+ if val = ENV[env]
52
+ names = field[2].split('.')
53
+ value = opts
54
+ names.each do |name|
55
+ if name == names.last
56
+ value[name] = val
57
+ else
58
+ value = value[name]
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def file_opts(file)
67
+ return {} unless File.exist?(file)
68
+ YAML.load(File.read(file))
69
+ end
70
+
71
+ def digest(args)
72
+ opts = default_opts.deep_merge(env_opts, cli_opts(args))
73
+ file = opts['configfile']
74
+ default_opts.deep_merge(file_opts(file), env_opts, cli_opts(args))
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,9 @@
1
+ class Hash
2
+
3
+ class << self
4
+ def nested_hash
5
+ Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
6
+ end
7
+ end
8
+
9
+ end
@@ -1,3 +1,3 @@
1
1
  module Trickery
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trickery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-10-18 00:00:00.000000000 Z
12
+ date: 2015-11-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -106,9 +106,11 @@ files:
106
106
  - lib/trickery.rb
107
107
  - lib/trickery/cmd_runner.rb
108
108
  - lib/trickery/config.rb
109
+ - lib/trickery/configurability.rb
109
110
  - lib/trickery/deep_ostruct.rb
110
111
  - lib/trickery/hash/deep_merge.rb
111
112
  - lib/trickery/hash/deep_ostruct.rb
113
+ - lib/trickery/hash/new_nested.rb
112
114
  - lib/trickery/version.rb
113
115
  - spec/spec_helper.rb
114
116
  - spec/trickery/deep_ostruct_spec.rb
@@ -130,7 +132,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
132
  version: '0'
131
133
  segments:
132
134
  - 0
133
- hash: -4013730140083209136
135
+ hash: -4447898236271774713
134
136
  required_rubygems_version: !ruby/object:Gem::Requirement
135
137
  none: false
136
138
  requirements:
@@ -139,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
141
  version: '0'
140
142
  segments:
141
143
  - 0
142
- hash: -4013730140083209136
144
+ hash: -4447898236271774713
143
145
  requirements: []
144
146
  rubyforge_project:
145
147
  rubygems_version: 1.8.23