personhood 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +19 -0
  4. data/Gemfile.lock +111 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +279 -0
  7. data/Rakefile +55 -0
  8. data/VERSION +1 -0
  9. data/lib/juscribe/display_optional.rb +19 -0
  10. data/lib/juscribe/name.rb +42 -0
  11. data/lib/juscribe/personhood.rb +93 -0
  12. data/lib/juscribe/tos_acceptable.rb +26 -0
  13. data/lib/personhood.rb +10 -0
  14. data/spec/dummy/Rakefile +7 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  16. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  17. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  18. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  19. data/spec/dummy/app/mailers/.gitkeep +0 -0
  20. data/spec/dummy/app/models/.gitkeep +0 -0
  21. data/spec/dummy/app/models/user.rb +3 -0
  22. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  23. data/spec/dummy/config/application.rb +45 -0
  24. data/spec/dummy/config/boot.rb +10 -0
  25. data/spec/dummy/config/database.yml +25 -0
  26. data/spec/dummy/config/environment.rb +5 -0
  27. data/spec/dummy/config/environments/development.rb +30 -0
  28. data/spec/dummy/config/environments/production.rb +60 -0
  29. data/spec/dummy/config/environments/test.rb +39 -0
  30. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  31. data/spec/dummy/config/initializers/inflections.rb +10 -0
  32. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  33. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  34. data/spec/dummy/config/initializers/session_store.rb +8 -0
  35. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  36. data/spec/dummy/config/locales/en.yml +5 -0
  37. data/spec/dummy/config/routes.rb +58 -0
  38. data/spec/dummy/config.ru +4 -0
  39. data/spec/dummy/db/migrate/20121022000000_create_users.rb +19 -0
  40. data/spec/dummy/db/schema.rb +28 -0
  41. data/spec/dummy/db/test.sqlite3 +0 -0
  42. data/spec/dummy/lib/assets/.gitkeep +0 -0
  43. data/spec/dummy/log/.gitkeep +0 -0
  44. data/spec/dummy/log/development.log +20 -0
  45. data/spec/dummy/public/404.html +26 -0
  46. data/spec/dummy/public/422.html +26 -0
  47. data/spec/dummy/public/500.html +26 -0
  48. data/spec/dummy/public/favicon.ico +0 -0
  49. data/spec/dummy/script/rails +6 -0
  50. data/spec/personhood_spec.rb +237 -0
  51. data/spec/spec_helper.rb +23 -0
  52. data/spec/support/have_error_matchers.rb +46 -0
  53. metadata +202 -0
@@ -0,0 +1,237 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'Juscribe::Personhood' do
6
+ ActiveRecord::Base.send(:include, Juscribe::DisplayOptional)
7
+
8
+ describe User do # from spec/dummy app
9
+
10
+ def build(*args)
11
+ attrs = args.extract_options!
12
+ described_class.new(attrs)
13
+ end
14
+
15
+ before(:each) do
16
+ @colin ||= User.new({
17
+ email: 'caleon@gmail.com',
18
+ username: 'colin',
19
+ first_name: 'colin',
20
+ last_name: 'chun',
21
+ middle_name: 'potenza',
22
+ sex: 1,
23
+ birthdate: Date.new(1985, 1, 29)
24
+ }, as: :admin)
25
+ @jimbo ||= User.new({
26
+ email: 'jimbo@juscribe.com',
27
+ username: 'jimbo',
28
+ first_name: 'Jim',
29
+ last_name: 'Bo'
30
+ }, as: :admin)
31
+ @marion = User.new({
32
+ username: 'marion',
33
+ first_name: 'Marion',
34
+ last_name: 'Cotillard',
35
+ sex: 0
36
+ }, as: :admin)
37
+ end
38
+
39
+ shared_context 'with a lowercased name', name: :lowercased do
40
+ subject { @colin }
41
+ end
42
+
43
+ shared_context 'with a normal name', :name do
44
+ subject { @jimbo }
45
+ end
46
+
47
+ shared_context 'with email', :email do
48
+ subject { @colin }
49
+ end
50
+
51
+ shared_context 'with birthdate', :birthdate do
52
+ subject { @colin }
53
+ end
54
+
55
+ shared_context 'with male sex', sex: :male do
56
+ subject { @colin }
57
+ end
58
+
59
+ shared_context 'with female sex', sex: :female do
60
+ subject { @marion }
61
+ end
62
+
63
+ describe '#first_and_last_name' do
64
+
65
+ context 'with no name' do
66
+ its(:first_and_last_name) { should == '(unnamed)' }
67
+ end
68
+
69
+ context 'with a lowercased name', name: :lowercased do
70
+ its(:first_and_last_name) { should == 'colin chun' }
71
+ end
72
+
73
+ context 'with a normal name', :name do
74
+ its(:first_and_last_name) { should == 'Jim Bo' }
75
+ end
76
+ end
77
+
78
+ describe '#full_name' do
79
+
80
+ context 'with no name' do
81
+ its(:full_name) { should == '(unnamed)' }
82
+ end
83
+
84
+ context 'with a lowercased name', name: :lowercased do
85
+ its(:full_name) { should == 'colin p. chun' }
86
+ end
87
+
88
+ context 'with a normal name', :name do
89
+ its(:full_name) { should == 'Jim Bo' }
90
+ end
91
+ end
92
+
93
+ describe '#email_address' do
94
+
95
+ context 'with no email' do
96
+ its(:email_address) { should be_nil }
97
+ end
98
+
99
+ context 'with a lowercased name', name: :lowercased do
100
+ its(:email_address) { should == 'colin chun <caleon@gmail.com>' }
101
+ end
102
+
103
+ context 'with a normal name', :name do
104
+ its(:email_address) { should == 'Jim Bo <jimbo@juscribe.com>' }
105
+ end
106
+ end
107
+
108
+ describe '#to_s' do
109
+
110
+ context 'with no name' do
111
+ its(:username) { should be_nil }
112
+ its(:to_s) { should == '(unnamed)' }
113
+ end
114
+
115
+ context 'with a lowercased name', name: :lowercased do
116
+ its(:username) { should == 'colin' }
117
+ its(:to_s) { should == @colin.username }
118
+ end
119
+
120
+ context 'with a normal name', :name do
121
+ its(:username) { should == 'jimbo' }
122
+ its(:to_s) { should == @jimbo.username }
123
+ end
124
+ end
125
+
126
+ describe '#sex' do
127
+
128
+ context 'with no sex' do
129
+ its([:sex]) { should be_nil }
130
+ its(:sex) { should == '?' }
131
+ specify { subject.sex(:full).should == '(unknown)' }
132
+ end
133
+
134
+ context 'with male sex', sex: :male do
135
+ its([:sex]) { should == 1 }
136
+ its(:sex) { should == 'm' }
137
+ specify { subject.sex(:full).should == 'male' }
138
+ end
139
+
140
+ context 'with female sex', sex: :female do
141
+ its([:sex]) { should == 0}
142
+ its(:sex) { should == 'f' }
143
+ specify { subject.sex(:full).should == 'female' }
144
+ end
145
+ end
146
+
147
+ describe '#male?|#female?|#androgynous?' do
148
+
149
+ context 'with no sex' do
150
+ it { should_not be_male }
151
+ it { should_not be_female }
152
+ it { should be_androgynous }
153
+ end
154
+
155
+ context 'with male sex', sex: :male do
156
+ it { should be_male }
157
+ it { should_not be_female }
158
+ it { should_not be_androgynous }
159
+ end
160
+
161
+ context 'with female sex', sex: :female do
162
+ it { should be_female }
163
+ it { should_not be_male }
164
+ it { should_not be_androgynous }
165
+ end
166
+ end
167
+
168
+ describe '#birthdate' do
169
+
170
+ context 'with no birthdate' do
171
+ its(:birthdate) { should == Date.today }
172
+ end
173
+
174
+ context 'with birthdate', :birthdate do
175
+ its(:birthdate) { should be_a_kind_of Date }
176
+ its('birthdate.year') { should == 1985 }
177
+ its('birthdate.month') { should == 1 }
178
+ its('birthdate.day') { should == 29 }
179
+ end
180
+ end
181
+
182
+ describe '#age' do
183
+
184
+ context 'with no birthdate' do
185
+ its(:age) { should == 0 }
186
+ end
187
+
188
+ context 'with birthdate', :birthdate do
189
+ its(:age) do
190
+ years_difference = Date.today.year - @colin.birthdate.year
191
+ bday_this_year = @colin.birthdate + years_difference.years
192
+ age = years_difference - (bday_this_year > Date.today ? 1 : 0)
193
+ should == age
194
+ end
195
+ end
196
+ end
197
+
198
+ describe 'validations' do
199
+
200
+ it 'should reject a blank first_name' do
201
+ build(:user, first_name: nil).should have_error(:blank).on(:first_name)
202
+ build(:user, first_name: '' ).should have_error(:blank).on(:first_name)
203
+ build(:user, first_name: ' ').should have_error(:blank).on(:first_name)
204
+ end
205
+
206
+ # it 'should reject a first_name which includes numerics' do
207
+ # user = build(:user, first_name: 'j0hn')
208
+ # user.should_not be_valid
209
+ # user.should have_error(:regexp).on(:first_name)
210
+ # end
211
+
212
+ # it 'should reject a first_name with symbols and spaces' do
213
+ # user = build(:user, first_name: 'mbaT !@ ahs!')
214
+ # user.should_not be_valid
215
+ # user.should have_error(:regexp).on(:first_name)
216
+ # end
217
+
218
+ it 'should reject a blank last_name' do
219
+ build(:user, last_name: nil).should have_error(:blank).on(:last_name)
220
+ build(:user, last_name: '' ).should have_error(:blank).on(:last_name)
221
+ build(:user, last_name: ' ').should have_error(:blank).on(:last_name)
222
+ end
223
+
224
+ # it 'should reject a last_name which includes numerics' do
225
+ # user = build(:user, last_name: 'sm1th')
226
+ # user.should_not be_valid
227
+ # user.should have_error(:regexp).on(:last_name)
228
+ # end
229
+
230
+ # it 'should reject a last_name with symbols' do
231
+ # user = build(:user, last_name: 'mbaT!@ahs!')
232
+ # user.should_not be_valid
233
+ # user.should have_error(:regexp).on(:last_name)
234
+ # end
235
+ end
236
+ end
237
+ end
@@ -0,0 +1,23 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
4
+ require "rails/test_help"
5
+
6
+ Rails.backtrace_cleaner.remove_silencers!
7
+
8
+ require 'rspec'
9
+ require 'personhood'
10
+ require 'rspec/rails'
11
+
12
+ # Requires supporting files with custom matchers and macros, etc,
13
+ # in ./support/ and its subdirectories.
14
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
15
+
16
+ RSpec.configure do |config|
17
+ # config.mock_with :rspec
18
+ config.treat_symbols_as_metadata_keys_with_true_values = true
19
+ config.filter_run focus: true
20
+ config.run_all_when_everything_filtered = true
21
+ config.alias_it_should_behave_like_to :it_has_behavior, 'has behavior:'
22
+ config.include HaveErrorMatchers
23
+ end
@@ -0,0 +1,46 @@
1
+ module HaveErrorMatchers
2
+ class HaveError
3
+ def initialize(ya, *type_and_opts)
4
+ @ya = ya
5
+ @options = type_and_opts.extract_options!
6
+ @type = type_and_opts.shift || :invalid
7
+ end
8
+
9
+ def on(attribute)
10
+ @attribute = attribute
11
+ return self
12
+ end
13
+
14
+ def matches?(record)
15
+ @record = record
16
+ @record.send(:run_validations!) unless @options.delete(:skip_validate)
17
+ @record.errors[@attribute].include?(@record.errors.generate_message(@attribute, @type, @options))
18
+ end
19
+
20
+ def failure_message_for_should
21
+ "Expected #{@record.class.model_name.human} record to have an error of type :#@type for attribute #{@record.class.human_attribute_name(@attribute)}."
22
+ end
23
+
24
+ def failure_message_for_should_not
25
+ "Expected #{@record.class.model_name.human} record to NOT have an error of type :#@type for attribute #{@record.class.human_attribute_name(@attribute)}."
26
+ end
27
+
28
+ def description
29
+ ["have a :#@type validation error",
30
+ *("on :#@attribute" if @attribute)].join(' ')
31
+ end
32
+ end
33
+
34
+ def have_error(*type_and_opts)
35
+ HaveError.new(true, *type_and_opts)
36
+ end
37
+
38
+ # RSpec::Matchers.define :have_error do |*type_and_opts|
39
+ # match do |actual|
40
+ # opts = type_and_opts.extract_options!
41
+ # type = type_and_opts.shift || :invalid
42
+ # actual.send(:run_validations!) unless opts.delete(:skip_validate)
43
+ # actual.errors # WIP
44
+ # end
45
+ # end
46
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: personhood
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - caleon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.11'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.11'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rdoc
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.12'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3.12'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: jeweler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.4
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.4
78
+ - !ruby/object:Gem::Dependency
79
+ name: rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 3.1.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 3.1.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ! " When you are tired of coding the same kinds of things for your
111
+ User model\n (or any other person-like model) with all its typical `first_name`,\n
112
+ \ `full_name`, and other brouhaha, use the Personhood gem to clean up your\n code
113
+ by getting rid of those pesky lines and instead focusing on the lines of\n code
114
+ that *truly* set your app apart.\n"
115
+ email: caleon@gmail.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files:
119
+ - LICENSE.txt
120
+ - README.rdoc
121
+ files:
122
+ - .document
123
+ - .rspec
124
+ - Gemfile
125
+ - Gemfile.lock
126
+ - LICENSE.txt
127
+ - README.rdoc
128
+ - Rakefile
129
+ - VERSION
130
+ - lib/juscribe/display_optional.rb
131
+ - lib/juscribe/name.rb
132
+ - lib/juscribe/personhood.rb
133
+ - lib/juscribe/tos_acceptable.rb
134
+ - lib/personhood.rb
135
+ - spec/dummy/Rakefile
136
+ - spec/dummy/app/assets/javascripts/application.js
137
+ - spec/dummy/app/assets/stylesheets/application.css
138
+ - spec/dummy/app/controllers/application_controller.rb
139
+ - spec/dummy/app/helpers/application_helper.rb
140
+ - spec/dummy/app/mailers/.gitkeep
141
+ - spec/dummy/app/models/.gitkeep
142
+ - spec/dummy/app/models/user.rb
143
+ - spec/dummy/app/views/layouts/application.html.erb
144
+ - spec/dummy/config.ru
145
+ - spec/dummy/config/application.rb
146
+ - spec/dummy/config/boot.rb
147
+ - spec/dummy/config/database.yml
148
+ - spec/dummy/config/environment.rb
149
+ - spec/dummy/config/environments/development.rb
150
+ - spec/dummy/config/environments/production.rb
151
+ - spec/dummy/config/environments/test.rb
152
+ - spec/dummy/config/initializers/backtrace_silencers.rb
153
+ - spec/dummy/config/initializers/inflections.rb
154
+ - spec/dummy/config/initializers/mime_types.rb
155
+ - spec/dummy/config/initializers/secret_token.rb
156
+ - spec/dummy/config/initializers/session_store.rb
157
+ - spec/dummy/config/initializers/wrap_parameters.rb
158
+ - spec/dummy/config/locales/en.yml
159
+ - spec/dummy/config/routes.rb
160
+ - spec/dummy/db/migrate/20121022000000_create_users.rb
161
+ - spec/dummy/db/schema.rb
162
+ - spec/dummy/db/test.sqlite3
163
+ - spec/dummy/lib/assets/.gitkeep
164
+ - spec/dummy/log/.gitkeep
165
+ - spec/dummy/log/development.log
166
+ - spec/dummy/public/404.html
167
+ - spec/dummy/public/422.html
168
+ - spec/dummy/public/500.html
169
+ - spec/dummy/public/favicon.ico
170
+ - spec/dummy/script/rails
171
+ - spec/personhood_spec.rb
172
+ - spec/spec_helper.rb
173
+ - spec/support/have_error_matchers.rb
174
+ homepage: http://github.com/caleon/personhood
175
+ licenses:
176
+ - MIT
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ none: false
183
+ requirements:
184
+ - - ! '>='
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ segments:
188
+ - 0
189
+ hash: -4137017533771121721
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ none: false
192
+ requirements:
193
+ - - ! '>='
194
+ - !ruby/object:Gem::Version
195
+ version: '0'
196
+ requirements: []
197
+ rubyforge_project:
198
+ rubygems_version: 1.8.24
199
+ signing_key:
200
+ specification_version: 3
201
+ summary: ActiveRecord abstraction for attributes pertaining to a person-like model
202
+ test_files: []