-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
661 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
inherit_gem: | ||
rubocop-espago: rubocop.yml | ||
|
||
AllCops: | ||
TargetRubyVersion: 2.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.2.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## [Unreleased] | ||
|
||
## [0.1.0] - 2023-02-21 | ||
|
||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.