params_parser 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d24234708daa8a03a4abed4d0a508f6a8e0ab848
4
+ data.tar.gz: 80ab678e77efac3da06f05ab08db29dad02d8460
5
+ SHA512:
6
+ metadata.gz: 821ea85578fcd59e18bc68e50f013e49c9583455fdd1b65ec594284ec2ffb0979a219401b223949b9d1464d45d995d72b70b3bd5a9f8e6d8c01a823c461016ba
7
+ data.tar.gz: 4195539451e165ada152bf02df604633c68022dda2d1c29426c5f2db71d4d8c3c286920492e815acae3f856648672497cfea6681ac915701ff77b525ece77b7f
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pete Swan
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # ParamsParser
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'params_parser'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install params_parser
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/gustly/params_parser/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,54 @@
1
+ module ParamsParser
2
+ module Transforms
3
+ ID = lambda { |x| x }.freeze
4
+ end
5
+
6
+ class Parser
7
+ def initialize(config)
8
+ @config = Hash[config.map do |(key, param_config)|
9
+ [key, ParamConfig.new(key, param_config)]
10
+ end]
11
+ end
12
+
13
+ def parse(params)
14
+ params.class.new.tap do |parsed_params|
15
+ config.each do |(key, param_config)|
16
+ if params[key]
17
+ parsed_params[param_config.key] = param_config.transform.call(params[key])
18
+ end
19
+
20
+ parsed_params[param_config.key] ||= param_config.default if param_config.has_default?
21
+ end
22
+ end
23
+ end
24
+
25
+ def to_proc
26
+ public_method(:parse)
27
+ end
28
+
29
+ private
30
+
31
+ attr_reader :config
32
+ end
33
+
34
+ class ParamConfig
35
+ def initialize(key, config)
36
+ @config = config
37
+ @transform = config.fetch(:transform, Transforms::ID)
38
+ @key = config.fetch(:map_to, key)
39
+ @default = config[:default]
40
+ end
41
+
42
+ attr_reader :transform
43
+ attr_reader :key
44
+ attr_reader :default
45
+
46
+ def has_default?
47
+ config.has_key?(:default)
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :config
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module ParamsParser
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'params_parser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "params_parser"
8
+ spec.version = ParamsParser::VERSION
9
+ spec.authors = ["Peter Swan", "Todd Mohney"]
10
+ spec.email = ["pdswan@gmail.com", "todd@gust.com"]
11
+ spec.summary = %q{Parse Parameters with key mapping, transformation, and defaults.}
12
+ spec.description = %q{Parse Parameters with key mapping, transformation, and defaults.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,88 @@
1
+ require "support/spec_helper"
2
+
3
+ describe ParamsParser::Parser do
4
+ subject(:params_parser) { ParamsParser::Parser.new(config) }
5
+
6
+ describe "#to_proc" do
7
+ let(:config) { { } }
8
+ specify { expect(params_parser.to_proc).to eq(params_parser.public_method(:parse)) }
9
+ end
10
+
11
+ describe "#parse" do
12
+ subject(:parsed) { params_parser.parse(parameters) }
13
+
14
+ describe "limiting keys in the parameters" do
15
+ let(:config) { { configured_key: { } } }
16
+ let(:parameters) { { not_configured_key: double } }
17
+
18
+ it "discards unconfigured keys" do
19
+ expect(parsed.keys).to_not include(:not_configured_key)
20
+ end
21
+ end
22
+
23
+ describe "mapping keys" do
24
+ let(:config) { { a: { map_to: :b } } }
25
+ let(:parameters) { { a: double(:value_at_a) } }
26
+
27
+ specify do
28
+ expect(parsed).to_not have_key(:a)
29
+ expect(parsed[:b]).to eq(parameters[:a])
30
+ end
31
+ end
32
+
33
+ describe "transforms" do
34
+ context "the key has a transform" do
35
+ let(:config) { { int: { transform: :to_i.to_proc } } }
36
+ let(:parameters) { { int: "10" } }
37
+
38
+ it "transforms the value using the transform callable" do
39
+ expect(parsed[:int]).to eq(10)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "defaults" do
45
+ context "a default is not specified" do
46
+ let(:config) { { key_without_default: { } } }
47
+
48
+ context "the key is not in the parameters" do
49
+ let(:parameters) { { } }
50
+
51
+ it "does not add the key to the parameters" do
52
+ expect(parsed).to_not include(:key_without_default)
53
+ end
54
+ end
55
+ end
56
+
57
+ context "a default is specified" do
58
+ let(:default) { double(:default) }
59
+ let(:config) { { key_with_default: { default: default } } }
60
+
61
+ context "the key is not in the parameters" do
62
+ let(:parameters) { { } }
63
+
64
+ it "sets the parameter to the default" do
65
+ expect(parsed[:key_with_default]).to eq(default)
66
+ end
67
+ end
68
+
69
+ context "the key is in the parameters, but falsey" do
70
+ let(:parameters) { { key_with_default: nil } }
71
+
72
+ it "sets the parameter to the default" do
73
+ expect(parsed[:key_with_default]).to eq(default)
74
+ end
75
+ end
76
+
77
+ context "the key is in the parameters, but the transform returns a falsey value" do
78
+ let(:config) { { key_with_default: { default: default, transform: proc { nil } } } }
79
+ let(:parameters) { { key_with_default: double(:anything) } }
80
+
81
+ it "sets the parameter to the default" do
82
+ expect(parsed[:key_with_default]).to eq(default)
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,36 @@
1
+ require "support/spec_helper"
2
+
3
+ describe "Nested Parsers" do
4
+ subject(:parser) { ParamsParser::Parser.new(config) }
5
+
6
+ let(:config) do
7
+ {
8
+ a: { transform: :upcase.to_proc },
9
+ b: {
10
+ transform: ParamsParser::Parser.new(
11
+ c: { default: "c" },
12
+ d: { transform: :upcase.to_proc }
13
+ ).to_proc
14
+ }
15
+ }
16
+ end
17
+
18
+ let(:params) do
19
+ {
20
+ a: "a",
21
+ b: {
22
+ d: "d"
23
+ }
24
+ }
25
+ end
26
+
27
+ specify do
28
+ expect(parser.parse(params)).to eq({
29
+ a: "A",
30
+ b: {
31
+ c: "c",
32
+ d: "D"
33
+ }
34
+ })
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ require "rspec"
2
+
3
+ require "params_parser"
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: params_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Swan
8
+ - Todd Mohney
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.5'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.5'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rspec
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '3.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '3.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: Parse Parameters with key mapping, transformation, and defaults.
57
+ email:
58
+ - pdswan@gmail.com
59
+ - todd@gust.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitignore
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/params_parser.rb
70
+ - lib/params_parser/version.rb
71
+ - params_parser.gemspec
72
+ - spec/params_parser/parser_spec.rb
73
+ - spec/params_parser/use_cases/nested_parsers_spec.rb
74
+ - spec/support/spec_helper.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Parse Parameters with key mapping, transformation, and defaults.
99
+ test_files:
100
+ - spec/params_parser/parser_spec.rb
101
+ - spec/params_parser/use_cases/nested_parsers_spec.rb
102
+ - spec/support/spec_helper.rb