active_attr 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of active_attr might be problematic. Click here for more details.
- data/.document +1 -0
- data/.gitignore +2 -0
- data/CHANGELOG.md +14 -2
- data/Gemfile +4 -1
- data/Guardfile +5 -4
- data/MIT-LICENSE +1 -1
- data/README.md +52 -1
- data/active_attr.gemspec +2 -2
- data/lib/active_attr.rb +16 -1
- data/lib/active_attr/attribute_definition.rb +52 -0
- data/lib/active_attr/attributes.rb +138 -0
- data/lib/active_attr/basic_model.rb +25 -0
- data/lib/active_attr/error.rb +16 -0
- data/lib/active_attr/mass_assignment.rb +48 -7
- data/lib/active_attr/matchers.rb +13 -0
- data/lib/active_attr/matchers/have_attribute_matcher.rb +69 -0
- data/lib/active_attr/rspec.rb +5 -0
- data/lib/active_attr/strict_mass_assignment.rb +44 -0
- data/lib/active_attr/unknown_attributes_error.rb +18 -0
- data/lib/active_attr/version.rb +3 -1
- data/spec/active_attr/attribute_definition_spec.rb +46 -0
- data/spec/active_attr/attributes_spec.rb +141 -0
- data/spec/active_attr/basic_model_spec.rb +19 -0
- data/spec/active_attr/error_spec.rb +8 -0
- data/spec/active_attr/mass_assignment_spec.rb +7 -95
- data/spec/active_attr/matchers/have_attribute_matcher_spec.rb +103 -0
- data/spec/active_attr/strict_mass_assignment_spec.rb +38 -0
- data/spec/active_attr/unknown_attributes_error_spec.rb +9 -0
- data/spec/active_attr/version_spec.rb +35 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/active_model_lint.rb +1 -1
- data/spec/support/mass_assignment_shared_examples.rb +95 -0
- metadata +44 -14
@@ -0,0 +1,103 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_attr/attributes"
|
3
|
+
require "active_attr/matchers/have_attribute_matcher"
|
4
|
+
|
5
|
+
module ActiveAttr
|
6
|
+
describe Matchers do
|
7
|
+
let(:dsl) { Object.new.extend described_class }
|
8
|
+
subject { dsl }
|
9
|
+
|
10
|
+
it { should respond_to(:have_attribute).with(1).argument }
|
11
|
+
|
12
|
+
describe "#have_attribute" do
|
13
|
+
subject { dsl.have_attribute(:name) }
|
14
|
+
|
15
|
+
it "builds a HaveAttributeMatcher with the given attribute name" do
|
16
|
+
should be_a_kind_of Matchers::HaveAttributeMatcher
|
17
|
+
end
|
18
|
+
|
19
|
+
it "uses the given attribute name to construct the matcher" do
|
20
|
+
subject.attribute_name.should == :name
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module Matchers
|
26
|
+
describe HaveAttributeMatcher do
|
27
|
+
let :model_class do
|
28
|
+
Class.new do
|
29
|
+
include Attributes
|
30
|
+
attribute :name
|
31
|
+
|
32
|
+
def self.to_s
|
33
|
+
"Person"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
subject { positive_matcher }
|
39
|
+
let(:positive_matcher) { described_class.new(:name) }
|
40
|
+
let(:negative_matcher) { described_class.new(:age) }
|
41
|
+
|
42
|
+
it { described_class.should respond_to(:new).with(1).argument }
|
43
|
+
|
44
|
+
describe "#attribute_name" do
|
45
|
+
it "returns the value used to initialize the matcher" do
|
46
|
+
subject.attribute_name.should == :name
|
47
|
+
end
|
48
|
+
|
49
|
+
it "converts the value used to initialize the matcher to a symbol" do
|
50
|
+
described_class.new('name').attribute_name.should == :name
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#description" do
|
55
|
+
it "returns a description appropriate to the expectation" do
|
56
|
+
subject.description.should == "have attribute named name"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#failure_message" do
|
61
|
+
it "returns a failure message appropriate to the expectation and subject" do
|
62
|
+
negative_matcher.tap do |matcher|
|
63
|
+
matcher.matches? model_class
|
64
|
+
end.failure_message.should == "Expected Person to have attribute named age"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#initialize" do
|
69
|
+
it "raises a TypeError when the attribute name does not respond to #to_sym" do
|
70
|
+
expect { described_class.new(Object.new) }.to raise_error(TypeError, "can't convert Object into Symbol")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#matches?" do
|
75
|
+
let(:model_instance) { model_class.new }
|
76
|
+
|
77
|
+
it "is true with an instance of a model class that has the attribute" do
|
78
|
+
positive_matcher.matches?(model_instance).should be_true
|
79
|
+
end
|
80
|
+
|
81
|
+
it "is true with a model class that has the attribute" do
|
82
|
+
positive_matcher.matches?(model_class).should be_true
|
83
|
+
end
|
84
|
+
|
85
|
+
it "is false with an instance of a model class that does not have the attribute" do
|
86
|
+
negative_matcher.matches?(model_instance).should be_false
|
87
|
+
end
|
88
|
+
|
89
|
+
it "is false with a model class that does not have the attribute" do
|
90
|
+
negative_matcher.matches?(model_class).should be_false
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "#negative_failure_message" do
|
95
|
+
it "returns a failure message appropriate to the expectation and subject" do
|
96
|
+
positive_matcher.tap do |matcher|
|
97
|
+
matcher.matches? model_class
|
98
|
+
end.negative_failure_message.should == "Expected Person to not have attribute named name"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_attr/strict_mass_assignment"
|
3
|
+
|
4
|
+
module ActiveAttr
|
5
|
+
describe StrictMassAssignment, :mass_assignment do
|
6
|
+
subject do
|
7
|
+
model_class.class_eval do
|
8
|
+
include StrictMassAssignment
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
shared_examples "strict mass assignment method", :strict_mass_assignment_method => true do
|
13
|
+
include_examples "mass assignment method"
|
14
|
+
|
15
|
+
it "raises when assigning an unknown attribute" do
|
16
|
+
expect do
|
17
|
+
mass_assign_attributes(:middle_initial => "J")
|
18
|
+
end.to raise_error UnknownAttributesError, "unknown attribute(s): middle_initial"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "raises when trying to assign a private attribute" do
|
22
|
+
expect do
|
23
|
+
mass_assign_attributes(:middle_name => "J")
|
24
|
+
end.to raise_error UnknownAttributesError, "unknown attribute(s): middle_name"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "raises when assigning multiple unknown attributes with a message referencing both in alphabetical order" do
|
28
|
+
expect do
|
29
|
+
mass_assign_attributes(:middle_name => "J", :middle_initial => "J")
|
30
|
+
end.to raise_error UnknownAttributesError, "unknown attribute(s): middle_initial, middle_name"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#assign_attributes", :assign_attributes, :strict_mass_assignment_method
|
35
|
+
describe "#attributes=", :attributes=, :strict_mass_assignment_method
|
36
|
+
describe "#initialize", :initialize, :strict_mass_assignment_method
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_attr/version"
|
3
|
+
|
4
|
+
module ActiveAttr
|
5
|
+
describe "ActiveAttr::VERSION" do
|
6
|
+
subject { VERSION }
|
7
|
+
|
8
|
+
it { should be_a_kind_of String }
|
9
|
+
|
10
|
+
describe "is compliant with Semantic Versioning <http://semver.org/>" do
|
11
|
+
let(:gem_version) { Gem::Version.new VERSION }
|
12
|
+
subject { gem_version }
|
13
|
+
|
14
|
+
it { subject.should have(3).segments }
|
15
|
+
|
16
|
+
describe "major version" do
|
17
|
+
subject { gem_version.segments[0] }
|
18
|
+
|
19
|
+
it { should be_a_kind_of Fixnum }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "minor version" do
|
23
|
+
subject { gem_version.segments[1] }
|
24
|
+
|
25
|
+
it { should be_a_kind_of Fixnum }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "patch version" do
|
29
|
+
subject { gem_version.segments[2] }
|
30
|
+
|
31
|
+
it { subject.to_s.should =~ /\d+([A-Za-z][0-9A-Za-z-]*)?/ }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,95 @@
|
|
1
|
+
shared_examples "mass assignment class", :mass_assignment => true do
|
2
|
+
let :model_class do
|
3
|
+
Class.new do
|
4
|
+
attr_accessor :first_name, :last_name, :middle_name
|
5
|
+
private :middle_name=
|
6
|
+
|
7
|
+
def name=(name)
|
8
|
+
self.last_name, self.first_name = name.split(nil, 2).reverse
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:person) { subject.new }
|
14
|
+
let(:first_name) { "Chris" }
|
15
|
+
let(:last_name) { "Griego" }
|
16
|
+
|
17
|
+
def should_assign_names_to(person)
|
18
|
+
person.first_name.should == first_name
|
19
|
+
person.last_name.should == last_name
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
shared_examples "mass assignment method" do
|
24
|
+
it "does not raise when assigning nil attributes" do
|
25
|
+
expect { mass_assign_attributes nil }.not_to raise_error
|
26
|
+
end
|
27
|
+
|
28
|
+
it "assigns all valid attributes when passed as a hash with string keys" do
|
29
|
+
should_assign_names_to mass_assign_attributes('first_name' => first_name, 'last_name' => last_name)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "assigns all valid attributes when passed as a hash with symbol keys" do
|
33
|
+
should_assign_names_to mass_assign_attributes(:first_name => first_name, :last_name => last_name)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "uses any available writer methods" do
|
37
|
+
should_assign_names_to mass_assign_attributes(:name => "#{first_name} #{last_name}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
shared_examples "#assign_attribures", :assign_attributes => true do
|
42
|
+
def mass_assign_attributes(attributes)
|
43
|
+
person.assign_attributes attributes
|
44
|
+
person
|
45
|
+
end
|
46
|
+
|
47
|
+
it "raises ArgumentError when called with three arguments" do
|
48
|
+
expect { subject.new.assign_attributes({}, {}, nil) }.to raise_error ArgumentError
|
49
|
+
end
|
50
|
+
|
51
|
+
it "does not raise when called with two arguments" do
|
52
|
+
expect { subject.new.assign_attributes({}, {}) }.not_to raise_error
|
53
|
+
end
|
54
|
+
|
55
|
+
it "does not raise when called with a single argument" do
|
56
|
+
expect { subject.new.assign_attributes({}) }.not_to raise_error
|
57
|
+
end
|
58
|
+
|
59
|
+
it "raises ArgumentError when called with no arguments" do
|
60
|
+
expect { subject.new.assign_attributes }.to raise_error ArgumentError
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
shared_examples "#attributes=", :attributes= => true do
|
65
|
+
def mass_assign_attributes(attributes)
|
66
|
+
person.attributes = attributes
|
67
|
+
person
|
68
|
+
end
|
69
|
+
|
70
|
+
it "raises ArgumentError when called with two arguments" do
|
71
|
+
expect { person.send(:attributes=, {}, {}) }.to raise_error ArgumentError
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
shared_examples "#initialize", :initialize => true do
|
76
|
+
def mass_assign_attributes(attributes)
|
77
|
+
subject.new(attributes)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "raises ArgumentError when called with three arguments" do
|
81
|
+
expect { subject.new({}, {}, nil) }.to raise_error ArgumentError
|
82
|
+
end
|
83
|
+
|
84
|
+
it "does not raise when called with two arguments" do
|
85
|
+
expect { subject.new({}, {}) }.not_to raise_error
|
86
|
+
end
|
87
|
+
|
88
|
+
it "does not raise when called with a single argument" do
|
89
|
+
expect { subject.new({}) }.not_to raise_error
|
90
|
+
end
|
91
|
+
|
92
|
+
it "does not raise when called with no arguments" do
|
93
|
+
expect { subject.new }.not_to raise_error
|
94
|
+
end
|
95
|
+
end
|
metadata
CHANGED
@@ -1,20 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_attr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Chris Griego
|
9
|
+
- Ben Poweski
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2011-
|
13
|
+
date: 2011-10-03 00:00:00.000000000 -05:00
|
13
14
|
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: activemodel
|
17
|
-
requirement: &
|
18
|
+
requirement: &70212822508240 !ruby/object:Gem::Requirement
|
18
19
|
none: false
|
19
20
|
requirements:
|
20
21
|
- - ~>
|
@@ -22,10 +23,10 @@ dependencies:
|
|
22
23
|
version: '3.1'
|
23
24
|
type: :runtime
|
24
25
|
prerelease: false
|
25
|
-
version_requirements: *
|
26
|
+
version_requirements: *70212822508240
|
26
27
|
- !ruby/object:Gem::Dependency
|
27
28
|
name: activesupport
|
28
|
-
requirement: &
|
29
|
+
requirement: &70212822507140 !ruby/object:Gem::Requirement
|
29
30
|
none: false
|
30
31
|
requirements:
|
31
32
|
- - ~>
|
@@ -33,10 +34,10 @@ dependencies:
|
|
33
34
|
version: '3.1'
|
34
35
|
type: :runtime
|
35
36
|
prerelease: false
|
36
|
-
version_requirements: *
|
37
|
+
version_requirements: *70212822507140
|
37
38
|
- !ruby/object:Gem::Dependency
|
38
39
|
name: bundler
|
39
|
-
requirement: &
|
40
|
+
requirement: &70212822506320 !ruby/object:Gem::Requirement
|
40
41
|
none: false
|
41
42
|
requirements:
|
42
43
|
- - ~>
|
@@ -44,10 +45,10 @@ dependencies:
|
|
44
45
|
version: '1.0'
|
45
46
|
type: :development
|
46
47
|
prerelease: false
|
47
|
-
version_requirements: *
|
48
|
+
version_requirements: *70212822506320
|
48
49
|
- !ruby/object:Gem::Dependency
|
49
50
|
name: rake
|
50
|
-
requirement: &
|
51
|
+
requirement: &70212822505480 !ruby/object:Gem::Requirement
|
51
52
|
none: false
|
52
53
|
requirements:
|
53
54
|
- - ~>
|
@@ -55,10 +56,10 @@ dependencies:
|
|
55
56
|
version: 0.9.0
|
56
57
|
type: :development
|
57
58
|
prerelease: false
|
58
|
-
version_requirements: *
|
59
|
+
version_requirements: *70212822505480
|
59
60
|
- !ruby/object:Gem::Dependency
|
60
61
|
name: rspec
|
61
|
-
requirement: &
|
62
|
+
requirement: &70212822504100 !ruby/object:Gem::Requirement
|
62
63
|
none: false
|
63
64
|
requirements:
|
64
65
|
- - ~>
|
@@ -66,14 +67,16 @@ dependencies:
|
|
66
67
|
version: '2.6'
|
67
68
|
type: :development
|
68
69
|
prerelease: false
|
69
|
-
version_requirements: *
|
70
|
+
version_requirements: *70212822504100
|
70
71
|
description: Create plain old ruby models without reinventing the wheel.
|
71
72
|
email:
|
72
73
|
- cgriego@gmail.com
|
74
|
+
- bpoweski@gmail.com
|
73
75
|
executables: []
|
74
76
|
extensions: []
|
75
77
|
extra_rdoc_files: []
|
76
78
|
files:
|
79
|
+
- .document
|
77
80
|
- .gitignore
|
78
81
|
- .rspec
|
79
82
|
- .rvmrc
|
@@ -86,11 +89,29 @@ files:
|
|
86
89
|
- Rakefile
|
87
90
|
- active_attr.gemspec
|
88
91
|
- lib/active_attr.rb
|
92
|
+
- lib/active_attr/attribute_definition.rb
|
93
|
+
- lib/active_attr/attributes.rb
|
94
|
+
- lib/active_attr/basic_model.rb
|
95
|
+
- lib/active_attr/error.rb
|
89
96
|
- lib/active_attr/mass_assignment.rb
|
97
|
+
- lib/active_attr/matchers.rb
|
98
|
+
- lib/active_attr/matchers/have_attribute_matcher.rb
|
99
|
+
- lib/active_attr/rspec.rb
|
100
|
+
- lib/active_attr/strict_mass_assignment.rb
|
101
|
+
- lib/active_attr/unknown_attributes_error.rb
|
90
102
|
- lib/active_attr/version.rb
|
103
|
+
- spec/active_attr/attribute_definition_spec.rb
|
104
|
+
- spec/active_attr/attributes_spec.rb
|
105
|
+
- spec/active_attr/basic_model_spec.rb
|
106
|
+
- spec/active_attr/error_spec.rb
|
91
107
|
- spec/active_attr/mass_assignment_spec.rb
|
108
|
+
- spec/active_attr/matchers/have_attribute_matcher_spec.rb
|
109
|
+
- spec/active_attr/strict_mass_assignment_spec.rb
|
110
|
+
- spec/active_attr/unknown_attributes_error_spec.rb
|
111
|
+
- spec/active_attr/version_spec.rb
|
92
112
|
- spec/spec_helper.rb
|
93
113
|
- spec/support/active_model_lint.rb
|
114
|
+
- spec/support/mass_assignment_shared_examples.rb
|
94
115
|
has_rdoc: true
|
95
116
|
homepage: https://github.com/cgriego/active_attr
|
96
117
|
licenses: []
|
@@ -106,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
127
|
version: '0'
|
107
128
|
segments:
|
108
129
|
- 0
|
109
|
-
hash:
|
130
|
+
hash: 4452087611638543141
|
110
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
132
|
none: false
|
112
133
|
requirements:
|
@@ -115,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
115
136
|
version: '0'
|
116
137
|
segments:
|
117
138
|
- 0
|
118
|
-
hash:
|
139
|
+
hash: 4452087611638543141
|
119
140
|
requirements: []
|
120
141
|
rubyforge_project: active_attr
|
121
142
|
rubygems_version: 1.5.2
|
@@ -123,6 +144,15 @@ signing_key:
|
|
123
144
|
specification_version: 3
|
124
145
|
summary: What ActiveModel left out
|
125
146
|
test_files:
|
147
|
+
- spec/active_attr/attribute_definition_spec.rb
|
148
|
+
- spec/active_attr/attributes_spec.rb
|
149
|
+
- spec/active_attr/basic_model_spec.rb
|
150
|
+
- spec/active_attr/error_spec.rb
|
126
151
|
- spec/active_attr/mass_assignment_spec.rb
|
152
|
+
- spec/active_attr/matchers/have_attribute_matcher_spec.rb
|
153
|
+
- spec/active_attr/strict_mass_assignment_spec.rb
|
154
|
+
- spec/active_attr/unknown_attributes_error_spec.rb
|
155
|
+
- spec/active_attr/version_spec.rb
|
127
156
|
- spec/spec_helper.rb
|
128
157
|
- spec/support/active_model_lint.rb
|
158
|
+
- spec/support/mass_assignment_shared_examples.rb
|