attribute_normalizer 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,12 +2,14 @@ h1. Attribute Normalizer
2
2
 
3
3
  p. A little normalization goes a long way in helping maintain data integrity.
4
4
 
5
- h2. Recent Changes
5
+ h2. Change History
6
+ * 1.1.0
7
+ ** Allow the use of default normalizers before and after the evaluation of a given block
6
8
 
7
- * 1.0.0.pre
8
- * -DSL Changes-
9
- * Default attributes to normalize
10
- * mongid support
9
+ * 1.0.0
10
+ ** -DSL Changes-
11
+ ** Default attributes to normalize
12
+ ** mongid support
11
13
 
12
14
  * 0.3.0
13
15
  ** Normalizer Chaining
@@ -66,6 +68,8 @@ describe Book do
66
68
  it { should normalize_attribute(:price).from('$3,450.98').to(3450.98) }
67
69
  it { should normalize_attribute(:summary).from(' Here is my summary that is a little to long ').to('Here is m...') }
68
70
  it { should normalize_attribute(:title).from(' pick up chicks with magic tricks ').to('Pick Up Chicks With Magic Tricks') }
71
+ it { should normalize_attribute(:slug).from(' Social Life at the Edge of Chaos ').to('social-life-at-the-edge-of-chaos') }
72
+ it { should normalize_attribute(:limited_slug).from(' Social Life at the Edge of Chaos ').to('social-life') }
69
73
  end</code></pre>
70
74
 
71
75
  p. The following normalizers are already included with the +0.3 version of the gem.
@@ -127,17 +131,33 @@ The _normalize_attributes_ method is eager loaded into your ORM. _normalize_att
127
131
  value.is_a?(String) ? value.titleize.strip : value
128
132
  end
129
133
 
134
+ # Or use a combination of normalizers plus a inline block.
135
+ # the normalizers in the :with option will each be evalulated
136
+ # in order and the result will be given to the block.
137
+ # You could also use option :before in place of :with
138
+ normalize_attribute :slug, :with => [ :strip, :blank ] do |value|
139
+ value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
140
+ end
141
+
142
+ # Use buildin normalizers before and after the evaluation of your inline
143
+ # block
144
+ normalize_attribute :limited_slug, :before => [ :strip, :blank ], :after => [ { :truncate => { :length => 11, :omission => '' } } ] do |value|
145
+ value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
146
+ end
147
+
130
148
  end</code></pre>
131
149
 
132
150
  p. All the specs will pass now. Here is quick look at the behaviour from a console.
133
151
 
134
152
  <pre><code>summary = 'Here is my summary that is a little to long'
135
153
  title = 'pick up chicks with magic tricks'
136
- book = Book.create!(:author => '', :price => '$3,450.89', :summary => summary, :title => title)
137
- book.author # => nil
138
- book.price # => 3450.89
139
- book.summary # => 'Here is m...'
140
- book.title # => 'Pick Up Chicks With Magic Tricks'
154
+ book = Book.create!(:author => '', :price => '$3,450.89', :summary => summary, :title => title, :slug => title, :limited_slug => title)
155
+ book.author # => nil
156
+ book.price # => 3450.89
157
+ book.summary # => 'Here is m...'
158
+ book.title # => 'Pick Up Chicks With Magic Tricks'
159
+ book.slug # => 'pick-up-chicks-with-magic-tricks'
160
+ book.limited_slug # => 'pick-up-chi'
141
161
  </code></pre>
142
162
 
143
163
  h2. Test Helpers
data/Rakefile CHANGED
@@ -1,14 +1,12 @@
1
1
  require 'rake'
2
- require 'rake/rdoctask'
3
- require 'spec/rake/spectask'
2
+ require 'rdoc/task'
3
+ require 'rspec/core/rake_task'
4
4
  require 'bundler/gem_tasks'
5
5
 
6
6
  desc 'Default: spec tests.'
7
7
  task :default => :spec
8
8
 
9
9
  desc 'Test the attribute_normalizer plugin.'
10
- Spec::Rake::SpecTask.new('spec') do |t|
11
- t.spec_files = FileList['spec/**/*_spec.rb']
12
- t.spec_opts = ["-c"]
10
+ RSpec::Core::RakeTask.new do |t|
13
11
  end
14
12
 
@@ -8,22 +8,31 @@ module AttributeNormalizer
8
8
  def normalize_attributes(*attributes, &block)
9
9
  options = attributes.last.is_a?(::Hash) ? attributes.pop : {}
10
10
 
11
- normalizers = [ options.delete(:with) ].flatten.compact
11
+ normalizers = [ options.delete(:with) ].flatten.compact
12
+ normalizers = [ options.delete(:before) ].flatten.compact if block_given? && normalizers.empty?
13
+ post_normalizers = [ options.delete(:after) ].flatten.compact if block_given?
12
14
 
13
15
  if normalizers.empty? && !block_given?
14
16
  normalizers = AttributeNormalizer.configuration.default_normalizers # the default normalizers
15
17
  end
16
18
 
17
19
  attributes.each do |attribute|
18
- if block_given?
19
- define_method "normalize_#{attribute}" do |value|
20
- yield(value)
20
+ define_method "normalize_#{attribute}" do |value|
21
+ normalized = value
22
+
23
+ normalizers.each do |normalizer_name|
24
+ unless normalizer_name.kind_of?(Symbol)
25
+ normalizer_name, options = normalizer_name.keys[0], normalizer_name[ normalizer_name.keys[0] ]
26
+ end
27
+ normalizer = AttributeNormalizer.configuration.normalizers[normalizer_name]
28
+ raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{normalizer_name}") unless normalizer
29
+ normalized = normalizer.respond_to?(:normalize) ? normalizer.normalize( normalized , options) : normalizer.call(normalized, options)
21
30
  end
22
- else
23
- define_method "normalize_#{attribute}" do |value|
24
- normalized = value
25
31
 
26
- normalizers.each do |normalizer_name|
32
+ normalized = block_given? ? yield(normalized) : normalized
33
+
34
+ if block_given?
35
+ post_normalizers.each do |normalizer_name|
27
36
  unless normalizer_name.kind_of?(Symbol)
28
37
  normalizer_name, options = normalizer_name.keys[0], normalizer_name[ normalizer_name.keys[0] ]
29
38
  end
@@ -31,8 +40,9 @@ module AttributeNormalizer
31
40
  raise AttributeNormalizer::MissingNormalizer.new("No normalizer was found for #{normalizer_name}") unless normalizer
32
41
  normalized = normalizer.respond_to?(:normalize) ? normalizer.normalize( normalized , options) : normalizer.call(normalized, options)
33
42
  end
34
- normalized
35
43
  end
44
+
45
+ normalized
36
46
  end
37
47
 
38
48
  self.send :private, "normalize_#{attribute}"
@@ -3,6 +3,10 @@ require File.dirname(File.expand_path(__FILE__)) + '/test_helper'
3
3
  describe Article do
4
4
  it { should normalize_attribute(:title).from(' Social Life at the Edge of Chaos ').to('Social Life at the Edge of Chaos') }
5
5
  it { should normalize_attribute(:authors).from(' Octavio Miramontes and Pedro Miramontes ').to('Octavio Miramontes and Pedro Miramontes') }
6
+ it { should normalize_attribute(:slug) }
7
+ it { should normalize_attribute(:slug).from(' Social Life at the Edge of Chaos ').to('social-life-at-the-edge-of-chaos') }
8
+ it { should normalize_attribute(:limited_slug) }
9
+ it { should normalize_attribute(:limited_slug).from(' Social Life at the Edge of Chaos ').to('social-life') }
6
10
 
7
11
  context 'normalization should not interfere with other hooks and aliases on the attribute assignment' do
8
12
  before do
@@ -42,4 +46,4 @@ describe Article do
42
46
  @article.authors.should == 'testing the default normalizer'
43
47
  end
44
48
 
45
- end
49
+ end
@@ -29,5 +29,7 @@ ActiveRecord::Schema.define do
29
29
  create_table :articles, :force => true do |t|
30
30
  t.string :title
31
31
  t.string :authors
32
+ t.string :slug
33
+ t.string :limited_slug
32
34
  end
33
- end
35
+ end
@@ -1,2 +1,11 @@
1
1
  class Article < ActiveRecord::Base
2
- end
2
+
3
+ normalize_attribute :slug, :with => [ :strip, :blank ] do |value|
4
+ value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
5
+ end
6
+
7
+ normalize_attribute :limited_slug, :before => [ :strip, :blank ], :after => [ { :truncate => { :length => 11, :omission => '' } } ] do |value|
8
+ value.present? && value.is_a?(String) ? value.downcase.gsub(/\s+/, '-') : value
9
+ end
10
+
11
+ end
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
- require 'spec'
2
+ require 'rspec'
3
3
  require 'active_record'
4
4
  require 'mongoid'
5
5
 
@@ -44,6 +44,6 @@ require 'models/article'
44
44
  require 'models/magazine'
45
45
 
46
46
 
47
- Spec::Runner.configure do |config|
47
+ RSpec.configure do |config|
48
48
  config.include AttributeNormalizer::RSpecMatcher
49
- end
49
+ end
metadata CHANGED
@@ -1,33 +1,23 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: attribute_normalizer
3
- version: !ruby/object:Gem::Version
4
- hash: 21
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
5
  prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 1
10
- version: 1.0.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Michael Deering
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-07-28 00:00:00 -06:00
19
- default_executable:
12
+ date: 2011-07-28 00:00:00.000000000 Z
20
13
  dependencies: []
21
-
22
14
  description:
23
15
  email: mdeering@mdeering.com
24
16
  executables: []
25
-
26
17
  extensions: []
27
-
28
- extra_rdoc_files:
18
+ extra_rdoc_files:
29
19
  - README.textile
30
- files:
20
+ files:
31
21
  - MIT-LICENSE
32
22
  - README.textile
33
23
  - Rakefile
@@ -52,43 +42,35 @@ files:
52
42
  - spec/models/book.rb
53
43
  - spec/models/journal.rb
54
44
  - spec/test_helper.rb
55
- has_rdoc: true
56
45
  homepage: http://github.com/mdeering/attribute_normalizer
57
46
  licenses: []
58
-
59
47
  post_install_message:
60
- rdoc_options:
48
+ rdoc_options:
61
49
  - --charset=UTF-8
62
- require_paths:
50
+ require_paths:
63
51
  - lib
64
- required_ruby_version: !ruby/object:Gem::Requirement
52
+ required_ruby_version: !ruby/object:Gem::Requirement
65
53
  none: false
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- hash: 3
70
- segments:
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ segments:
71
59
  - 0
72
- version: "0"
73
- required_rubygems_version: !ruby/object:Gem::Requirement
60
+ hash: -3251542831906071239
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
62
  none: false
75
- requirements:
76
- - - ">"
77
- - !ruby/object:Gem::Version
78
- hash: 25
79
- segments:
80
- - 1
81
- - 3
82
- - 1
63
+ requirements:
64
+ - - ! '>'
65
+ - !ruby/object:Gem::Version
83
66
  version: 1.3.1
84
67
  requirements: []
85
-
86
68
  rubyforge_project:
87
- rubygems_version: 1.4.2
69
+ rubygems_version: 1.8.10
88
70
  signing_key:
89
71
  specification_version: 3
90
72
  summary: Attribute normalizer that excepts code blocks.
91
- test_files:
73
+ test_files:
92
74
  - spec/article_spec.rb
93
75
  - spec/attribute_normalizer_spec.rb
94
76
  - spec/author_spec.rb