fortify 0.0.1 → 0.0.2
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.
- data/README.rdoc +23 -13
- data/lib/fortify/fortify.rb +41 -6
- data/spec/lib_specs/fortify_spec.rb +95 -21
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -59,16 +59,13 @@ First, install fortify by adding the following line to your config/environment.r
|
|
59
59
|
|
60
60
|
config.gem :fortify, :lib => 'fortify', :source => 'http://gemcutter.org'
|
61
61
|
|
62
|
-
Next, go back to your book model and add some fortification to it
|
62
|
+
Next, go back to your book model and add some fortification to it. You can do this in one of three ways. Let's first assume that
|
63
|
+
we just want to make sure the Book model has "author", "title", and "genre" attributes.
|
63
64
|
|
64
65
|
# app/models/book.rb
|
65
66
|
class Book < ActiveResource::Base
|
66
|
-
fortify do |default_attributes|
|
67
|
-
default_attributes.author
|
68
|
-
default_attributes.title
|
69
|
-
default_attributes.genre
|
70
|
-
end
|
71
67
|
self.site = ''
|
68
|
+
fortify :author, :title, :genre
|
72
69
|
end
|
73
70
|
|
74
71
|
Now, your controller / views as you originally specified them will work, since now:
|
@@ -77,19 +74,32 @@ Now, your controller / views as you originally specified them will work, since n
|
|
77
74
|
console> b.attributes.inspect
|
78
75
|
==> {:author => nil, :title => nil, :genre => nil}
|
79
76
|
|
80
|
-
|
77
|
+
What if, however, we wanted default values for each of those attributes?
|
81
78
|
|
82
79
|
# app/models/book.rb
|
83
80
|
class Book < ActiveResource::Base
|
84
|
-
fortify do |default_attributes|
|
85
|
-
default_attributes.author = 'Anonymous'
|
86
|
-
default_attributes.title = 'Untitled'
|
87
|
-
default_attributes.genre
|
88
|
-
end
|
89
81
|
self.site = ''
|
82
|
+
fortify :author => 'Anonymous', :genre => 'Unknown', :title => 'Untitled'
|
90
83
|
end
|
91
84
|
|
92
85
|
In this case, a Book.new would give you:
|
93
86
|
console> b = Book.new
|
94
87
|
console> b.attributes.inspect
|
95
|
-
==> {:author => 'Anonymous', :title => 'Untitled', :genre =>
|
88
|
+
==> {:author => 'Anonymous', :title => 'Untitled', :genre => 'Unknown'}
|
89
|
+
|
90
|
+
Lastly, what if we only wanted a default value for the :author attribute? Try this:
|
91
|
+
# app/models/book.rb
|
92
|
+
class Book < ActiveResource::Base
|
93
|
+
self.site = ''
|
94
|
+
fortify do |attrs|
|
95
|
+
attrs.author = 'Anonymous'
|
96
|
+
attrs.title
|
97
|
+
attrs.genre
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
Console would report something like this:
|
102
|
+
console> b = Book.new
|
103
|
+
console> b.attributes.inspect
|
104
|
+
==> {:author => 'Anonymous', :title => nil, :genre => nil}
|
105
|
+
|
data/lib/fortify/fortify.rb
CHANGED
@@ -26,20 +26,55 @@ ActiveResource::Base.instance_eval do
|
|
26
26
|
name[-1..-1] == '=' ? name[0..-2] : name
|
27
27
|
end
|
28
28
|
end
|
29
|
+
|
30
|
+
class BlockArgumentError < ArgumentError; end
|
29
31
|
|
30
|
-
class BlockRequiredError < ArgumentError; end
|
31
32
|
|
32
|
-
|
33
|
+
def fortify(*args, &block)
|
34
|
+
if block
|
35
|
+
process_block(block)
|
36
|
+
elsif passed_a_hash(args)
|
37
|
+
process_hash(args.first)
|
38
|
+
elsif passed_one_or_more_nonhash_parameters(args)
|
39
|
+
process_symbols(args)
|
40
|
+
end
|
41
|
+
end
|
33
42
|
|
43
|
+
def default_attributes
|
44
|
+
@default_attributes ||= DefaultAttributes.new
|
45
|
+
end
|
34
46
|
|
35
|
-
|
36
|
-
|
47
|
+
private
|
48
|
+
def process_hash(args)
|
49
|
+
args.each do |attr_name, attr_value|
|
50
|
+
default_attributes.send(
|
51
|
+
(attr_name.to_s + '=').to_sym,
|
52
|
+
attr_value
|
53
|
+
)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def process_block(block)
|
37
58
|
raise BlockArgumentError.new("the block you pass to fortify must accept one argument") unless block.arity == 1
|
38
59
|
block.call default_attributes
|
39
60
|
end
|
40
61
|
|
41
|
-
def
|
42
|
-
|
62
|
+
def passed_a_hash(args)
|
63
|
+
args.length == 1 && args.first.kind_of?(Hash)
|
64
|
+
end
|
65
|
+
|
66
|
+
def passed_one_or_more_nonhash_parameters(args)
|
67
|
+
args.respond_to?(:length) &&
|
68
|
+
(args.length > 1 || (args.length == 1 && args.first.kind_of?(Symbol)))
|
69
|
+
end
|
70
|
+
|
71
|
+
def process_symbols(args)
|
72
|
+
raise ArgumentError.new("if you pass several parameters to fortify, they must all be symbols") unless args_are_symbols(args)
|
73
|
+
args.map {|attribute| default_attributes.send attribute}
|
74
|
+
end
|
75
|
+
|
76
|
+
def args_are_symbols(args)
|
77
|
+
args.inject(true) {|bool, current_arg| bool && current_arg.class == Symbol}
|
43
78
|
end
|
44
79
|
end
|
45
80
|
|
@@ -25,38 +25,112 @@ describe "ActiveResource::Base --" do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
describe "fortify class method" do
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
describe "when passed a block" do
|
29
|
+
it "should not require a block be passed to it" do
|
30
|
+
proc { Book.fortify }.should_not raise_error
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
|
33
|
+
it "should require the block passed to it accept one argument" do
|
34
|
+
proc { Book.fortify {} }.should raise_error(BlockArgumentError)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should pass a default attributes class to the block" do
|
38
|
+
Book.fortify do |default_attributes|
|
39
|
+
default_attributes.class.should == DefaultAttributes
|
40
|
+
end
|
41
|
+
end
|
34
42
|
end
|
35
43
|
|
36
|
-
|
37
|
-
|
38
|
-
|
44
|
+
describe "when passed one or more nonhash parameters" do
|
45
|
+
it "should throw an error unless all the paramters are symbols" do
|
46
|
+
proc { Book.fortify :author, :genre, :isbn }.should_not raise_error
|
47
|
+
proc { Book.fortify :author, :isbn, :genre => 'fiction'}.should raise_error(ArgumentError)
|
48
|
+
proc { Book.fortify :author}.should_not raise_error
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should add nil default attributes for each symbol passed to it" do
|
52
|
+
Book.fortify :author, :genre, :isbn
|
53
|
+
Book.default_attributes.attributes.has_key?(:author).should be_true
|
54
|
+
Book.default_attributes.attributes[:author].should be_nil
|
55
|
+
Book.default_attributes.attributes.has_key?(:genre).should be_true
|
56
|
+
Book.default_attributes.attributes[:genre].should be_nil
|
57
|
+
Book.default_attributes.attributes.has_key?(:isbn).should be_true
|
58
|
+
Book.default_attributes.attributes[:isbn].should be_nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "when passed a single hash" do
|
63
|
+
it "should not throw an error" do
|
64
|
+
proc {Book.fortify :author => 'Anonymous'}.should_not raise_error
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should add default attributes based on the hash" do
|
68
|
+
Book.fortify :author => 'Anonymous', :genre => 'fiction'
|
69
|
+
Book.default_attributes.attributes.has_key?(:author).should be_true
|
70
|
+
Book.default_attributes.attributes[:author].should == 'Anonymous'
|
71
|
+
Book.default_attributes.attributes.has_key?(:genre).should be_true
|
72
|
+
Book.default_attributes.attributes[:genre].should == 'fiction'
|
39
73
|
end
|
40
74
|
end
|
41
75
|
end
|
42
76
|
|
43
77
|
describe "new" do
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
78
|
+
describe "when fortified with a block" do
|
79
|
+
before do
|
80
|
+
Book.fortify do |default_attributes|
|
81
|
+
default_attributes.author = 'Anonymous'
|
82
|
+
default_attributes.genre = 'Fiction'
|
83
|
+
default_attributes.isbn
|
84
|
+
end
|
49
85
|
end
|
50
86
|
|
51
|
-
|
87
|
+
it "should use the default attributes created via fortify" do
|
88
|
+
b = Book.new
|
89
|
+
|
90
|
+
proc {b.author}.should_not raise_error
|
91
|
+
b.author.should == 'Anonymous'
|
92
|
+
proc {b.genre}.should_not raise_error
|
93
|
+
b.genre.should == 'Fiction'
|
94
|
+
proc {b.isbn}.should_not raise_error
|
95
|
+
b.isbn.should be_nil
|
96
|
+
proc {b.attribute_not_appearing_in_this_test}.should raise_error
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "when fortified with symbols" do
|
101
|
+
before do
|
102
|
+
Book.fortify :author, :genre, :isbn
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should use the default attributes created via fortify" do
|
106
|
+
b = Book.new
|
107
|
+
proc {b.author}.should_not raise_error
|
108
|
+
b.author.should be_nil
|
109
|
+
proc {b.genre}.should_not raise_error
|
110
|
+
b.genre.should be_nil
|
111
|
+
proc {b.isbn}.should_not raise_error
|
112
|
+
b.isbn.should be_nil
|
113
|
+
proc {b.attribute_not_appearing_in_this_test}.should raise_error
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "when fortified with a hash" do
|
118
|
+
before do
|
119
|
+
Book.fortify :author => 'Anonymous', :genre => 'Fiction', :isbn => '0000'
|
120
|
+
end
|
52
121
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
122
|
+
it "should use the default attributes created via fortify" do
|
123
|
+
b = Book.new
|
124
|
+
|
125
|
+
proc {b.author}.should_not raise_error
|
126
|
+
b.author.should == 'Anonymous'
|
127
|
+
proc {b.genre}.should_not raise_error
|
128
|
+
b.genre.should == 'Fiction'
|
129
|
+
proc {b.isbn}.should_not raise_error
|
130
|
+
b.isbn.should == '0000'
|
131
|
+
proc {b.attribute_not_appearing_in_this_test}.should raise_error
|
132
|
+
end
|
60
133
|
end
|
134
|
+
|
61
135
|
end
|
62
136
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fortify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Parker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-12-02 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|