pick_and_roll 0.9 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/pick_and_roll.rb +49 -23
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7892a2ef90c07b149b2f958ed57d0c4f5bfa04a
4
- data.tar.gz: dfd28b34fdd449f3e58f35d62c4f8c264c2faba5
3
+ metadata.gz: 73616cb908100f380eb2e2818ce72f68e165f0c6
4
+ data.tar.gz: b45aecd506c90312c122d98c97f7b8bd1776f4e3
5
5
  SHA512:
6
- metadata.gz: 8b455ae0d0c4e6063be872831dc0d19f2fce4228e13350d2507840aab923d533afbd45574127c25745684b39a81d82874925e2be4da5843aa7bbcef3a0610518
7
- data.tar.gz: 6efc0feb7fe303f63890890c3d0855c2f2ef5880ed0c8d97b7d146bd4fe75f00d3a3201400a14b7c7d03142511b6d697d5f4d49de7d6bd4a3d58ce5ded0da13d
6
+ metadata.gz: f3c480d5346cb15b60532fd7e4bc2293e53b33268c868ccb7666a25399397ba0d2ba381e4c7d2b2dc09eac26a7c988cffe4e04268d259b08d719a7510ce659d6
7
+ data.tar.gz: 6c7b38bf6e2f708768572202f0191dc4dddb6e91a4f30662f5159caaa4eeea842c656f4f5aaf51a86f14fad005434eddc4f2c1dcc4acd237fe6e790c49c58770
data/lib/pick_and_roll.rb CHANGED
@@ -1,40 +1,33 @@
1
1
  require 'json'
2
- require 'hash_deep_merge'
2
+ require 'deep'
3
3
  #require 'awesome_print'
4
4
 
5
5
  class PickAndRoll
6
+ CONFIGS_DIR = '_configs'
6
7
  MASTER_CONFIG = 'config.json'
7
8
  PARCONFIG_FILE_NAME = '.parconfig'
8
9
 
9
- def initialize(config_path = '')
10
+ def initialize(config_path = '')
10
11
  @parconfig = read_configuration
12
+ @configs_dir = @parconfig.has_key?('customDir') ? @parconfig['customDir'] : CONFIGS_DIR
11
13
  @config_file = config_path.to_s
12
14
  end
13
15
 
14
16
  def pick
15
- master_config_name = @parconfig.has_key?('config') ? @parconfig['config'] : MASTER_CONFIG
16
- if File.exist?(master_config_name)
17
- @config = JSON.parse(File.read(master_config_name))
18
- puts "pick config: #{master_config_name}"
17
+ master_config_path = File.join(@configs_dir, @parconfig.has_key?('config') ? @parconfig['config'] : MASTER_CONFIG)
18
+ if File.size?(master_config_path) != nil
19
+ @config = JSON.parse(File.read(master_config_path))
20
+ puts "pick config: #{master_config_path}"
19
21
  else
20
22
  @config = Hash.new
21
23
  end
22
24
 
23
- if @config_file.strip.empty? == false && File.exists?("#{@config_file}.json")
24
- @config.deep_merge!(JSON.parse(File.read("#{@config_file}.json")))
25
- puts "pick config: #{@config_file}.json"
26
- else
27
- custom_config = "#{ENV["COMPUTERNAME"]}.json"
28
- if @parconfig.has_key?('customDir')
29
- custom_config = File.join(@parconfig['customDir'], custom_config)
30
- end
31
- if File.exist?(custom_config)
32
- @config.deep_merge!(JSON.parse(File.read(custom_config)))
33
- puts "pick config: #{custom_config}"
34
- end
25
+ custom_config_path = File.join(@configs_dir, "#{@config_file.strip.empty? ? ENV['COMPUTERNAME'] : @config_file}.json")
26
+ if File.size?(custom_config_path) != nil
27
+ @config.deep_merge!(JSON.parse(File.read(custom_config_path)))
28
+ puts "pick config: #{custom_config_path}"
35
29
  end
36
30
 
37
-
38
31
  if @config.empty?
39
32
  printf 'Please set configuration path or create config.json file'
40
33
  exit
@@ -46,10 +39,11 @@ class PickAndRoll
46
39
 
47
40
  file_patterns.each {|file_pattern|
48
41
  Dir.glob("**/#{file_pattern}"){ |file_name|
49
- File.open(file_name,'r'){ |config_file|
42
+ File.open(file_name,'r'){ |rolling_file|
50
43
  puts "roll file: #{file_name}"
44
+ content = rolling_file.read()
51
45
  File.open(file_name.gsub(/\.generic\./,'.'), 'w'){ |f|
52
- f.write config_file.read().gsub(/@@([\w\.]*)@@/) {|s| find_config_value $1}
46
+ f.write content.gsub(/@@([\w\.]*)@@/) {|s| find_config_value $1}
53
47
  }
54
48
  }
55
49
  }
@@ -70,11 +64,43 @@ class PickAndRoll
70
64
  end
71
65
 
72
66
  def find_config_value(key)
73
- key.split('.').inject(@config) { |config, name| config[name] }
67
+ result = key.split('.').inject(@config) { |config, name| config.nil? ? nil : config[name] }
68
+ if result.nil?
69
+ puts "ERROR: cannot find value for parameter #{key}"
70
+ end
71
+ result
74
72
  end
75
73
 
76
74
  def go
77
75
  pick
78
76
  roll
79
77
  end
80
- end
78
+ end
79
+
80
+ #class Hash
81
+ #
82
+ # def deep_merge!(specialized_hash)
83
+ # return internal_deep_merge!(self, specialized_hash)
84
+ # end
85
+ #
86
+ # def deep_merge(specialized_hash)
87
+ # return internal_deep_merge!(Hash.new.replace(self), specialized_hash)
88
+ # end
89
+ #
90
+ # protected
91
+ # def internal_deep_merge!(source_hash, specialized_hash)
92
+ # specialized_hash.each_pair do |rkey, rval|
93
+ # if source_hash.has_key?(rkey) then
94
+ # if rval.is_a?(Hash) and source_hash[rkey].is_a?(Hash) then
95
+ # internal_deep_merge!(source_hash[rkey], rval)
96
+ # elsif rval == source_hash[rkey] then
97
+ # else
98
+ # source_hash[rkey] = rval
99
+ # end
100
+ # else
101
+ # source_hash[rkey] = rval
102
+ # end
103
+ # end
104
+ # return source_hash
105
+ # end
106
+ #end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pick_and_roll
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.9'
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dima Salakhov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-31 00:00:00.000000000 Z
11
+ date: 2013-08-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Creates customizable configuration files
14
14
  email: to@dimasalakhov.com
@@ -37,7 +37,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
37
37
  version: '0'
38
38
  requirements: []
39
39
  rubyforge_project:
40
- rubygems_version: 2.0.0
40
+ rubygems_version: 2.0.3
41
41
  signing_key:
42
42
  specification_version: 4
43
43
  summary: Configuration helper