yaml_master 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf6bb10efe0b9d8174aaeeac4d496bfb615ecd75
4
- data.tar.gz: 40ed206f16f5a8c257ee1b3177c0c2a4c8379486
3
+ metadata.gz: 34993300413ebe4676e7cf80c868d219262334dc
4
+ data.tar.gz: 6ab8999f19b39b2d61116afea9bd67f406bc7b94
5
5
  SHA512:
6
- metadata.gz: 72044d3a058cca0e3194fd8e559826f45fb8356a81bd6435212e24fbe504c4b0b3915c25d8927fbaec04f67b07de26092e96d15e55223552c54c507a98aae7d4
7
- data.tar.gz: ea18452f7cef940744971b30960704e49cabccd8b8e13be8e1343c59788d418e7efb0d8b2a90b2710ab8a0cc871c864a7aa1a7e82da52365c370ee8212805454
6
+ metadata.gz: 1096589dade1789f856a57e3bddd8b05ccf4a89c97965743a530b5f6d92efda5a9b25290c6260d0a66fd436ac57872a002b62f613fe9f07ba9f2668206326005
7
+ data.tar.gz: 282ed2538a8ef0c9fbc2deb54581f7402cc89d7805c788ccba37d8a604b486055debaf6ffe214a6d29efe00d649fabd7393da295fed56ae838c3cc613c0ad89f
data/.travis.yml CHANGED
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
+ sudo: false
2
3
  rvm:
3
4
  - 2.2.3
4
5
  before_install: gem install bundler -v 1.10.5
6
+ cache: bundler
data/exe/yaml_master CHANGED
@@ -3,13 +3,14 @@
3
3
  require "yaml_master"
4
4
  require "slop"
5
5
 
6
- opts = Slop.parse do |o|
7
- o.string '-m', '--master', 'master yaml file'
8
- o.string '-k', '--key', 'target yaml key (in data: block)'
9
- o.string '-o', '--output', 'output filename'
10
- o.bool '-a', '--all', 'target all key (defined in yaml_master: block)'
11
- o.bool '-h', '--help', 'display this help'
12
- o.on '--version', 'print the version' do
6
+ opts = Slop.parse(strict: true) do
7
+ on 'm', 'master=', 'master yaml file'
8
+ on 'k', 'key=', 'single generate target key (in data: block)'
9
+ on 'o', 'output=', 'output filename for single generate target'
10
+ on 'a', 'all', 'target all key (defined in yaml_master: block)'
11
+ on 'h', 'help', 'display this help'
12
+ on 'v', 'verbose', 'verbose mode'
13
+ on '--version', 'print the version' do
13
14
  puts YamlMaster::VERSION
14
15
  exit
15
16
  end
@@ -23,13 +24,22 @@ if options[:help]
23
24
  end
24
25
 
25
26
  unless options[:master]
26
- raise "--master options is necessary"
27
+ puts "--master options is necessary"
28
+ puts opts
29
+ exit 1
30
+ end
31
+
32
+ if options[:all].nil? && options[:key].nil?
33
+ puts "--all or --key is necessary"
34
+ puts opts
35
+ exit 1
27
36
  end
28
37
 
29
38
  yaml_master = YamlMaster.new(options[:master])
30
39
 
31
40
  if options[:all]
32
- yaml_master.generate_all
41
+ yaml_master.generate_all(verbose: options[:verbose])
33
42
  else
34
- yaml_master.generate(options[:key], options[:output])
43
+ result = yaml_master.generate(options[:key], options[:output], {verbose: options[:verbose]})
44
+ puts result unless options[:output]
35
45
  end
@@ -1,3 +1,3 @@
1
1
  class YamlMaster
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/yaml_master.rb CHANGED
@@ -2,16 +2,24 @@ require "yaml_master/version"
2
2
 
3
3
  require "yaml"
4
4
  require "erb"
5
+ require "pathname"
6
+
7
+ YAML.add_domain_type(nil, "include") do |type, val|
8
+ YAML.load_file(val)
9
+ end
5
10
 
6
11
  class YamlMaster
7
- attr_reader :master
12
+ attr_reader :master, :master_path
8
13
 
9
14
  def initialize(io_or_filename)
15
+ embedded_methods = EmbeddedMethods.new(self)
16
+ embedded_methods_binding = embedded_methods.instance_eval { binding }
10
17
  data =
11
18
  if io_or_filename.is_a?(IO)
12
- ERB.new(io_or_filename.read).result
19
+ ERB.new(io_or_filename.read).result(embedded_methods_binding)
13
20
  else
14
- ERB.new(File.read(io_or_filename)).result
21
+ @master_path = File.expand_path(io_or_filename)
22
+ ERB.new(File.read(io_or_filename)).result(embedded_methods_binding)
15
23
  end
16
24
 
17
25
  @master = YAML.load(data)
@@ -19,15 +27,45 @@ class YamlMaster
19
27
  raise "data key is necessary on toplevel" unless @master["data"]
20
28
  end
21
29
 
22
- def generate(key, output)
23
- output_file = File.open(output, 'w')
24
- YAML.dump(@master["data"][key], output_file)
25
- output_file.close
30
+ def generate(key, output, options = {})
31
+ puts "gen: #{output}" if options[:verbose]
32
+ yaml = YAML.dump(fetch_data_from_master(key))
33
+
34
+ return yaml unless output
35
+
36
+ File.open(output, 'w') do |f|
37
+ f.write(yaml)
38
+ end
26
39
  end
27
40
 
28
- def generate_all
41
+ def generate_all(options = {})
29
42
  @master["yaml_master"].each do |key, output|
30
- generate(key, output)
43
+ generate(key, output, options)
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def fetch_data_from_master(key)
50
+ @master["data"].fetch(key)
51
+ end
52
+
53
+ class EmbeddedMethods
54
+ def initialize(yaml_master)
55
+ @yaml_master = yaml_master
56
+ end
57
+
58
+ def master_path
59
+ Pathname(@yaml_master.master_path)
60
+ end
61
+
62
+ def user_home
63
+ Pathname(ENV["HOME"])
64
+ end
65
+
66
+ def read_file_if_exist(path)
67
+ return nil unless File.exist?(path)
68
+ File.read(path)
31
69
  end
32
70
  end
33
71
  end
data/yaml_master.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "slop"
21
+ spec.add_runtime_dependency "slop", "~> 3.6"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.10"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_master
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-08 00:00:00.000000000 Z
11
+ date: 2015-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '3.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
119
  version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
- rubygems_version: 2.4.8
122
+ rubygems_version: 2.5.0
123
123
  signing_key:
124
124
  specification_version: 4
125
125
  summary: Helper of YAML file generation from single master YAML file