ParamsCollector
Installation
Add this line to your application's Gemfile:
gem 'params_collector'
And then execute:
$ bundle
Or install it yourself as:
$ gem install params_collector
Usage
require 'params_collector'
parser = ParamsCollector.expect do
# <marshaler> <param_name> <default_value>
boolean :option1
boolean :option2, false
number :num, 4
string :desc
end
params = ... e.g. from request ... -> { option1: true, desc: "foo" }
parser.parse(params)
then you can check if all expected parameters were parsed
parser.valid? # => true
and use that object in the same way as a hash
parser[:option1] # => true
parser[:option2] # => false
parser[:num] # => 4
parser[:desc] # => "foo"
parser.to_hash # => { option1: true, desc: "foo" }
parser.merge num: 2 # => { option1: true, page: 2, desc: "foo" }
If you call to_hash method, the output will contain only parameters that are
valid, or where the default state was changed.
If you define the default_value (e.g. number :page, 1), then the parser
will be valid even if you do not specify e.g.{ page: 123 } to the
parse method.
Marshalers
When you call ParamsCollector.expect, you should define, which parameters you
expect to be parsed. You can do that by calling:
boolean :name, [default_value = false]- converts: true|false, "yes|no", "on|off", 1|0 to theTrueClassorFalseClassnumber :name, [default_value = 0]- converts digits and string with digits to theFixnumorFloatstring :name, [default_value = ""]hash :name, [default_value = {}]array :name, [default_value = []]
Creating the own marshaler
Marshaler class should respond to:
.valueand return stored value.set(value)to set new value
and register self in the ParamsCollector:
ParamsCollector::Parser.register_marshaler("foo", self.name)
so:
class FooMarshaler
ParamsCollector::Parser.register_marshaler("foo", name)
attr_reader :value
def initialize
@value = nil
end
def set(value)
@value = value
end
end
Versioning
See semver.org
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request