Skip to content

Commit

Permalink
Implement Shale::Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Feb 21, 2023
1 parent d06df1b commit 5b17caf
Show file tree
Hide file tree
Showing 18 changed files with 661 additions and 7 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI Ruby

on:
push:
branches: [ 'main', 'develop' ]
pull_request:
branches: [ 'main', 'develop' ]

jobs:
test:
runs-on: ubuntu-latest
env:
CI: true
strategy:
matrix:
ruby-version: ['2.7', '3.0', '3.1', '3.2']
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Ruby and gems
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- name: Run unit tests
run: bundle exec rake test

lint:
runs-on: ubuntu-latest
env:
CI: true
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Ruby and gems
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true
- name: Lint Ruby files
run: bundle exec rubocop --parallel
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
5 changes: 5 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
inherit_gem:
rubocop-espago: rubocop.yml

AllCops:
TargetRubyVersion: 2.7
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.2.1
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [Unreleased]

## [0.1.0] - 2023-02-21

- Initial release
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in diggable.gemspec
gemspec

gem 'minitest', '~> 5.0' # test framework
gem 'rake', '~> 13.0' # automation tasks
gem 'rubocop-espago', '~> 1.0' # ruby linter
gem 'shoulda-context', '~> 2.0' # more pleasant test syntax
gem 'solargraph', '~> 0.48' # language server
88 changes: 88 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
PATH
remote: .
specs:
shale-builder (0.1.0)
shale (< 1.0)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
backport (1.2.0)
benchmark (0.2.1)
diff-lcs (1.5.0)
e2mmap (0.1.0)
jaro_winkler (1.5.4)
json (2.6.3)
kramdown (2.4.0)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
minitest (5.17.0)
nokogiri (1.14.2-arm64-darwin)
racc (~> 1.4)
nokogiri (1.14.2-x86_64-linux)
racc (~> 1.4)
parallel (1.22.1)
parser (3.2.1.0)
ast (~> 2.4.1)
racc (1.6.2)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.7.0)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
rubocop (1.45.1)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.24.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.26.0)
parser (>= 3.2.1.0)
rubocop-espago (1.0.2)
rubocop
ruby-progressbar (1.11.0)
shale (0.9.0)
shoulda-context (2.0.0)
solargraph (0.48.0)
backport (~> 1.2)
benchmark
bundler (>= 1.17.2)
diff-lcs (~> 1.4)
e2mmap
jaro_winkler (~> 1.5)
kramdown (~> 2.3)
kramdown-parser-gfm (~> 1.1)
parser (~> 3.0)
reverse_markdown (>= 1.0.5, < 3)
rubocop (>= 0.52)
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
thor (1.2.1)
tilt (2.1.0)
unicode-display_width (2.4.2)
webrick (1.7.0)
yard (0.9.28)
webrick (~> 1.7.0)

PLATFORMS
arm64-darwin-20
x86_64-linux

DEPENDENCIES
minitest (~> 5.0)
rake (~> 13.0)
rubocop-espago (~> 1.0)
shale-builder!
shoulda-context (~> 2.0)
solargraph (~> 0.48)

BUNDLED WITH
2.4.7
10 changes: 5 additions & 5 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MIT License
The MIT License (MIT)

Copyright (c) 2023 Mateusz Drewniak

Expand All @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
181 changes: 179 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,179 @@
# ruby-shale-builder
An addon to the shale Ruby gem which adds a simple yet powerful builder DSL.
# Shale::Builder

This addon to the [shale](https://github.com/kgiszczak/shale) Ruby gem adds a simple yet powerful builder DSL.

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add shale-builder

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install shale-builder

## Usage

### TLDR

Build your [shale](https://github.com/kgiszczak/shale) classes like a boss.

```rb
transaction = Transaction.build do |t|
t.cvv_code = '123'
t.amount do |a|
a.value = 2.3
a.currency = 'PLN'
end
t.payment_instrument do |p|
p.number = '4242424242424242'
p.expiration_year = 2045
p.expiration_month = 12
end
end
```

### Build method

This gem adds a module named `Shale::Builder`. It's meant to be included
in subclasses of `Shale::Mapper` to provide an easier way of building
instances.

You can use it like that:

```rb
require 'shale/builder'

class Amount < Shale::Mapper
include Shale::Builder

attribute :value, Shale::Type::Float
attribute :currency, Shale::Type::String
end
```

Now instead of creating an instance like that:

```rb
amount = Amount.new(value: 2.3, currency: 'PLN')
```

You can do it like that:

```rb
amount = Amount.build do |a|
a.value = 2.3
a.currency = 'PLN'
end
```

### Building nested objects

It's kind of pointless when you've got a flat structure.
It really shines when nested objects come into play.

Let's say that you've got a structure like this:

```rb
class Amount < Shale::Mapper
include Shale::Builder

attribute :value, Shale::Type::Float
attribute :currency, Shale::Type::String
end

class PaymentInstrument < Shale::Mapper
include Shale::Builder

attribute :number, Shale::Type::String
attribute :expiration_year, ::Shale::Type::Integer
attribute :expiration_month, ::Shale::Type::Integer
end

class Transaction < ::Shale::Mapper
include Shale::Builder

attribute :cvv_code, Shale::Type::String
attribute :amount, Amount
attribute :payment_instrument, PaymentInstrument
end
```

Normally you would instantiate it like that:

```rb
transaction = Transaction.new(
cvv_code: '123',
amount: Amount.new(
value: 2.3,
currency: 'PLN'
),
payment_instrument: PaymentInstrument.new(
number: '4242424242424242',
expiration_year: 2045,
expiration_month: 12
)
)
```

It's really repetitive and it makes it hard to
modify the values of certain attributes or omit them
conditionally.

This gem provides a better way:

```rb
transaction = Transaction.build do |t|
t.cvv_code = '123'
t.amount do |a|
a.value = 2.3
a.currency = 'PLN'
end
t.payment_instrument do |p|
p.number = '4242424242424242'
p.expiration_year = 2045
p.expiration_month = 12
end
end
```

That's possible because the getters of attributes with
non-primitive types have been overridden to accept blocks.
When a block is given to such a getter, it instantiates an empty object
of its type and yields it to the block.

### Conditional building

This DSL makes it extremely easy to build nested
objects conditionally.

```rb
transaction = Transaction.build do |t|
t.cvv_code = '123'
t.amount do |a|
a.value = 2.3 if some_flag?
a.currency = 'PLN'
end
t.payment_instrument do |p|
p.number = '4242424242424242'
if some_condition?
p.expiration_year = 2045
p.expiration_month = 12
end
end
end
```

## Development

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.

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).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Verseth/ruby-shale-builder.

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
16 changes: 16 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'

::Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.libs << 'lib'
t.test_files = ::FileList['test/**/*_test.rb']
end

require 'rubocop/rake_task'

::RuboCop::RakeTask.new

task default: %i[test rubocop]
Loading

0 comments on commit 5b17caf

Please sign in to comment.