https://github.com/janjiss/massager
https://github.com/janjiss/massager
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/janjiss/massager
- Owner: janjiss
- License: mit
- Created: 2016-09-09T03:11:01.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-09-21T16:19:16.000Z (over 8 years ago)
- Last Synced: 2024-10-02T19:29:28.708Z (8 months ago)
- Language: Ruby
- Size: 35.2 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Massager
[](https://travis-ci.org/janjiss/massager)
[](https://badge.fury.io/rb/massager)
[](https://gemnasium.com/github.com/janjiss/massager)
[](https://codeclimate.com/github/janjiss/massager)
Have you ever felt a need to massage your data just a little bit before working with it? This is what Massager was built for.## Installation
Add this line to your application's Gemfile:
```ruby
gem 'massager'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install massager
## Simplest usecase
To start using Massager, just include it in your classes, like so:
```ruby
class ExampleClass
include Massager
attribute :foo, "bar"
end
```
In this scenario, the "bar" key's value will become the result `foo` method
```ruby
testable = ExampleClass.call({"bar" => "value"})
testable.foo #=> "value"
```
## Strict schema
You can have required keys defined with `strict: true`
```ruby
class ExampleClass
include Massager
attribute :foo, "bar", strict: true
end
```
It will raise an error if "bar" is not passed:
```ruby
testable = ExampleClass.call({"bar" => "value"})
testable.foo #=> "value"
testable = ExampleClass.call({"baz" => "value"}) #=> raises ArgumentError
```## Type checking
You can also pass type checks using dry-types library:
```ruby
class ExampleClass
include Massager
attribute :foo, "bar", type: Types::Strict::String
end
```
It will raise an error if the type is not correct:
```ruby
testable = ExampleClass.call({"bar" => "value"})
testable.foo #=> "value"
testable = ExampleClass.call({"bar" => 123}) #=> raises Dry::Types::ConstraintError
```
If you want to define your own types, check the Dry Types library. Type needs to respond to `call` method, so
you can define your own## Preprocessing the value via block
You can add bit of preprocessing via block (The type check will be preformed afer the block is executed):
```ruby
class ExampleClass
include Massager
attribute :foo, "bar", type: Types::Strict::String do |v|
v.upcase
end
end
```
And it will have following result
```ruby
testable = ExampleClass.call({"bar" => "value"})
testable.foo #=> "VALUE"
```## Combining multiple keys
```ruby
class ExampleClass
include Massager
attribute :foo, "bar", "baz", type: Types::Strict::String do |bar, baz|
"#{bar} #{baz}"
end
end
```
Note that if you pass multiple keys, the modifier block is mandatory```ruby
testable = ExampleClass.call({"bar" => "bar", "baz" => "baz"})
testable.foo #=> "bar baz"
```## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).