spine-parameters 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd9ed99df2da99181265828c269cfecc00c05fae
4
+ data.tar.gz: 214f867131978a0d58f1cd1a699d7b71a3652fdf
5
+ SHA512:
6
+ metadata.gz: c29db06623b7408446f3820bfac76ec0536b4d24c3a5d4aef5b61218e9581f47f58aa1de0e88e18108ac69ec0d120902bfd0d69f6c5d0a0aff7ef2b94ecbe296
7
+ data.tar.gz: ef7fb9ed94c8cce1a475a110cc4fe831230cc4d315655d33e75a60b4336b454f02180af08c658aaba510abbc628a259c1b5d1cb0429215cf96a436132834f894
data/.gitignore ADDED
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /bin/
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ # Gemfile.lock
32
+ # .ruby-version
33
+ # .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require config/default
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spine-parameters.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ spine-parameters (0.1.0)
5
+ spine-symbolize (~> 0.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ rake (10.4.2)
12
+ rspec (3.2.0)
13
+ rspec-core (~> 3.2.0)
14
+ rspec-expectations (~> 3.2.0)
15
+ rspec-mocks (~> 3.2.0)
16
+ rspec-core (3.2.3)
17
+ rspec-support (~> 3.2.0)
18
+ rspec-expectations (3.2.1)
19
+ diff-lcs (>= 1.2.0, < 2.0)
20
+ rspec-support (~> 3.2.0)
21
+ rspec-mocks (3.2.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.2.0)
24
+ rspec-support (3.2.2)
25
+ spine-symbolize (0.1.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.7)
32
+ rake (~> 10.0)
33
+ rspec (~> 3.2)
34
+ spine-parameters!
data/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ Copyright (c) 2015, TOGGL LLC
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+
8
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9
+
10
+ 3. Neither the name of the TOGGL LLC nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11
+
12
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Spine::Parameters
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/spine-parameters.svg)](http://badge.fury.io/rb/spine-parameters)
4
+ [![Dependency Status](https://gemnasium.com/rspine/parameters.svg)](https://gemnasium.com/rspine/parameters)
5
+ [![Code Climate](https://codeclimate.com/github/rspine/parameters/badges/gpa.svg)](https://codeclimate.com/github/rspine/parameters)
6
+ [ ![Codeship Status for rspine/parameters](https://codeship.com/projects/c3d614e0-e11c-0132-df07-763d3ae4d225/status?branch=master)](https://codeship.com/projects/81066)
7
+
8
+ Validates and converts parameters. Also it converts all keys to symbols.
9
+
10
+ ## Installation
11
+
12
+ To install it, add the gem to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'spine-parameters'
16
+ ```
17
+
18
+ Then run `bundle`. If you're not using Bundler, just `gem install spine-parameters`.
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ parameters = Spine::Parameters::Collection.new(parameters_containing_hash)
24
+ parameters.required(:name)
25
+ .integer(:id)
26
+ .timestamp(:since)
27
+
28
+ parameters.errors? # Checks if there is any parsing error.
29
+ parameters.errors
30
+ # => { id: ['invalid number'] }
31
+
32
+ parameters[:since]
33
+ # => <DateTime: 2014-01-01T12:00:00+00:00>
34
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,38 @@
1
+ require 'spine/symbolize'
2
+
3
+ module Spine
4
+ module Parameters
5
+ class Collection
6
+ include Syntax::Timestamp
7
+ include Syntax::Integer
8
+ include Syntax::Boolean
9
+ include Syntax::Constraint
10
+ include Syntax::Required
11
+ include Errors
12
+
13
+ attr_reader :raw, :values
14
+
15
+ def initialize(raw)
16
+ @raw = raw
17
+ @values = {}
18
+ end
19
+
20
+ def [](key)
21
+ values.fetch(key.to_sym) { symbolized[key.to_sym] }
22
+ end
23
+
24
+ def symbolized
25
+ @symbolized ||= Symbolize.keys(raw)
26
+ end
27
+
28
+ private
29
+
30
+ def parse(name, parser, &block)
31
+ key = name.to_sym
32
+ values[key] = parser.parse(key, symbolized[key]) if symbolized[key]
33
+ values[key] ||= block.call if block_given?
34
+ self
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ module Spine
2
+ module Parameters
3
+ module Errors
4
+ def errors
5
+ @errors ||= {}
6
+ end
7
+
8
+ def add_error(field, message)
9
+ errors[field] = [message]
10
+ end
11
+
12
+ def errors?
13
+ !errors.empty?
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Spine
2
+ module Parameters
3
+ module Parsers
4
+ module Boolean
5
+ extend self
6
+
7
+ def parse(name, value)
8
+ return nil unless value
9
+ return true if value == true || value =~ (/^(true|t|yes|y|1)$/i)
10
+ return false if value == false || value.empty? || value =~ (/^(false|f|no|n|0)$/i)
11
+
12
+ raise ArgumentError.new
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module Spine
2
+ module Parameters
3
+ module Parsers
4
+ module Integer
5
+ extend self
6
+
7
+ def parse(name, value)
8
+ return nil unless value
9
+
10
+ Integer(value.to_s, 10)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ module Spine
2
+ module Parameters
3
+ module Parsers
4
+ module Timestamp
5
+ extend self
6
+
7
+ def parse(name, value)
8
+ return nil unless value
9
+ return value if value.is_a?(Date)
10
+
11
+ DateTime.iso8601(value.to_s)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module Spine
2
+ module Parameters
3
+ module Syntax
4
+ module Boolean
5
+ def boolean(name, options = {}, &block)
6
+ parse(name, Parsers::Boolean, &block)
7
+ rescue ArgumentError
8
+ add_error(name, 'is invalid truth value.')
9
+ self
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module Spine
2
+ module Parameters
3
+ module Syntax
4
+ module Constraint
5
+ def constraint(name, message, &block)
6
+ add_error(name, message) if block.call(symbolized[name])
7
+ self
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ module Spine
2
+ module Parameters
3
+ module Syntax
4
+ module Integer
5
+ def integer(name, options = {}, &block)
6
+ parse(name, Parsers::Integer, &block)
7
+ rescue ArgumentError
8
+ add_error(name, 'is invalid number.')
9
+ self
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module Spine
2
+ module Parameters
3
+ module Syntax
4
+ module Required
5
+ def required(name)
6
+ constraint(name.to_sym, 'is required') { |value| value.nil? }
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ module Spine
2
+ module Parameters
3
+ module Syntax
4
+ module Timestamp
5
+ def timestamp(name, options = {}, &block)
6
+ parse(name, Parsers::Timestamp, &block)
7
+ rescue ArgumentError
8
+ add_error(name, 'is invalid date.')
9
+ self
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module Spine
2
+ module Parameters
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ module Spine
2
+ module Parameters
3
+ autoload :Errors, 'spine/parameters/errors'
4
+ autoload :Collection, 'spine/parameters/collection'
5
+
6
+ module Syntax
7
+ autoload :Constraint, 'spine/parameters/syntax/constraint'
8
+ autoload :Integer, 'spine/parameters/syntax/integer'
9
+ autoload :Timestamp, 'spine/parameters/syntax/timestamp'
10
+ autoload :Boolean, 'spine/parameters/syntax/boolean'
11
+ autoload :Required, 'spine/parameters/syntax/required'
12
+ end
13
+
14
+ module Parsers
15
+ autoload :Integer, 'spine/parameters/parsers/integer'
16
+ autoload :Timestamp, 'spine/parameters/parsers/timestamp'
17
+ autoload :Boolean, 'spine/parameters/parsers/boolean'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ RSpec.configure do |config|
2
+ config.filter_run :focus
3
+ config.run_all_when_everything_filtered = true
4
+
5
+ if config.files_to_run.one?
6
+ config.default_formatter = 'doc'
7
+ end
8
+
9
+ config.order = :random
10
+
11
+ Kernel.srand config.seed
12
+
13
+ config.expect_with :rspec do |expectations|
14
+ expectations.syntax = :expect
15
+ end
16
+
17
+ config.mock_with :rspec do |mocks|
18
+ mocks.syntax = :expect
19
+ end
20
+ end
@@ -0,0 +1,83 @@
1
+ require 'ostruct'
2
+ require 'date'
3
+ require 'spine/parameters'
4
+
5
+ module Spine
6
+ module Parameters
7
+ describe Parameters do
8
+ subject { Parameters::Collection.new(parameters) }
9
+ let(:parameters){
10
+ {
11
+ "selected_account_id"=>"1",
12
+ "some_timestamp" => "2015-01-12T10:42:36+02:00",
13
+ "google_login" => "true"
14
+ }
15
+ }
16
+
17
+ it "symbolizes keys" do
18
+ expect(subject[:selected_account_id]).to be
19
+ expect(subject[:some_timestamp]).to be
20
+ end
21
+
22
+ context "when parsing integer" do
23
+ it "returns valid integer" do
24
+ subject.integer(:selected_account_id)
25
+ expect(subject[:selected_account_id]).to eq(1)
26
+ end
27
+
28
+ it "has errors if integer not parsed" do
29
+ subject.integer(:some_timestamp)
30
+ expect(subject.errors?).to be
31
+ end
32
+ end
33
+
34
+ context "when parsing timestamp" do
35
+ it "parses timestamp" do
36
+ subject.timestamp(:some_timestamp)
37
+ expect(subject[:some_timestamp].is_a?(DateTime)).to be
38
+ expect(subject.errors?).not_to be
39
+ end
40
+
41
+ it "has errors if timestamp not parsed" do
42
+ subject.timestamp(:selected_account_id)
43
+ expect(subject.errors?).to be
44
+ end
45
+ end
46
+
47
+ context "when parsing boolean" do
48
+ it "returns valid boolean value" do
49
+ subject.boolean(:google_login)
50
+ expect(subject[:google_login]).to eq(true)
51
+ end
52
+
53
+ it "has errors if not parsed" do
54
+ subject.boolean(:some_timestamp)
55
+ expect(subject.errors?).to be
56
+ end
57
+ end
58
+
59
+ it "initializes value if block present" do
60
+ date = Date.today.prev_month(6)
61
+ subject.timestamp(:since) { date }
62
+ expect(subject[:since]).to eq(date)
63
+ end
64
+
65
+ it "doesn't add key to values, if it's not present in raw data" do
66
+ subject.integer(:beginning_of_week)
67
+ expect(subject.values).to eq({})
68
+ end
69
+
70
+ context "with required constraint" do
71
+ it "has an error, if key is missing" do
72
+ subject.required(:beginning_of_week)
73
+ expect(subject.errors?).to be
74
+ end
75
+
76
+ it "has no errors if key exists" do
77
+ subject.required(:some_timestamp)
78
+ expect(subject.errors?).not_to be
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spine/parameters/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spine-parameters"
8
+ spec.version = Spine::Parameters::VERSION
9
+ spec.authors = ["TOGGL LLC"]
10
+ spec.email = ["support@toggl.com"]
11
+ spec.summary = 'Parameters validation and coversion.'
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/rspine/parameters'
14
+ spec.license = 'BSD-3-Clause'
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_dependency 'spine-symbolize', "~> 0.1"
22
+
23
+ spec.add_development_dependency 'bundler', "~> 1.7"
24
+ spec.add_development_dependency 'rake', "~> 10.0"
25
+ spec.add_development_dependency 'rspec', "~> 3.2"
26
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spine-parameters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TOGGL LLC
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: spine-symbolize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: ''
70
+ email:
71
+ - support@toggl.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - lib/spine/parameters.rb
84
+ - lib/spine/parameters/collection.rb
85
+ - lib/spine/parameters/errors.rb
86
+ - lib/spine/parameters/parsers/boolean.rb
87
+ - lib/spine/parameters/parsers/integer.rb
88
+ - lib/spine/parameters/parsers/timestamp.rb
89
+ - lib/spine/parameters/syntax/boolean.rb
90
+ - lib/spine/parameters/syntax/constraint.rb
91
+ - lib/spine/parameters/syntax/integer.rb
92
+ - lib/spine/parameters/syntax/required.rb
93
+ - lib/spine/parameters/syntax/timestamp.rb
94
+ - lib/spine/parameters/version.rb
95
+ - spec/config/default.rb
96
+ - spec/spine/parameters/parameters_spec.rb
97
+ - spine-parameters.gemspec
98
+ homepage: https://github.com/rspine/parameters
99
+ licenses:
100
+ - BSD-3-Clause
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.5
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Parameters validation and coversion.
122
+ test_files:
123
+ - spec/config/default.rb
124
+ - spec/spine/parameters/parameters_spec.rb