shoulda-hanami 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 101720cc41b943ff7160efeaba7e94f78dc468b7
4
+ data.tar.gz: 18ad04099097a8d19155baf4d383627ba7e2a3b1
5
+ SHA512:
6
+ metadata.gz: c12c4b306fd9330d8ed1eb5fe8d0aa494b007082d3cf83ac84b678ac6655d378d2d3f3f3d2ba2c00478ef03f25e3d776cb8ac9609faf7d1fc8ea25f1471236a6
7
+ data.tar.gz: 253a548d75cb1292c0e6832a84175359a0c77c7d141e9118588cecd04af4eb7d6bb54fa4367499706a331f6f266af66280e84aac5a25cc097e15db4c1965f0bc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,35 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - 2.1.1
6
+ - 2.1.2
7
+ - 2.1.3
8
+ - 2.1.4
9
+ - 2.1.5
10
+ - 2.1.6
11
+ - 2.1.7
12
+ - 2.1.8
13
+ - 2.2.0
14
+ - 2.2.1
15
+ - 2.2.2
16
+ - 2.2.3
17
+ - 2.2.4
18
+ - 2.3.0
19
+ - rbx-2
20
+ - jruby-9000
21
+ - jruby-9.0.1.0
22
+ - jruby-9.0.3.0
23
+ - jruby-head
24
+ - ruby-head
25
+ matrix:
26
+ allow_failures:
27
+ - rvm: rbx-2
28
+ - rvm: jruby-head
29
+ - rvm: ruby-head
30
+ - rvm: jruby-9000
31
+ - rvm: jruby-9.0.1.0
32
+ - rvm: jruby-9.0.3.0
33
+ before_install: gem install bundler -v 1.10.5
34
+ env:
35
+ - TRAVIS=true
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --no-private
2
+ --protected
3
+ --readme README.md
4
+ --files LICENSE.txt
5
+ --markup markdown
6
+ --hide-tag return
7
+ --hide-tag param
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'codeclimate-test-reporter'
5
+
6
+ # documentation
7
+ gem 'inch'
8
+ gem 'yard'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Leonardo Saraiva
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # shoulda-hanami
2
+ [![Gem Version](https://badge.fury.io/rb/shoulda-hanami.svg)](http://badge.fury.io/rb/shoulda-hanami) [![Build Status](https://travis-ci.org/mcorp/shoulda-hanami.svg?branch=master)](https://travis-ci.org/mcorp/shoulda-hanami) [![Code Climate](https://codeclimate.com/github/mcorp/shoulda-hanami/badges/gpa.svg)](https://codeclimate.com/github/mcorp/shoulda-hanami) [![Test Coverage](https://codeclimate.com/github/mcorp/shoulda-hanami/badges/coverage.svg)](https://codeclimate.com/github/mcorp/shoulda-hanami/coverage) [![Dependency Status](https://gemnasium.com/mcorp/shoulda-hanami.svg)](https://gemnasium.com/mcorp/shoulda-hanami) [![Inline docs](http://inch-ci.org/github/mcorp/shoulda-hanami.svg?branch=master)](http://inch-ci.org/github/mcorp/shoulda-hanami)
3
+
4
+ Making tests easy on the fingers and eyes, but on hanami.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'shoulda-hanami'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install shoulda-hanami
21
+
22
+ ## Usage
23
+
24
+ ### Entity
25
+
26
+ ```ruby
27
+ class Person
28
+ include Hanami::Validations
29
+
30
+ attribute :email, type: String, presence: true, format: /@/
31
+ attribute :name, type: String, size: 5..50
32
+ attribute :password, type: String, size: 10
33
+ attribute :birthday, type: Date
34
+ attribute :created_at, type: DateTime
35
+ attribute :state, type: String, inclusion: %w(PR SC SP)
36
+ attribute :year, type: Integer, inclusion: 1979..1990
37
+ end
38
+ ```
39
+
40
+ ### Spec
41
+ ```ruby
42
+ # allow_value
43
+ it { is_expected.to allow_value("leo@nospam.org").for(:email) }
44
+ it { is_expected.to_not allow_value('leo-at-nospam.org').for(:email) }
45
+
46
+ # presence
47
+ it { is_expected.to validate_presence_of(:email) }
48
+
49
+ # size
50
+ it { is_expected.to validate_length_of(:name).is_at_least(5).is_at_most(50) }
51
+ it { is_expected.to validate_length_of(:password).is_equal_to(10) }
52
+
53
+ # coerces
54
+ it { is_expected.to coerce_attribute(:email).to(String) }
55
+ it { is_expected.to coerce_attribute(:name).to(String) }
56
+ it { is_expected.to coerce_attribute(:password).to(String) }
57
+ it { is_expected.to coerce_attribute(:birthday).to(Date) }
58
+ it { is_expected.to coerce_attribute(:created_at).to(DateTime) }
59
+
60
+ # inclusion
61
+ it { is_expected.to validate_inclusion_of(:state).in_array(%w(PR SC SP)) }
62
+ it { is_expected.to validate_inclusion_of(:year).in_array(1979..1990) }
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ 1. Fork it
68
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
69
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
70
+ 4. Push to the branch (`git push origin my-new-feature`)
71
+ 5. Create new Pull Request
72
+
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,37 @@
1
+ module Shoulda
2
+ module Hanami
3
+ module Matchers
4
+ def allow_value(value)
5
+ AllowValueMatcher.new(value)
6
+ end
7
+
8
+ class AllowValueMatcher
9
+ def initialize(value)
10
+ @value = value
11
+ end
12
+
13
+ def matches?(target)
14
+ target.send("#{@attribute}=", @value)
15
+ !Matcher.new(target, @attribute, :format).matches?
16
+ end
17
+
18
+ def description
19
+ "allow '#{@value}' to be set to '#{@attribute}'"
20
+ end
21
+
22
+ def failure_message
23
+ "'#{@value}' is an invalid format for '#{@attribute}'"
24
+ end
25
+
26
+ def failure_message_when_negated
27
+ "'#{@value}' is a valid format for '#{@attribute}'"
28
+ end
29
+
30
+ def for(attribute)
31
+ @attribute = attribute
32
+ self
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,53 @@
1
+ module Shoulda
2
+ module Hanami
3
+ module Matchers
4
+ def coerce_attribute(attribute)
5
+ CoerceAttributeMatcher.new(attribute)
6
+ end
7
+
8
+ class CoerceAttributeMatcher
9
+ TYPES = {
10
+ 'Array' => [1, [1]],
11
+ 'BigDecimal' => ['x', nil],
12
+ 'Boolean' => ['0303', false],
13
+ 'Date' => [false, nil],
14
+ 'DateTime' => ['x', nil],
15
+ 'Float' => ['x', nil],
16
+ 'Hash' => ['ç', nil],
17
+ 'Integer' => ['x', nil],
18
+ 'Pathname' => [true, nil],
19
+ 'Set' => [nil, Set.new],
20
+ 'String' => [1, '1'],
21
+ 'Symbol' => [Hash.new, nil],
22
+ 'Time' => ['uy', nil],
23
+ }
24
+
25
+ def initialize(attribute)
26
+ @attribute = attribute
27
+ end
28
+
29
+ def matches?(target)
30
+ target.send("#{@attribute}=", TYPES[@type.to_s].first)
31
+ target.send(@attribute) == TYPES[@type.to_s].last
32
+ end
33
+
34
+ def description
35
+ "coerce '#{@attribute}' to '#{@type}'"
36
+ end
37
+
38
+ def failure_message
39
+ "does coerce '#{@attribute}' to '#{@type}'"
40
+ end
41
+
42
+ def failure_message_when_negated
43
+ "does not coerce '#{@attribute}' to '#{@type}'"
44
+ end
45
+
46
+ def to(type)
47
+ @type = type
48
+ self
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,44 @@
1
+ module Shoulda
2
+ module Hanami
3
+ module Matchers
4
+ def validate_inclusion_of(attribute)
5
+ ValidateInclusionOfMatcher.new(attribute)
6
+ end
7
+
8
+ class ValidateInclusionOfMatcher
9
+ def initialize(attribute)
10
+ @attribute = attribute
11
+ end
12
+
13
+ def matches?(target)
14
+ value = nil
15
+
16
+ loop do
17
+ value = SecureRandom.hex
18
+ break unless @values.include? value
19
+ end
20
+
21
+ target.send("#{@attribute}=", value)
22
+ Matcher.new(target, @attribute, :inclusion).matches?
23
+ end
24
+
25
+ def description
26
+ "inclusion only '#{@values.inspect}' values on '#{@attribute}'"
27
+ end
28
+
29
+ def failure_message
30
+ "'#{@attribute}' is include only '#{@values.inspect}'"
31
+ end
32
+
33
+ def failure_message_when_negated
34
+ "'#{@attribute}' is not include only '#{@values.inspect}'"
35
+ end
36
+
37
+ def in_array(values)
38
+ @values = values
39
+ self
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,64 @@
1
+ module Shoulda
2
+ module Hanami
3
+ module Matchers
4
+ def validate_length_of(attribute)
5
+ ValidateLengthOfMatcher.new(attribute)
6
+ end
7
+
8
+ class ValidateLengthOfMatcher
9
+ def initialize(attribute)
10
+ @attribute = attribute
11
+ end
12
+
13
+ def matches?(target)
14
+ errors = []
15
+
16
+ target.send("#{@attribute}=", '*' * (minimum - 1))
17
+ errors << Matcher.new(target, @attribute, :size).matches?
18
+
19
+ target.send("#{@attribute}=", '*' * (maximum + 1))
20
+ errors << Matcher.new(target, @attribute, :size).matches?
21
+
22
+ errors.all? { |error| error }
23
+ end
24
+
25
+ def description
26
+ "'#{@attribute}' size between '#{@minimum}..#{@maximum}'"
27
+ end
28
+
29
+ def failure_message
30
+ "'#{@attribute}' is not between '#{@minimum}..#{@maximum}'"
31
+ end
32
+
33
+ def failure_message_when_negated
34
+ "'#{@attribute}' is between '#{@minimum}..#{@maximum}'"
35
+ end
36
+
37
+ def is_at_least(minimum)
38
+ @minimum = minimum
39
+ self
40
+ end
41
+
42
+ def is_at_most(maximum)
43
+ @maximum = maximum
44
+ self
45
+ end
46
+
47
+ def is_equal_to(limit)
48
+ @minimum, @maximum = limit
49
+ self
50
+ end
51
+
52
+ private
53
+
54
+ def minimum
55
+ @minimum ||= 1
56
+ end
57
+
58
+ def maximum
59
+ @maximum ||= 1
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,31 @@
1
+ module Shoulda
2
+ module Hanami
3
+ module Matchers
4
+ def validate_presence_of(attribute)
5
+ ValidatePresenceOfMatcher.new(attribute)
6
+ end
7
+
8
+ class ValidatePresenceOfMatcher
9
+ def initialize(attribute)
10
+ @attribute = attribute
11
+ end
12
+
13
+ def matches?(target)
14
+ Matcher.new(target, @attribute, :presence).matches?
15
+ end
16
+
17
+ def description
18
+ "require '#{@attribute}' to be set"
19
+ end
20
+
21
+ def failure_message
22
+ "'#{@attribute}' is a required attribute"
23
+ end
24
+
25
+ def failure_message_when_negated
26
+ "'#{@attribute}' is not a required attribute"
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ require 'shoulda/hanami/matchers/coerce_attribute_matcher'
2
+ require 'shoulda/hanami/matchers/allow_value_matcher'
3
+ require 'shoulda/hanami/matchers/validate_inclusion_of_matcher'
4
+ require 'shoulda/hanami/matchers/validate_length_of_matcher'
5
+ require 'shoulda/hanami/matchers/validate_presence_of_matcher'
6
+
7
+ module Shoulda
8
+ module Hanami
9
+ module Matchers
10
+ class Matcher
11
+ def initialize(target, attribute, validation)
12
+ @target = target
13
+ @attribute = attribute
14
+ @validation = validation
15
+ end
16
+
17
+ def matches?
18
+ @target.valid?
19
+ @target.errors.for(@attribute).select { |error| error.attribute == @attribute.to_s && error.validation == @validation }.size > 0
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module Shoulda
2
+ module Hanami
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require 'shoulda/hanami/version'
2
+ require 'shoulda/hanami/matchers'
3
+
4
+ module Shoulda
5
+ module Hanami
6
+ end
7
+ 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 'shoulda/hanami/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'shoulda-hanami'
8
+ spec.version = Shoulda::Hanami::VERSION
9
+ spec.authors = ['Leonardo Saraiva']
10
+ spec.email = ['vyper@maneh.org']
11
+
12
+ spec.summary = %q{Making tests easy on the fingers and eyes, but on hanami.}
13
+ spec.homepage = 'https://github.com/mcorp/shoulda-hanami'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.4.0'
24
+
25
+ spec.add_dependency 'hanami-validations', '~> 0.5.0'
26
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shoulda-hanami
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leonardo Saraiva
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.4.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.4.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: hanami-validations
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.0
69
+ description:
70
+ email:
71
+ - vyper@maneh.org
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - ".yardopts"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/shoulda/hanami.rb
85
+ - lib/shoulda/hanami/matchers.rb
86
+ - lib/shoulda/hanami/matchers/allow_value_matcher.rb
87
+ - lib/shoulda/hanami/matchers/coerce_attribute_matcher.rb
88
+ - lib/shoulda/hanami/matchers/validate_inclusion_of_matcher.rb
89
+ - lib/shoulda/hanami/matchers/validate_length_of_matcher.rb
90
+ - lib/shoulda/hanami/matchers/validate_presence_of_matcher.rb
91
+ - lib/shoulda/hanami/version.rb
92
+ - shoulda-hanami.gemspec
93
+ homepage: https://github.com/mcorp/shoulda-hanami
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.5.1
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Making tests easy on the fingers and eyes, but on hanami.
117
+ test_files: []
118
+ has_rdoc: