shale-builder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c7749b75b69a25526581d8b071fd93b44a7b6d9cc1a953fec8fbb0815fe3d86a
4
+ data.tar.gz: 30ce0be01b5290da4a5960c1993f3372edc251209477205fcedeb0cc0f4fabd9
5
+ SHA512:
6
+ metadata.gz: 5f846398c26ed0afa0641bb9a912f357d446082ff11d71b738027916c4af16561f8767898d002b3f8af5396fe5f47d1ba31dc6bb3f75e22d847ae41e432ab337
7
+ data.tar.gz: 960fa9d1138ce59b5d859659b5aa713f263831328105d73ee403a631a4fbfe1c3ea34c758a4cecd10d9b01ab5d16db3016aecd2abe6960e3c0ca71cecb095915
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ inherit_gem:
2
+ rubocop-espago: rubocop.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.7
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-02-21
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in diggable.gemspec
6
+ gemspec
7
+
8
+ gem 'minitest', '~> 5.0' # test framework
9
+ gem 'rake', '~> 13.0' # automation tasks
10
+ gem 'rubocop-espago', '~> 1.0' # ruby linter
11
+ gem 'shoulda-context', '~> 2.0' # more pleasant test syntax
12
+ gem 'solargraph', '~> 0.48' # language server
data/Gemfile.lock ADDED
@@ -0,0 +1,88 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ shale-builder (0.1.0)
5
+ shale (< 1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ backport (1.2.0)
12
+ benchmark (0.2.1)
13
+ diff-lcs (1.5.0)
14
+ e2mmap (0.1.0)
15
+ jaro_winkler (1.5.4)
16
+ json (2.6.3)
17
+ kramdown (2.4.0)
18
+ rexml
19
+ kramdown-parser-gfm (1.1.0)
20
+ kramdown (~> 2.0)
21
+ minitest (5.17.0)
22
+ nokogiri (1.14.2-arm64-darwin)
23
+ racc (~> 1.4)
24
+ nokogiri (1.14.2-x86_64-linux)
25
+ racc (~> 1.4)
26
+ parallel (1.22.1)
27
+ parser (3.2.1.0)
28
+ ast (~> 2.4.1)
29
+ racc (1.6.2)
30
+ rainbow (3.1.1)
31
+ rake (13.0.6)
32
+ regexp_parser (2.7.0)
33
+ reverse_markdown (2.1.1)
34
+ nokogiri
35
+ rexml (3.2.5)
36
+ rubocop (1.45.1)
37
+ json (~> 2.3)
38
+ parallel (~> 1.10)
39
+ parser (>= 3.2.0.0)
40
+ rainbow (>= 2.2.2, < 4.0)
41
+ regexp_parser (>= 1.8, < 3.0)
42
+ rexml (>= 3.2.5, < 4.0)
43
+ rubocop-ast (>= 1.24.1, < 2.0)
44
+ ruby-progressbar (~> 1.7)
45
+ unicode-display_width (>= 2.4.0, < 3.0)
46
+ rubocop-ast (1.26.0)
47
+ parser (>= 3.2.1.0)
48
+ rubocop-espago (1.0.2)
49
+ rubocop
50
+ ruby-progressbar (1.11.0)
51
+ shale (0.9.0)
52
+ shoulda-context (2.0.0)
53
+ solargraph (0.48.0)
54
+ backport (~> 1.2)
55
+ benchmark
56
+ bundler (>= 1.17.2)
57
+ diff-lcs (~> 1.4)
58
+ e2mmap
59
+ jaro_winkler (~> 1.5)
60
+ kramdown (~> 2.3)
61
+ kramdown-parser-gfm (~> 1.1)
62
+ parser (~> 3.0)
63
+ reverse_markdown (>= 1.0.5, < 3)
64
+ rubocop (>= 0.52)
65
+ thor (~> 1.0)
66
+ tilt (~> 2.0)
67
+ yard (~> 0.9, >= 0.9.24)
68
+ thor (1.2.1)
69
+ tilt (2.1.0)
70
+ unicode-display_width (2.4.2)
71
+ webrick (1.7.0)
72
+ yard (0.9.28)
73
+ webrick (~> 1.7.0)
74
+
75
+ PLATFORMS
76
+ arm64-darwin-20
77
+ x86_64-linux
78
+
79
+ DEPENDENCIES
80
+ minitest (~> 5.0)
81
+ rake (~> 13.0)
82
+ rubocop-espago (~> 1.0)
83
+ shale-builder!
84
+ shoulda-context (~> 2.0)
85
+ solargraph (~> 0.48)
86
+
87
+ BUNDLED WITH
88
+ 2.4.7
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Mateusz Drewniak
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,179 @@
1
+ # Shale::Builder
2
+
3
+ This addon to the [shale](https://github.com/kgiszczak/shale) Ruby gem adds a simple yet powerful builder DSL.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add shale-builder
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install shale-builder
14
+
15
+ ## Usage
16
+
17
+ ### TLDR
18
+
19
+ Build your [shale](https://github.com/kgiszczak/shale) classes like a boss.
20
+
21
+ ```rb
22
+ transaction = Transaction.build do |t|
23
+ t.cvv_code = '123'
24
+ t.amount do |a|
25
+ a.value = 2.3
26
+ a.currency = 'PLN'
27
+ end
28
+ t.payment_instrument do |p|
29
+ p.number = '4242424242424242'
30
+ p.expiration_year = 2045
31
+ p.expiration_month = 12
32
+ end
33
+ end
34
+ ```
35
+
36
+ ### Build method
37
+
38
+ This gem adds a module named `Shale::Builder`. It's meant to be included
39
+ in subclasses of `Shale::Mapper` to provide an easier way of building
40
+ instances.
41
+
42
+ You can use it like that:
43
+
44
+ ```rb
45
+ require 'shale/builder'
46
+
47
+ class Amount < Shale::Mapper
48
+ include Shale::Builder
49
+
50
+ attribute :value, Shale::Type::Float
51
+ attribute :currency, Shale::Type::String
52
+ end
53
+ ```
54
+
55
+ Now instead of creating an instance like that:
56
+
57
+ ```rb
58
+ amount = Amount.new(value: 2.3, currency: 'PLN')
59
+ ```
60
+
61
+ You can do it like that:
62
+
63
+ ```rb
64
+ amount = Amount.build do |a|
65
+ a.value = 2.3
66
+ a.currency = 'PLN'
67
+ end
68
+ ```
69
+
70
+ ### Building nested objects
71
+
72
+ It's kind of pointless when you've got a flat structure.
73
+ It really shines when nested objects come into play.
74
+
75
+ Let's say that you've got a structure like this:
76
+
77
+ ```rb
78
+ class Amount < Shale::Mapper
79
+ include Shale::Builder
80
+
81
+ attribute :value, Shale::Type::Float
82
+ attribute :currency, Shale::Type::String
83
+ end
84
+
85
+ class PaymentInstrument < Shale::Mapper
86
+ include Shale::Builder
87
+
88
+ attribute :number, Shale::Type::String
89
+ attribute :expiration_year, ::Shale::Type::Integer
90
+ attribute :expiration_month, ::Shale::Type::Integer
91
+ end
92
+
93
+ class Transaction < ::Shale::Mapper
94
+ include Shale::Builder
95
+
96
+ attribute :cvv_code, Shale::Type::String
97
+ attribute :amount, Amount
98
+ attribute :payment_instrument, PaymentInstrument
99
+ end
100
+ ```
101
+
102
+ Normally you would instantiate it like that:
103
+
104
+ ```rb
105
+ transaction = Transaction.new(
106
+ cvv_code: '123',
107
+ amount: Amount.new(
108
+ value: 2.3,
109
+ currency: 'PLN'
110
+ ),
111
+ payment_instrument: PaymentInstrument.new(
112
+ number: '4242424242424242',
113
+ expiration_year: 2045,
114
+ expiration_month: 12
115
+ )
116
+ )
117
+ ```
118
+
119
+ It's really repetitive and it makes it hard to
120
+ modify the values of certain attributes or omit them
121
+ conditionally.
122
+
123
+ This gem provides a better way:
124
+
125
+ ```rb
126
+ transaction = Transaction.build do |t|
127
+ t.cvv_code = '123'
128
+ t.amount do |a|
129
+ a.value = 2.3
130
+ a.currency = 'PLN'
131
+ end
132
+ t.payment_instrument do |p|
133
+ p.number = '4242424242424242'
134
+ p.expiration_year = 2045
135
+ p.expiration_month = 12
136
+ end
137
+ end
138
+ ```
139
+
140
+ That's possible because the getters of attributes with
141
+ non-primitive types have been overridden to accept blocks.
142
+ When a block is given to such a getter, it instantiates an empty object
143
+ of its type and yields it to the block.
144
+
145
+ ### Conditional building
146
+
147
+ This DSL makes it extremely easy to build nested
148
+ objects conditionally.
149
+
150
+ ```rb
151
+ transaction = Transaction.build do |t|
152
+ t.cvv_code = '123'
153
+ t.amount do |a|
154
+ a.value = 2.3 if some_flag?
155
+ a.currency = 'PLN'
156
+ end
157
+ t.payment_instrument do |p|
158
+ p.number = '4242424242424242'
159
+ if some_condition?
160
+ p.expiration_year = 2045
161
+ p.expiration_month = 12
162
+ end
163
+ end
164
+ end
165
+ ```
166
+
167
+ ## Development
168
+
169
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
170
+
171
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
172
+
173
+ ## Contributing
174
+
175
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Verseth/ruby-shale-builder.
176
+
177
+ ## License
178
+
179
+ 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,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ ::Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = ::FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+
14
+ ::RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shale
4
+ module Builder
5
+ # @return [String]
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shale'
4
+
5
+ require_relative 'builder/version'
6
+
7
+ module Shale
8
+ # It's meant to be included in subclasses of `Shale::Mapper`
9
+ # to provide an easier way of building instances.
10
+ #
11
+ # Example:
12
+ #
13
+ # require 'shale/builder'
14
+ #
15
+ # class PaymentInstrument < Shale::Mapper
16
+ # include Shale::Builder
17
+ #
18
+ # attribute :number, Shale::Type::String
19
+ # attribute :expiration_year, ::Shale::Type::Integer
20
+ # attribute :expiration_month, ::Shale::Type::Integer
21
+ # end
22
+ #
23
+ # class Transaction < ::Shale::Mapper
24
+ # include Shale::Builder
25
+ #
26
+ # attribute :cvv_code, Shale::Type::String
27
+ # attribute :payment_instrument, PaymentInstrument
28
+ # end
29
+ #
30
+ # transaction = Transaction.build do |t|
31
+ # t.cvv_code = '123'
32
+ # t.payment_instrument do |p|
33
+ # p.number = '4242424242424242'
34
+ # p.expiration_year = 2045
35
+ # p.expiration_month = 12
36
+ # end
37
+ # end
38
+ #
39
+ module Builder
40
+ class << self
41
+ # Gets called after including this module.
42
+ #
43
+ # @param mod [Module]
44
+ def included(mod)
45
+ mod.extend ClassMethods
46
+ builder_methods_module = ::Module.new
47
+ mod.instance_variable_set :@builder_methods_module, builder_methods_module
48
+ mod.include builder_methods_module
49
+ end
50
+ end
51
+
52
+ # Class methods provided by `Shale::Builder`
53
+ module ClassMethods
54
+ # Contains overridden getter methods for attributes
55
+ # with complex types (so that they accept a block for building)
56
+ #
57
+ # @return [Module]
58
+ attr_reader :builder_methods_module
59
+
60
+ # @return [Class, nil]
61
+ attr_accessor :request_class
62
+
63
+ # @yieldparam [self]
64
+ # @return [self]
65
+ def build
66
+ body = new
67
+ yield(body)
68
+
69
+ body
70
+ end
71
+
72
+ # @param name [String, Symbol]
73
+ # @param type [Class]
74
+ # @return [void]
75
+ def attribute(name, type, *args, **kwargs, &block)
76
+ super
77
+ return unless type < ::Shale::Mapper
78
+
79
+ @builder_methods_module.class_eval <<~RUBY, __FILE__, __LINE__ + 1
80
+ def #{name} # def amount
81
+ return super unless block_given? # return super unless block_given?
82
+ #
83
+ object = #{type}.new # object = Amount.new
84
+ yield(object) # yield(object)
85
+ self.#{name} = object # self.amount = object
86
+ end # end
87
+ RUBY
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,6 @@
1
+ module Shale
2
+ module Builder
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shale-builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mateusz Drewniak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: shale
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "<"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ description: An addon to the shale Ruby gem which adds a simple yet powerful builder
28
+ DSL.
29
+ email:
30
+ - matmg24@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rubocop.yml"
36
+ - ".ruby-version"
37
+ - CHANGELOG.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/shale/builder.rb
44
+ - lib/shale/builder/version.rb
45
+ - sig/shale/builder.rbs
46
+ homepage: https://github.com/Verseth/ruby-shale-builder
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/Verseth/ruby-shale-builder
51
+ source_code_uri: https://github.com/Verseth/ruby-shale-builder
52
+ rubygems_mfa_required: 'true'
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.7.0
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubygems_version: 3.4.7
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: An addon to the shale Ruby gem which adds a simple yet powerful builder DSL.
72
+ test_files: []