data_crate 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ac96e96f7bd14c705dc841ba66b4d6a51eaa8551d0935df49db4d6a990226c9
4
+ data.tar.gz: deeef65b54ff7e5ac74d0d74b7f6bfaa5437f860f88f84d5b09e4100d00c0976
5
+ SHA512:
6
+ metadata.gz: 5a7b9ab6c37891886b1350e4258a984dd349f7c8d864925ca9c33ff018efababbb516cdd31d25b1621cf09e586f6186b67d035fd64f42071134f853510f18af9
7
+ data.tar.gz: 846f0d80cea24fae9cdd3f8336b1acf0fb70b23f82f08b0f750bd41eaf0794c4cbfdbeb1185efe336d6c53ef8b6bd9e02636e5164bc7295dd564cd68d6c287db
data/.fasterer.yml ADDED
@@ -0,0 +1,2 @@
1
+ speedups:
2
+ proc_call_vs_yield: false
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .byebug_history
10
+
11
+ # rspec failure tracking
12
+ /spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: '2.4'
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ - '*.gemspec'
12
+ - 'spec/**/*'
13
+
14
+ Style/TrailingCommaInLiteral:
15
+ EnforcedStyleForMultiline: comma
16
+
17
+ Layout/AlignHash:
18
+ Enabled: false
data/.rubocop_todo.yml ADDED
File without changes
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in data_crate.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,123 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ data_crate (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.3.0)
10
+ byebug (9.1.0)
11
+ coderay (1.1.2)
12
+ colorize (0.8.1)
13
+ diff-lcs (1.3)
14
+ fasterer (0.3.2)
15
+ colorize (~> 0.7)
16
+ ruby_parser (~> 3.7)
17
+ ffi (1.9.18)
18
+ formatador (0.2.5)
19
+ guard (2.14.1)
20
+ formatador (>= 0.2.4)
21
+ listen (>= 2.7, < 4.0)
22
+ lumberjack (~> 1.0)
23
+ nenv (~> 0.1)
24
+ notiffany (~> 0.0)
25
+ pry (>= 0.9.12)
26
+ shellany (~> 0.0)
27
+ thor (>= 0.18.1)
28
+ guard-bundler (2.1.0)
29
+ bundler (~> 1.0)
30
+ guard (~> 2.2)
31
+ guard-compat (~> 1.1)
32
+ guard-compat (1.2.1)
33
+ guard-fasterer (0.1.0)
34
+ fasterer (~> 0.1)
35
+ guard (>= 2.13.0)
36
+ guard-compat (>= 1.2.1)
37
+ guard-rspec (4.7.3)
38
+ guard (~> 2.1)
39
+ guard-compat (~> 1.1)
40
+ rspec (>= 2.99.0, < 4.0)
41
+ guard-rubocop (1.3.0)
42
+ guard (~> 2.0)
43
+ rubocop (~> 0.20)
44
+ guard-shell (0.7.1)
45
+ guard (>= 2.0.0)
46
+ guard-compat (~> 1.0)
47
+ listen (3.1.5)
48
+ rb-fsevent (~> 0.9, >= 0.9.4)
49
+ rb-inotify (~> 0.9, >= 0.9.7)
50
+ ruby_dep (~> 1.2)
51
+ lumberjack (1.0.12)
52
+ method_source (0.9.0)
53
+ nenv (0.3.0)
54
+ notiffany (0.1.1)
55
+ nenv (~> 0.1)
56
+ shellany (~> 0.0)
57
+ parallel (1.12.0)
58
+ parser (2.4.0.2)
59
+ ast (~> 2.3)
60
+ powerpack (0.1.1)
61
+ pry (0.11.3)
62
+ coderay (~> 1.1.0)
63
+ method_source (~> 0.9.0)
64
+ rainbow (2.2.2)
65
+ rake
66
+ rake (12.3.0)
67
+ rb-fsevent (0.10.2)
68
+ rb-inotify (0.9.10)
69
+ ffi (>= 0.5.0, < 2)
70
+ rspec (3.7.0)
71
+ rspec-core (~> 3.7.0)
72
+ rspec-expectations (~> 3.7.0)
73
+ rspec-mocks (~> 3.7.0)
74
+ rspec-core (3.7.0)
75
+ rspec-support (~> 3.7.0)
76
+ rspec-expectations (3.7.0)
77
+ diff-lcs (>= 1.2.0, < 2.0)
78
+ rspec-support (~> 3.7.0)
79
+ rspec-its (1.2.0)
80
+ rspec-core (>= 3.0.0)
81
+ rspec-expectations (>= 3.0.0)
82
+ rspec-mocks (3.7.0)
83
+ diff-lcs (>= 1.2.0, < 2.0)
84
+ rspec-support (~> 3.7.0)
85
+ rspec-support (3.7.0)
86
+ rubocop (0.51.0)
87
+ parallel (~> 1.10)
88
+ parser (>= 2.3.3.1, < 3.0)
89
+ powerpack (~> 0.1)
90
+ rainbow (>= 2.2.2, < 3.0)
91
+ ruby-progressbar (~> 1.7)
92
+ unicode-display_width (~> 1.0, >= 1.0.1)
93
+ ruby-progressbar (1.9.0)
94
+ ruby_dep (1.5.0)
95
+ ruby_parser (3.10.1)
96
+ sexp_processor (~> 4.9)
97
+ sexp_processor (4.10.0)
98
+ shellany (0.0.1)
99
+ thor (0.20.0)
100
+ unicode-display_width (1.3.0)
101
+
102
+ PLATFORMS
103
+ ruby
104
+
105
+ DEPENDENCIES
106
+ bundler
107
+ byebug
108
+ data_crate!
109
+ fasterer
110
+ guard
111
+ guard-bundler
112
+ guard-fasterer
113
+ guard-rspec
114
+ guard-rubocop
115
+ guard-shell
116
+ pry
117
+ rake
118
+ rspec
119
+ rspec-its
120
+ rubocop
121
+
122
+ BUNDLED WITH
123
+ 1.16.0
data/Guardfile ADDED
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A sample Guardfile
4
+ # More info at https://github.com/guard/guard#readme
5
+
6
+ ## Uncomment and set this to only include directories you want to watch
7
+ # directories %w(app lib config test spec features) \
8
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
9
+
10
+ ## Note: if you are using the `directories` clause above and you are not
11
+ ## watching the project directory ('.'), then you will want to move
12
+ ## the Guardfile to a watched dir and symlink it back, e.g.
13
+ #
14
+ # $ mkdir config
15
+ # $ mv Guardfile config/
16
+ # $ ln -s config/Guardfile .
17
+ #
18
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
19
+
20
+ guard :bundler do
21
+ require 'guard/bundler'
22
+ require 'guard/bundler/verify'
23
+ helper = Guard::Bundler::Verify.new
24
+
25
+ files = ['Gemfile']
26
+ files += Dir['*.gemspec'] if files.any? { |f| helper.uses_gemspec?(f) }
27
+
28
+ # Assume files are symlinked from somewhere
29
+ files.each { |file| watch(helper.real_path(file)) }
30
+ end
31
+
32
+ # Note: The cmd option is now required due to the increasing number of ways
33
+ # rspec may be run, below are examples of the most common uses.
34
+ # * bundler: 'bundle exec rspec'
35
+ # * bundler binstubs: 'bin/rspec'
36
+ # * spring: 'bin/rspec' (This will use spring if running and you have
37
+ # installed the spring binstubs per the docs)
38
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
39
+ # * 'just' rspec: 'rspec'
40
+
41
+ guard :rspec, cmd: 'bundle exec rspec' do
42
+ require 'guard/rspec/dsl'
43
+ dsl = Guard::RSpec::Dsl.new(self)
44
+
45
+ # Feel free to open issues for suggestions and improvements
46
+
47
+ # RSpec files
48
+ rspec = dsl.rspec
49
+ watch(rspec.spec_helper) { rspec.spec_dir }
50
+ watch(rspec.spec_support) { rspec.spec_dir }
51
+ watch(rspec.spec_files)
52
+
53
+ # Ruby files
54
+ ruby = dsl.ruby
55
+ dsl.watch_spec_files_for(ruby.lib_files)
56
+ end
57
+
58
+ guard :rubocop do
59
+ watch(/.+\.rb$/)
60
+ watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
61
+ end
62
+
63
+ guard :fasterer do
64
+ watch(%r{^lib/.*.rb})
65
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Minwoo Lee
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # DataCrate
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/data_crate`. 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 'data_crate'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install data_crate
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]/data_crate.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/gem_tasks'
5
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
6
+ end
7
+
8
+ begin
9
+ require 'rspec/core/rake_task'
10
+ RSpec::Core::RakeTask.new(:spec)
11
+ task default: :spec
12
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
13
+ end
14
+
15
+ begin
16
+ require 'rubocop/rake_task'
17
+ RuboCop::RakeTask.new
18
+ namespace :rubocop do
19
+ desc "Run 'rubocop --auto-gen-config'"
20
+ task :todo do
21
+ sh 'rubocop --auto-gen-config'
22
+ end
23
+ end
24
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
25
+ end
26
+
27
+ begin
28
+ require 'rubycritic/rake_task'
29
+ RubyCritic::RakeTask.new do |task|
30
+ task.paths = FileList['**/*.rb'] \
31
+ - FileList['spec/**/*_spec.rb'] \
32
+ - FileList['lib/**/cli_deprecated.rb']
33
+ task.options = '--no-browser' + (ENV['CI'] ? ' --format console' : '')
34
+ end
35
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
36
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'data_crate'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'crate/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'data_crate'
9
+ spec.version = Crate::VERSION
10
+ spec.authors = ['Minwoo Lee']
11
+ spec.email = ['ermaker@gmail.com']
12
+
13
+ spec.summary = 'Crate'
14
+ spec.description = 'Crate'
15
+ spec.homepage = 'https://github.com/ermaker/data_crate'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler'
26
+ spec.add_development_dependency 'byebug'
27
+ spec.add_development_dependency 'fasterer'
28
+ spec.add_development_dependency 'guard'
29
+ spec.add_development_dependency 'guard-bundler'
30
+ spec.add_development_dependency 'guard-fasterer'
31
+ spec.add_development_dependency 'guard-rspec'
32
+ spec.add_development_dependency 'guard-rubocop'
33
+ spec.add_development_dependency 'guard-shell'
34
+ spec.add_development_dependency 'pry'
35
+ spec.add_development_dependency 'rake'
36
+ spec.add_development_dependency 'rspec'
37
+ spec.add_development_dependency 'rspec-its'
38
+ spec.add_development_dependency 'rubocop'
39
+ end
@@ -0,0 +1,190 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module Crate
6
+ class Crate # rubocop:disable Metrics/ClassLength
7
+ class << self
8
+ def _initialize(**options)
9
+ @_names = []
10
+ @_crates = {}
11
+ @_hooks = []
12
+ @_options = { logger: Logger.new(nil), **options }
13
+ @_crate_klasses = []
14
+ end
15
+
16
+ def _class(**options, &initializer)
17
+ Class.new(self) do
18
+ _initialize(**options)
19
+ class_eval(&initializer) if block_given?
20
+ end
21
+ end
22
+
23
+ def crate(*args, **options, &initializer)
24
+ name, klass, = args
25
+ declare(name)
26
+ @_crates[name] = klass || _class(
27
+ **_options, basename: name.to_s, **options,
28
+ &initializer
29
+ )
30
+ end
31
+
32
+ def versions(*crate_klasses)
33
+ @_crate_klasses = crate_klasses
34
+ end
35
+
36
+ def version(*args_, &block)
37
+ key, value, = *args_
38
+ block ||= proc { |raw| raw[key] == value }
39
+ (class << self; self end) \
40
+ .send(:define_method, :_valid_crate?) do |*args|
41
+ block.call(*args)
42
+ end
43
+ end
44
+
45
+ FILTERS = %i[default convert type strip required must_be].freeze
46
+
47
+ def _invalid_filter?(**options)
48
+ !(options.keys - FILTERS).empty?
49
+ end
50
+
51
+ def declare(name, **options)
52
+ raise if _invalid_filter?(**options)
53
+ return if name.nil? || method_defined?(name)
54
+
55
+ _declare_impl(name)
56
+ _make_hooks(name, **options)
57
+ end
58
+
59
+ def _declare_impl(name)
60
+ _names.push(name)
61
+ define_method(name) { _raw[name] }
62
+ end
63
+
64
+ def _make_args(hook_name, args) # rubocop:disable Metrics/MethodLength
65
+ args = [] if args == true
66
+ args = Array(args)
67
+ args, block =
68
+ if args[-1].respond_to?(:to_proc)
69
+ [args[0...-1], args[-1]]
70
+ else
71
+ [args, nil]
72
+ end
73
+ args.push({}) unless args[-1].is_a?(Hash)
74
+ args[-1] = { tag: hook_name, **args[-1] }
75
+ [args, block]
76
+ end
77
+
78
+ def _make_hooks(name, **options)
79
+ options.each do |hook_name, args|
80
+ args, block = _make_args(hook_name, args)
81
+ hook { method(:"_hook_#{hook_name}").call(name, *args, &block) }
82
+ end
83
+ end
84
+
85
+ def hook(&hook_)
86
+ _hooks.push(hook_)
87
+ end
88
+
89
+ def check_undefined_keys(**options)
90
+ hook do
91
+ method(:_hook_check_undefined_keys) \
92
+ .call(tag: 'check_undefined_keys', **options)
93
+ end
94
+ end
95
+
96
+ attr_reader :_options, :_names, :_crates, :_hooks, :_crate_klasses
97
+
98
+ def _basename
99
+ _options[:basename]
100
+ end
101
+ end
102
+
103
+ _initialize
104
+
105
+ def _log(**options)
106
+ options[:logger].add(options[:severity]) do
107
+ "#{options[:name]}: [#{options[:tag]}]" + \
108
+ (block_given? ? " #{yield}" : '')
109
+ end
110
+ end
111
+
112
+ def _fullname
113
+ _options[:fullname]
114
+ end
115
+
116
+ def _name(name)
117
+ "#{_fullname}[#{name.inspect}]"
118
+ end
119
+
120
+ attr_reader :_raw, :_options
121
+
122
+ def initialize(raw, **options)
123
+ @_options =
124
+ { **self.class._options, fullname: self.class._basename, **options }
125
+ @_raw = Raw.new(raw, **_options).to_hash
126
+ _new_crates
127
+ _run_hooks
128
+
129
+ return if self.class._crate_klasses.empty?
130
+ _select_klass(_find_valid_klass(raw, **options))
131
+ end
132
+
133
+ def _new_crates
134
+ self.class._crates.each do |name, klass|
135
+ _raw[name] = klass.new(_raw[name], **_options, fullname: _name(name))
136
+ end
137
+ end
138
+
139
+ def _run_hooks
140
+ self.class._hooks.each { |hook| instance_eval(&hook) }
141
+ end
142
+
143
+ def _find_valid_klass(*args)
144
+ self.class._crate_klasses.find do |klass|
145
+ klass._valid_crate?(*args)
146
+ end
147
+ end
148
+
149
+ REJECT_METHODS = %i[object_id __send__ instance_eval].freeze
150
+
151
+ def _exit_if_no_klass(klass)
152
+ return unless klass.nil?
153
+ _log(**_options, name: _fullname,
154
+ tag: 'version', severity: Logger::FATAL) do
155
+ 'must find valid version but did not'
156
+ end
157
+ exit 1
158
+ end
159
+
160
+ def _select_klass(klass)
161
+ _exit_if_no_klass(klass)
162
+ @_crate = klass.new(@_raw, **_options)
163
+ (methods - REJECT_METHODS).each do |method_name|
164
+ instance_eval("undef #{method_name.inspect}")
165
+ end
166
+ (class << self; self end) \
167
+ .send(:define_method, :method_missing) do |*args, &block|
168
+ @_crate.send(*args, &block)
169
+ end
170
+ end
171
+
172
+ def _crate_serialize
173
+ Hash[_raw.map { |key, value| [key, value._crate_serialize] }]
174
+ end
175
+
176
+ def to_json
177
+ _crate_serialize.to_json
178
+ end
179
+
180
+ def to_pretty_json
181
+ JSON.pretty_generate(_crate_serialize)
182
+ end
183
+
184
+ include Hook
185
+ end
186
+ end
187
+
188
+ class Object
189
+ alias _crate_serialize itself
190
+ end
data/lib/crate/hook.rb ADDED
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module Crate
6
+ module Hook
7
+ def _hook_convert_impl(name, **options, &block) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength
8
+ _log(**_options, name: _name(name), **options, severity: Logger::DEBUG)
9
+ before_value = _raw[name]
10
+ after_value = instance_exec(before_value, &block)
11
+ return if before_value == after_value
12
+ not_set = !_raw.key?(name)
13
+ _raw[name] = after_value
14
+ before_message = not_set ? 'not set' : before_value.inspect
15
+ _log(**_options, name: _name(name), tag: 'convert',
16
+ severity: Logger::INFO, **options) do
17
+ "was #{before_message} and is now #{after_value.inspect}"
18
+ end
19
+ end
20
+
21
+ def _hook_convert(name, **options, &block)
22
+ _hook_convert_impl(name, **options, &block)
23
+ end
24
+
25
+ def _hook_default(name, *args, **options, &block)
26
+ default, = args
27
+ block ||= proc { default }
28
+ _hook_convert_impl(name, **options) do |before|
29
+ _raw.key?(name) ? before : block.call
30
+ end
31
+ end
32
+
33
+ def _type_convert(klass)
34
+ Kernel.method(klass.to_s.to_sym)
35
+ end
36
+
37
+ def _hook_type(name, klass, **options)
38
+ _hook_convert_impl(name, **options) do |before|
39
+ _type_convert(klass).call(before)
40
+ end
41
+ end
42
+
43
+ def _hook_strip(name, **options)
44
+ _hook_convert_impl(name, **options, &:strip)
45
+ end
46
+
47
+ def _hook_required(name, **options)
48
+ _log(**_options, name: _name(name), **options, severity: Logger::DEBUG)
49
+ return if _raw.key?(name)
50
+ _log(**_options, name: _name(name), severity: Logger::FATAL, **options) do
51
+ 'is not set'
52
+ end
53
+ exit 1
54
+ end
55
+
56
+ def _hook_must_be(name, klass, **options)
57
+ _log(**_options, name: _name(name), **options, severity: Logger::DEBUG)
58
+ return if _raw[name].is_a?(klass)
59
+ _log(**_options, name: _name(name), severity: Logger::FATAL, **options) do
60
+ "must be #{klass} but is #{_raw[name].class}"
61
+ end
62
+ exit 1
63
+ end
64
+
65
+ def _hook_check_undefined_keys(**options)
66
+ _log(**_options, name: _fullname, **options, severity: Logger::DEBUG)
67
+ undefined_keys = _raw.keys - self.class._names
68
+ return if undefined_keys.empty?
69
+ _log(**_options, name: _fullname, severity: Logger::WARN, **options) do
70
+ "has undefined keys: #{undefined_keys.inspect}"
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/crate/raw.rb ADDED
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+ require 'json'
5
+
6
+ module Crate
7
+ class Raw
8
+ REPLACE = {
9
+ "\xe2\x80\x9c" => '"',
10
+ "\xe2\x80\x9d" => '"',
11
+ }.freeze
12
+
13
+ attr_reader :options
14
+
15
+ def gsub_(raw)
16
+ raw.dup.tap do |raw_|
17
+ REPLACE.each do |pattern, replacement|
18
+ raw_.gsub!(pattern, replacement)
19
+ end
20
+ end
21
+ end
22
+
23
+ def log(&block)
24
+ options[:logger].add(options[:severity], &block)
25
+ end
26
+
27
+ def log_if_changed(raw, replaced)
28
+ return if raw == replaced
29
+ log do
30
+ "#{options[:fullname]}: Harmful characters are changed " \
31
+ "from: #{raw.inspect} to: #{replaced.inspect}"
32
+ end
33
+ end
34
+
35
+ def log_if_harmful(replaced)
36
+ bytes = replaced.each_char.reject(&:ascii_only?).map(&:bytes)
37
+ return if bytes.empty?
38
+ log do
39
+ "#{options[:fullname]}: Harmful characters are still remained: " \
40
+ "#{bytes.inspect}"
41
+ end
42
+ end
43
+
44
+ def gsub(raw)
45
+ gsub_(raw).tap do |replaced|
46
+ log_if_changed(raw, replaced)
47
+ log_if_harmful(replaced)
48
+ end
49
+ end
50
+
51
+ def initialize(raw, **options)
52
+ @options = { logger: Logger.new(nil), severity: Logger::WARN, **options }
53
+
54
+ @raw =
55
+ case raw
56
+ when String
57
+ JSON.parse(gsub(raw), symbolize_names: true)
58
+ when Hash
59
+ raw
60
+ else
61
+ raise
62
+ end
63
+ end
64
+
65
+ def to_hash
66
+ @raw
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crate
4
+ VERSION = '0.1.0'
5
+ end
data/lib/crate.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'crate/version'
4
+
5
+ module Crate
6
+ autoload :Raw, 'crate/raw'
7
+ autoload :Hook, 'crate/hook'
8
+ autoload :Crate, 'crate/crate'
9
+ end
metadata ADDED
@@ -0,0 +1,260 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_crate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Minwoo Lee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
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: 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: fasterer
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: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-bundler
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
+ - !ruby/object:Gem::Dependency
84
+ name: guard-fasterer
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: guard-shell
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rake
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rspec-its
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: rubocop
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ description: Crate
210
+ email:
211
+ - ermaker@gmail.com
212
+ executables: []
213
+ extensions: []
214
+ extra_rdoc_files: []
215
+ files:
216
+ - ".fasterer.yml"
217
+ - ".gitignore"
218
+ - ".rspec"
219
+ - ".rubocop.yml"
220
+ - ".rubocop_todo.yml"
221
+ - ".travis.yml"
222
+ - Gemfile
223
+ - Gemfile.lock
224
+ - Guardfile
225
+ - LICENSE.txt
226
+ - README.md
227
+ - Rakefile
228
+ - bin/console
229
+ - bin/setup
230
+ - data_crate.gemspec
231
+ - lib/crate.rb
232
+ - lib/crate/crate.rb
233
+ - lib/crate/hook.rb
234
+ - lib/crate/raw.rb
235
+ - lib/crate/version.rb
236
+ homepage: https://github.com/ermaker/data_crate
237
+ licenses:
238
+ - MIT
239
+ metadata: {}
240
+ post_install_message:
241
+ rdoc_options: []
242
+ require_paths:
243
+ - lib
244
+ required_ruby_version: !ruby/object:Gem::Requirement
245
+ requirements:
246
+ - - ">="
247
+ - !ruby/object:Gem::Version
248
+ version: '0'
249
+ required_rubygems_version: !ruby/object:Gem::Requirement
250
+ requirements:
251
+ - - ">="
252
+ - !ruby/object:Gem::Version
253
+ version: '0'
254
+ requirements: []
255
+ rubyforge_project:
256
+ rubygems_version: 2.7.2
257
+ signing_key:
258
+ specification_version: 4
259
+ summary: Crate
260
+ test_files: []