yml2h 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6be9f9bcb989cdbf7bb2ba92cb7179ea3df2af3e5b54af2d2c6de92479088ecc
4
+ data.tar.gz: 8c4ee2028d7f1dc16dc7f06ec51a132fd4fec45ba4c8b44e0cbd44ede7498d0b
5
+ SHA512:
6
+ metadata.gz: b9958cceef3e79bac85cb1efc6694721f1f72f5fc6a258babeb5ecaef0787a7fa34fb61b3bef5fee49b03744e1e67cf9f4fe998e37c5fbbfa81cacd9b98e007c
7
+ data.tar.gz: 4ed77e767fa0007a620f1a01a314ebcad47a0669910d54eb8a71d76342afc41dbf2655476f84bdafddefdf603270b72ec6cbd4b3b8b56d9f51a668abc2cea9ed
@@ -0,0 +1,40 @@
1
+ # Yml2h
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/yml2h`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'yml2h'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install yml2h
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yml2h.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,5 @@
1
+ ---
2
+ :meta:
3
+ :created_at: !ruby/object:DateTime <%= @meta[:created_at] %>
4
+ :updated_at: !ruby/object:DateTime <%= @meta[:updated_at] %>
5
+ :uuid: <%= @meta[:uuid] %>
@@ -0,0 +1,17 @@
1
+ ---
2
+ :users: <% @users.each do |user| %>
3
+ - :name: <%= user[:name] %>
4
+ :email: <%= user[:email] %>
5
+ :tos_acceptance: <%= user[:tos_acceptance] %>
6
+ :address:
7
+ :street: <%= user[:address][:street] %>
8
+ :profile:
9
+ :introduction: <%= user[:profile][:introduction] %>
10
+ :hobbies: <% user[:profile][:hobbies].each do |hobby| %>
11
+ - <%= hobby %>
12
+ <% end %>
13
+ :meta:
14
+ :created_at: !ruby/object:DateTime <%= user[:meta][:created_at] %>
15
+ :updated_at: !ruby/object:DateTime <%= user[:meta][:updated_at] %>
16
+ :uuid: <%= user[:meta][:uuid] %>
17
+ <% end %>
@@ -0,0 +1,5 @@
1
+ ---
2
+ :meta:
3
+ :created_at: 2019-09-01 00:00:00.000000000 +00:00
4
+ :updated_at: 2019-09-01 00:00:00.000000000 +00:00
5
+ :uuid: "f0000000-f000-f000-f000-f00000000000"
@@ -0,0 +1,15 @@
1
+ ---
2
+ :users:
3
+ - :name: "Mario Lopez"
4
+ :email: "lil@mr.e"
5
+ :tos_acceptance: true
6
+ :address:
7
+ :street: "foosgonewild"
8
+ :profile:
9
+ :introduction: "foosgonewild.com/shop"
10
+ :hobbies:
11
+ - stuntin'
12
+ :meta:
13
+ :created_at: 2019-09-01 00:00:00.000000000 +00:00
14
+ :updated_at: 2019-09-01 00:00:00.000000000 +00:00
15
+ :uuid: "f0000000-f000-f000-f000-f00000000000"
@@ -0,0 +1,30 @@
1
+ # A team "fixture", using ERB+ivars to get dynamically-generated values
2
+ ---
3
+ :name: <%= @name %>
4
+ :rank: <%= @rank %>
5
+ :users: <% @users.each do |user| %>
6
+ - :name: <%= user[:name] %>
7
+ :email: <%= user[:email] %>
8
+ :tos_acceptance: <%= user[:tos_acceptance] %>
9
+ :address:
10
+ :street: <%= user[:address][:street] %>
11
+ :profile:
12
+ :introduction: <%= user[:profile][:introduction] %>
13
+ :hobbies: <% user[:profile][:hobbies].each do |hobby| %>
14
+ - <%= hobby %>
15
+ <% end %>
16
+ :meta:
17
+ :created_at: !ruby/object:DateTime <%= user[:meta][:created_at] %>
18
+ :updated_at: !ruby/object:DateTime <%= user[:meta][:updated_at] %>
19
+ :uuid: <%= user[:meta][:uuid] %>
20
+ <% end %>
21
+ :budget:
22
+ :amount: <%= @budget[:amount] %>
23
+ :meta:
24
+ :created_at: !ruby/object:DateTime <%= @budget[:meta][:created_at] %>
25
+ :updated_at: !ruby/object:DateTime <%= @budget[:meta][:updated_at] %>
26
+ :uuid: <%= @budget[:meta][:uuid] %>
27
+ :meta:
28
+ :created_at: !ruby/object:DateTime <%= @meta[:created_at] %>
29
+ :updated_at: !ruby/object:DateTime <%= @meta[:updated_at] %>
30
+ :uuid: <%= @meta[:uuid] %>
@@ -0,0 +1,31 @@
1
+ # A team "fixture", using ERB+ivars to get dynamically-generated values to ouput a ruby OpenStruct
2
+ --- !ruby/object:OpenStruct
3
+ table:
4
+ :name: <%= @name %>
5
+ :rank: <%= @rank %>
6
+ :users: <% @users.each do |user| %>
7
+ - :name: <%= user[:name] %>
8
+ :email: <%= user[:email] %>
9
+ :tos_acceptance: <%= user[:tos_acceptance] %>
10
+ :address:
11
+ :street: <%= user[:address][:street] %>
12
+ :profile:
13
+ :introduction: <%= user[:profile][:introduction] %>
14
+ :hobbies: <% user[:profile][:hobbies].each do |hobby| %>
15
+ - <%= hobby %>
16
+ <% end %>
17
+ :meta:
18
+ :created_at: !ruby/object:DateTime <%= user[:meta][:created_at] %>
19
+ :updated_at: !ruby/object:DateTime <%= user[:meta][:updated_at] %>
20
+ :uuid: <%= user[:meta][:uuid] %>
21
+ <% end %>
22
+ :budget:
23
+ :amount: <%= @budget[:amount] %>
24
+ :meta:
25
+ :created_at: !ruby/object:DateTime <%= @budget[:meta][:created_at] %>
26
+ :updated_at: !ruby/object:DateTime <%= @budget[:meta][:updated_at] %>
27
+ :uuid: <%= @budget[:meta][:uuid] %>
28
+ :meta:
29
+ :created_at: !ruby/object:DateTime <%= @meta[:created_at] %>
30
+ :updated_at: !ruby/object:DateTime <%= @meta[:updated_at] %>
31
+ :uuid: <%= @meta[:uuid] %>
@@ -0,0 +1,28 @@
1
+ # An example "Team", as a plain ruby hash
2
+ ---
3
+ :name: "Foos"
4
+ :rank: 1
5
+ :users:
6
+ - :name: "Mario Lopez"
7
+ :email: "lil@mr.e"
8
+ :tos_acceptance: true
9
+ :address:
10
+ :street: "foosgonewild"
11
+ :profile:
12
+ :introduction: "foosgonewild.com/shop"
13
+ :hobbies:
14
+ - stuntin'
15
+ :meta:
16
+ :created_at: 2019-09-01 00:00:00.000000000 +00:00
17
+ :updated_at: 2019-09-01 00:00:00.000000000 +00:00
18
+ :uuid: "f0000000-f000-f000-f000-f00000000000"
19
+ :budget:
20
+ :amount: 1000000
21
+ :meta:
22
+ :created_at: 2019-09-01 00:00:00.000000000 +00:00
23
+ :updated_at: 2019-09-01 00:00:00.000000000 +00:00
24
+ :uuid: "f0000000-f000-f000-f000-f00000000000"
25
+ :meta:
26
+ :created_at: 2019-09-01 00:00:00.000000000 +00:00
27
+ :updated_at: 2019-09-01 00:00:00.000000000 +00:00
28
+ :uuid: "f0000000-f000-f000-f000-f00000000000"
@@ -0,0 +1,29 @@
1
+ # An example Team as an OpenStruct, with ERB tags (no variables)
2
+ --- !ruby/object:OpenStruct
3
+ table:
4
+ :name: "Foos"
5
+ :rank: 1
6
+ :users:
7
+ - :name: "Mario Lopez"
8
+ :email: "lil@mr.e"
9
+ :tos_acceptance: true
10
+ :address:
11
+ :street: "foosgonewild"
12
+ :profile:
13
+ :introduction: "foosgonewild.com/shop"
14
+ :hobbies:
15
+ - stuntin'
16
+ :meta:
17
+ :created_at: <%= DateTime.parse("2019-09-01") %>
18
+ :updated_at: <%= DateTime.parse("2019-09-01") %>
19
+ :uuid: "f0000000-f000-f000-f000-f00000000000"
20
+ :budget:
21
+ :amount: 1000000
22
+ :meta:
23
+ :created_at: <%= DateTime.parse("2019-09-01") %>
24
+ :updated_at: <%= DateTime.parse("2019-09-01") %>
25
+ :uuid: "f0000000-f000-f000-f000-f00000000000"
26
+ :meta:
27
+ :created_at: <%= DateTime.parse("2019-09-01") %>
28
+ :updated_at: <%= DateTime.parse("2019-09-01") %>
29
+ :uuid: "f0000000-f000-f000-f000-f00000000000"
@@ -0,0 +1,89 @@
1
+ require_relative "yml2h/version"
2
+ require "erb"
3
+ require "fileutils"
4
+ require "ostruct"
5
+ require "yaml"
6
+
7
+ module Yml2h
8
+ module Write
9
+ attr_writer :output_file
10
+ def write_to_yaml!(path = nil)
11
+ destination = path || @output_file || "anonymous.yml"
12
+ write_to_yaml(self, Pathname.new(destination))
13
+ end
14
+
15
+ # NOTE: When a hash is extended with Write, #write_to_yaml becomes available as a private method.
16
+ module_function
17
+
18
+ # @param object [Object] A ruby object which can be written to YAML
19
+ # @param path [String] An absolute path which the YAML will be written to
20
+ def write_to_yaml(hash, path)
21
+ File.open(path, "w") { |f| f.write(hash.to_yaml) }
22
+ end
23
+ end
24
+
25
+ module Read
26
+ attr_writer :input_file
27
+ def read_from_yaml!(path = nil, permitted_classes: nil)
28
+ input = path || @input_file || "anonymous.yml"
29
+ read_from_yaml(input, permitted_classes: permitted_classes)
30
+ end
31
+
32
+ module_function
33
+
34
+ # @param path [String, Pathname] An absolute path to parseable YAML contents
35
+ # @param permitted_classes [Array<Object>] Keyword arg; Additive list of classes to permit when loading a file
36
+ def read_from_yaml(path, permitted_classes: nil)
37
+ raw = ERB.new(Pathname.new(path).read).result(binding)
38
+ permitted_classes ||= [Symbol, Struct, OpenStruct, Time, Date, DateTime]
39
+ YAML.safe_load(raw, permitted_classes: permitted_classes)
40
+ rescue ArgumentError => e
41
+ # TODO: Remove after ruby 2.5 support dropped (currently only here for AWS compat)
42
+ fail e unless e.message.match?(/unknown keyword: permitted_classes/)
43
+ YAML.safe_load(raw, permitted_classes)
44
+ end
45
+ end
46
+
47
+ module Compile
48
+ attr_writer :input_dir
49
+ def compile_from_dir!(dir = nil, extension: false, **options)
50
+ source = dir || @input_dir || `pwd`.chomp
51
+ compile_from_dir(Pathname.new(source), extension: extension, **options).to_h
52
+ end
53
+
54
+ module_function
55
+
56
+ # @param dir [String, Pathname] An absolute path to a directory which will have YAML files
57
+ # @param extension [Boolean] Whether or not to keep the file extension in the compiled result
58
+ # @param options @see Read#read_from_yaml
59
+ # @return [Array<Array<String, Object>>] Set of arrays where idx0 is the file name (extension optionally truncated), idx1 the attributes
60
+ def compile_from_dir(dir, extension: false, **options)
61
+ include_fixtures = singleton_class.included_modules.include?(Read)
62
+ file_extensions = %w[yml yaml]
63
+ file_extensions += %w[yml.erb yaml.erb] if include_fixtures
64
+
65
+ Dir.glob("#{dir}/*.{#{file_extensions.join(",")}}").map do |filename|
66
+ definition = filename
67
+
68
+ # Drop final "/" and extension unless specified
69
+ unless extension
70
+ # Drop extension
71
+ definition = definition.gsub(/\.(yml|yaml)(\.erb)?\z/i, "")
72
+ # Capture final "/" onwards
73
+ definition = definition.match(/\/\w+\z/i).to_s
74
+ # Drop "/"
75
+ definition = definition.sub("/", "")
76
+ end
77
+
78
+ # If we have access to Read#read_from_yaml!, use it here for injecting our current binding
79
+ as_object = if include_fixtures
80
+ read_from_yaml!(filename, **options)
81
+ else
82
+ Read.read_from_yaml(filename, **options)
83
+ end
84
+
85
+ [definition, as_object]
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,3 @@
1
+ module Yml2h
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yml2h
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Trevor James
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: amazing_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: standard
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email: trevor@osrs-stat.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - README.md
90
+ - examples/compile_erb/meta.yml.erb
91
+ - examples/compile_erb/users.yml.erb
92
+ - examples/compile_yml/meta.yml
93
+ - examples/compile_yml/users.yml
94
+ - examples/fixture_team_hash.yml.erb
95
+ - examples/fixture_team_ostruct.yml.erb
96
+ - examples/team_hash.yml
97
+ - examples/team_ostruct.yml
98
+ - lib/yml2h.rb
99
+ - lib/yml2h/version.rb
100
+ homepage: https://github.com/fire-pls/yml2h
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 2.5.8
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.7.4
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.7.6.2
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Convert Ruby hashes to YAML & vice-versa
124
+ test_files: []