simple_descriptor 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -1
- data/README +24 -0
- data/Rakefile +2 -2
- data/lib/simple_descriptor/class_descriptor.rb +57 -1
- data/lib/simple_descriptor/version.rb +2 -2
- metadata +7 -7
data/CHANGELOG
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
|
1
|
+
0.2.0 added direct bindings for ActiveRecord
|
2
|
+
2006 11 26 first release 0.1.0
|
data/README
CHANGED
@@ -104,6 +104,30 @@ The line
|
|
104
104
|
|
105
105
|
adds some new methods to our class that makes more easy to access a description
|
106
106
|
|
107
|
+
== Too lazy? Let Simple Descriptor do the hard work for you
|
108
|
+
|
109
|
+
Since 0.2.0 version Simple Descriptor provides method to automate some
|
110
|
+
ActiveRecord models description
|
111
|
+
|
112
|
+
class Employee < ActiveRecord::Base
|
113
|
+
extend SimpleDescriptor::ClassDescriptor
|
114
|
+
validates_ar_descriptions
|
115
|
+
end
|
116
|
+
|
117
|
+
With just the code above you will get validations for:
|
118
|
+
|
119
|
+
1) all your numeric fields defined as numeric in your DB
|
120
|
+
2) maximum length of all the fields defined as string in the DB
|
121
|
+
3) all the required field defined as not null in the DB
|
122
|
+
|
123
|
+
and you get
|
124
|
+
|
125
|
+
Employee.maxlength(field)
|
126
|
+
|
127
|
+
plus a lot of pre-defined descriptions for your model that you can inspect with
|
128
|
+
|
129
|
+
Employee.descriptions
|
130
|
+
|
107
131
|
== Where are the description stored and how?
|
108
132
|
|
109
133
|
All the descriptions are stored in a DESCRIPTIONS constant, you can always
|
data/Rakefile
CHANGED
@@ -12,8 +12,8 @@ require File.join(File.dirname(__FILE__), 'lib', 'simple_descriptor', 'version')
|
|
12
12
|
|
13
13
|
AUTHOR = "blank"
|
14
14
|
EMAIL = "your contact email for bug fixes and info"
|
15
|
-
DESCRIPTION = "description
|
16
|
-
RUBYFORGE_PROJECT = "
|
15
|
+
DESCRIPTION = "add description to your classes and make your ActiveRecord validations smarter"
|
16
|
+
RUBYFORGE_PROJECT = "simpledesc"
|
17
17
|
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
18
18
|
BIN_FILES = %w( )
|
19
19
|
|
@@ -125,7 +125,7 @@ module SimpleDescriptor #:nodoc:
|
|
125
125
|
end
|
126
126
|
end
|
127
127
|
alias :description_of :described_value_for
|
128
|
-
#creates some shortcut
|
128
|
+
#creates some shortcut methods to access a description
|
129
129
|
#
|
130
130
|
#example
|
131
131
|
# Person.shortcut_desctiption :limited, :as => :maxlength
|
@@ -163,6 +163,62 @@ module SimpleDescriptor #:nodoc:
|
|
163
163
|
end
|
164
164
|
end
|
165
165
|
end
|
166
|
+
#loads standard descriptions from columns_hash of ActiveRecord classes
|
167
|
+
#
|
168
|
+
#every column will be described as db type (i.e. :integer, :string, :date etc.)
|
169
|
+
#
|
170
|
+
#string columns are described as limited with the value of the maximum length
|
171
|
+
#
|
172
|
+
#a maxlength class method will be added to provide the maximum length of a field
|
173
|
+
#as defined from the database
|
174
|
+
#
|
175
|
+
#This methods is called by validates_ar_descriptions unless validates_ar_descriptions is called
|
176
|
+
#with the :load_desc option set to false
|
177
|
+
#
|
178
|
+
# Post.described_as :string
|
179
|
+
#
|
180
|
+
#will return all the fields of posts table that are defined as strings
|
181
|
+
#
|
182
|
+
# Post.maxlength :title
|
183
|
+
#
|
184
|
+
#will return the maximum length of post title as defined in the database
|
185
|
+
#
|
186
|
+
def load_ar_descriptions
|
187
|
+
if respond_to? :columns_hash
|
188
|
+
columns_hash.each do |k, v|
|
189
|
+
describe k.to_sym, :as => v.type
|
190
|
+
describe k.to_sym, :as => :required if !v.null
|
191
|
+
case v.type
|
192
|
+
when :string : describe k.to_sym, :as => :limited, :with_value => v.limit
|
193
|
+
end
|
194
|
+
end
|
195
|
+
shortcut_description :limited, :as => :maxlength
|
196
|
+
else
|
197
|
+
raise "no columns_hash method"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
#runs validations following the descriptions provided
|
201
|
+
# 1 loads all the descriptions from the database using load_ar_descriptions
|
202
|
+
# 2 validates presence of all required fields unless :required => false in options
|
203
|
+
# 3 validates maximum length of every string field unless :length => false in options
|
204
|
+
# 4 validates integer format for every integer field unless :integer => false in options
|
205
|
+
# 5 validates float format for every float field unless :float => false in options
|
206
|
+
# 6 validates decimal format for every decimal field unless :decimal => false in options
|
207
|
+
# 7 if numbers => false in options then float, decimal and integer validations won't be done
|
208
|
+
def validates_ar_descriptions(options = {})
|
209
|
+
options = {:load_desc => true, :required => true, :length => true, :numbers => true, :float => true, :decimal => true, :integer => true}.update options
|
210
|
+
load_ar_descriptions if options[:load_desc]
|
211
|
+
yield if block_given?
|
212
|
+
send(:validates_presence_of, described_as(:required)) if options[:required]
|
213
|
+
described_as(:limited).each { |field| validates_length_of field, :maximum => maxlength(field), :allow_nil => true } if options[:length]
|
214
|
+
if options[:numbers]
|
215
|
+
described_as(:integer).each { |field| validates_numericality_of field, :only_integer => true, :allow_nil => true } if options[:integer]
|
216
|
+
described_as(:decimal).each { |field| validates_numericality_of field } if options[:decimal]
|
217
|
+
described_as(:float).each { |field| validates_numericality_of field } if options[:float]
|
218
|
+
end
|
219
|
+
validates_custom_descriptions if respond_to? :validates_custom_descriptions
|
220
|
+
end
|
221
|
+
|
166
222
|
private
|
167
223
|
def find_description_for(arg)
|
168
224
|
descriptions.inject({}) do |h, d|
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.4
|
3
3
|
specification_version: 1
|
4
4
|
name: simple_descriptor
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
8
|
-
summary: description
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2007-06-04 00:00:00 +01:00
|
8
|
+
summary: add description to your classes and make your ActiveRecord validations smarter
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: your contact email for bug fixes and info
|
12
|
-
homepage: http://
|
13
|
-
rubyforge_project:
|
14
|
-
description: description
|
12
|
+
homepage: http://simpledesc.rubyforge.org
|
13
|
+
rubyforge_project: simpledesc
|
14
|
+
description: add description to your classes and make your ActiveRecord validations smarter
|
15
15
|
autorequire: simple_descriptor
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|