paranoid_starlight 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +54 -10
- data/lib/paranoid_starlight.rb +33 -10
- data/lib/paranoid_starlight/version.rb +1 -1
- data/paranoid_starlight.gemspec +5 -3
- data/specs/paranoid_starlight_spec.rb +35 -3
- metadata +5 -4
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
This piece of gem contains some custom validators and converters for ActiveModel.
|
4
4
|
|
5
|
-
It
|
5
|
+
It has validations for email and name (European style).
|
6
6
|
And validation and converter (to international format) of telephone number. (CZ/SK format)
|
7
|
-
|
7
|
+
Few converters for texts and strings. Specs included.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -12,6 +12,10 @@ Add this line to your application's Gemfile:
|
|
12
12
|
|
13
13
|
gem 'paranoid_starlight'
|
14
14
|
|
15
|
+
If you want to have methods available for ActiveRecord models, use:
|
16
|
+
|
17
|
+
gem 'paranoid_starlight', :require => 'paranoid_starlight/active_record'
|
18
|
+
|
15
19
|
And then execute:
|
16
20
|
|
17
21
|
$ bundle
|
@@ -34,21 +38,55 @@ Or install it yourself as:
|
|
34
38
|
model.email = 'example@example.org'
|
35
39
|
model.valid?
|
36
40
|
|
37
|
-
or put this into /config/initializers/paranoid.rb
|
38
41
|
|
39
|
-
|
40
|
-
ActiveRecord::Base.send(:include, ParanoidStarlight::Converters)
|
41
|
-
|
42
|
-
Now you have them available globally for models
|
42
|
+
If you required active_record hook, you can use:
|
43
43
|
|
44
44
|
class Person < ActiveRecord::Base
|
45
|
-
attr_accessible :name, :telephone, :email
|
45
|
+
attr_accessible :name, :telephone, :mobile, :email
|
46
|
+
attr_accessible :address, :zip
|
47
|
+
|
46
48
|
validates_email_format_of :email
|
47
49
|
validates_name_format_of :name
|
48
|
-
|
50
|
+
# few different valid formats are allowed
|
51
|
+
validates_telephone_format_of :telephone, :mobile
|
49
52
|
# or validates :email, :email => true
|
50
53
|
|
51
|
-
before_save :
|
54
|
+
before_save :convert_fields
|
55
|
+
before_validation :process_fields
|
56
|
+
|
57
|
+
def convert_fields
|
58
|
+
# convert to international form
|
59
|
+
convert_telephone_number(:telephone)
|
60
|
+
convert_telephone_number(:mobile)
|
61
|
+
# or convert_telephone_number [:telephone, :mobile]
|
62
|
+
# do other things
|
63
|
+
end
|
64
|
+
|
65
|
+
def process_fields
|
66
|
+
clean_text([:address, :zip])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
Currently there are these possibilities:
|
71
|
+
- validates_email_format_of
|
72
|
+
- validates_name_format_of
|
73
|
+
- validates_telephone_number_of
|
74
|
+
|
75
|
+
- clean_text
|
76
|
+
- clean_whitespaces
|
77
|
+
- convert_telephone_number
|
78
|
+
|
79
|
+
It is easy to create own converter, just do:
|
80
|
+
|
81
|
+
class Kiddie
|
82
|
+
include ::ParanoidStarlight::Converters
|
83
|
+
attr_accessor :name
|
84
|
+
|
85
|
+
def l33t
|
86
|
+
basic_converter(self, :name) do |text|
|
87
|
+
text.gsub('e', '3')
|
88
|
+
end
|
89
|
+
end
|
52
90
|
end
|
53
91
|
|
54
92
|
|
@@ -59,3 +97,9 @@ Now you have them available globally for models
|
|
59
97
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
60
98
|
4. Push to the branch (`git push origin my-new-feature`)
|
61
99
|
5. Create new Pull Request
|
100
|
+
|
101
|
+
## Author
|
102
|
+
Created by Ivan Stana
|
103
|
+
License: MIT
|
104
|
+
|
105
|
+
I encourage to write me something
|
data/lib/paranoid_starlight.rb
CHANGED
@@ -128,28 +128,51 @@ module ParanoidStarlight
|
|
128
128
|
end
|
129
129
|
end
|
130
130
|
|
131
|
+
def clean_whitespaces(inattr, outattr = '')
|
132
|
+
basic_converter(self, inattr, outattr) do |text|
|
133
|
+
text.to_s.gsub(/\s/, '')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
131
137
|
private
|
132
138
|
def basic_converter(obj, inattr, outattr, &code)
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
139
|
+
attributes = []
|
140
|
+
|
141
|
+
# May be only one value, ideally string or symbol
|
142
|
+
outattr = outattr.to_sym
|
143
|
+
|
144
|
+
|
145
|
+
if inattr.is_a? Array
|
146
|
+
attributes = inattr
|
147
|
+
elsif obj.respond_to? inattr.to_sym
|
148
|
+
attributes << inattr
|
149
|
+
# hash, array and like haven't got to_sym defined
|
150
|
+
outattr = inattr if outattr.to_s == ''
|
151
|
+
else
|
152
|
+
raise("Attribute #{inattr} does not exist.")
|
153
|
+
end
|
154
|
+
|
155
|
+
if attributes.size > 1 && outattr.to_s != ''
|
156
|
+
raise("Multiple attributes can be used only without output attribute")
|
157
|
+
end
|
158
|
+
|
159
|
+
attributes.each do |attribute|
|
160
|
+
outattr = attribute if attributes.size > 1
|
138
161
|
|
139
|
-
unless obj.respond_to? outattr
|
162
|
+
unless obj.respond_to? outattr
|
140
163
|
raise("Attribute #{outattr} does not exist.")
|
141
164
|
end
|
142
|
-
|
165
|
+
|
143
166
|
setter = "#{outattr}=".to_sym
|
144
167
|
unless obj.respond_to? setter
|
145
168
|
raise("Setter #{setter} does not exist.")
|
146
169
|
end
|
147
|
-
|
148
|
-
to_convert = obj.send(
|
170
|
+
|
171
|
+
to_convert = obj.send(attribute.to_sym)
|
149
172
|
unless to_convert.nil?
|
150
173
|
obj.send(setter, code.call(to_convert))
|
151
174
|
end
|
152
|
-
|
175
|
+
end
|
153
176
|
end
|
154
177
|
|
155
178
|
end # end converters
|
data/paranoid_starlight.gemspec
CHANGED
@@ -8,10 +8,12 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = ParanoidStarlight::VERSION
|
9
9
|
gem.author = 'Ivan Stana'
|
10
10
|
gem.email = 'stiipa@centrum.sk'
|
11
|
-
gem.summary = "Pack of custom validations and converters for ActiveModel"
|
11
|
+
gem.summary = "Pack of custom validations and converters for ActiveModel. Or standalone."
|
12
12
|
gem.description = <<-EOF
|
13
|
-
|
14
|
-
|
13
|
+
It has validations for email and name (European style).
|
14
|
+
And validation and converter (to international format)
|
15
|
+
of telephone number. (CZ/SK format)
|
16
|
+
Few converters for texts and strings. Specs included.
|
15
17
|
EOF
|
16
18
|
|
17
19
|
gem.homepage = "http://github.com/istana/paranoid_starlight"
|
@@ -8,7 +8,7 @@ class Fangirl
|
|
8
8
|
include ::ParanoidStarlight::Converters
|
9
9
|
attr_accessor :telephone
|
10
10
|
attr_accessor :text
|
11
|
-
attr_accessor :basic_field, :basic_field_out
|
11
|
+
attr_accessor :basic_field, :basic_field2, :basic_field_out
|
12
12
|
end
|
13
13
|
|
14
14
|
describe ParanoidStarlight::Converters do
|
@@ -61,6 +61,7 @@ describe ParanoidStarlight::Converters do
|
|
61
61
|
before do
|
62
62
|
@test = Fangirl.new
|
63
63
|
@test.basic_field = "Hello world!"
|
64
|
+
@test.basic_field2 = "World sends love to you!"
|
64
65
|
|
65
66
|
def @test.upcase(input, output = '')
|
66
67
|
basic_converter(self, input, output) do |text|
|
@@ -69,6 +70,22 @@ describe ParanoidStarlight::Converters do
|
|
69
70
|
end
|
70
71
|
end
|
71
72
|
|
73
|
+
context 'supplies parameters in different format' do
|
74
|
+
it 'should accept anything in input with to_s' do
|
75
|
+
@test.upcase(:basic_field)
|
76
|
+
@test.basic_field.should == "HELLO WORLD!"
|
77
|
+
@test.upcase('basic_field2')
|
78
|
+
@test.basic_field2.should == "WORLD SENDS LOVE TO YOU!"
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should accept anything with to_s as output' do
|
82
|
+
@test.upcase(:basic_field, :basic_field_out)
|
83
|
+
@test.basic_field_out.should == "HELLO WORLD!"
|
84
|
+
@test.upcase('basic_field2', 'basic_field_out')
|
85
|
+
@test.basic_field_out.should == "WORLD SENDS LOVE TO YOU!"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
72
89
|
context 'had one attribute supplied' do
|
73
90
|
it 'should overwrite attribute' do
|
74
91
|
@test.upcase(:basic_field)
|
@@ -84,6 +101,21 @@ describe ParanoidStarlight::Converters do
|
|
84
101
|
end
|
85
102
|
end
|
86
103
|
|
104
|
+
context 'had array of attributes supplied' do
|
105
|
+
it 'should convert all attributes of array' do
|
106
|
+
@test.upcase([:basic_field, :basic_field2])
|
107
|
+
@test.basic_field.should == "HELLO WORLD!"
|
108
|
+
@test.basic_field2.should == "WORLD SENDS LOVE TO YOU!"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'had array of attributes and output supplied' do
|
113
|
+
it 'should raise error' do
|
114
|
+
expect {@test.upcase([:basic_field, :basic_field2],
|
115
|
+
:basic_field_out) }.to raise_error(/Multiple attributes can/)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
87
119
|
end
|
88
120
|
|
89
121
|
|
@@ -149,7 +181,7 @@ describe 'ParanoidStarlight::Validations' do
|
|
149
181
|
'Ivan Stana', 'Ivan R. Stana', 'Ivan Ronon Stana',
|
150
182
|
'Jean Claude Van Damme', 'Jean-Pierre Cassel',
|
151
183
|
'Tatyana Sukhotina-Tolstaya',
|
152
|
-
'John R. R. Tolkien'
|
184
|
+
'John R. R. Tolkien', 'I S'
|
153
185
|
].each {
|
154
186
|
|name| @name.validate_each(@mock, "name", name)
|
155
187
|
}
|
@@ -158,7 +190,7 @@ describe 'ParanoidStarlight::Validations' do
|
|
158
190
|
it "should validate invalid name" do
|
159
191
|
@mock.errors[].should_receive('<<')
|
160
192
|
[
|
161
|
-
'J. R. R. Tolkien', 'J.R.R. Tolkien', 'Tolkien'
|
193
|
+
'J. R. R. Tolkien', 'J.R.R. Tolkien', 'Tolkien', ''
|
162
194
|
].each {
|
163
195
|
|name| @name.validate_each(@mock, "name", name)
|
164
196
|
}
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paranoid_starlight
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -75,8 +75,9 @@ dependencies:
|
|
75
75
|
- - ! '>='
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
|
-
description: ! "
|
79
|
-
|
78
|
+
description: ! " It has validations for email and name (European style).\n And
|
79
|
+
validation and converter (to international format)\n of telephone number. (CZ/SK
|
80
|
+
format)\n Few converters for texts and strings. Specs included.\n"
|
80
81
|
email: stiipa@centrum.sk
|
81
82
|
executables: []
|
82
83
|
extensions: []
|
@@ -114,5 +115,5 @@ rubyforge_project:
|
|
114
115
|
rubygems_version: 1.8.24
|
115
116
|
signing_key:
|
116
117
|
specification_version: 3
|
117
|
-
summary: Pack of custom validations and converters for ActiveModel
|
118
|
+
summary: Pack of custom validations and converters for ActiveModel. Or standalone.
|
118
119
|
test_files: []
|