transform 0.0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in transform.gemspec
4
3
  gemspec
4
+ gem 'rspec'
5
+ gem 'rails'
6
+ gem 'sqlite3'
data/Rakefile CHANGED
@@ -1,2 +1,5 @@
1
1
  require 'bundler'
2
+ require 'rspec/core/rake_task'
2
3
  Bundler::GemHelper.install_tasks
4
+ RSpec::Core::RakeTask.new('spec')
5
+ task :default => :spec
@@ -1,4 +1,2 @@
1
- module Transform
2
- require 'transform/methods'
3
- require 'transform/railtie' if defined?(Rails)
4
- end
1
+ require 'transform/transformer_chain'
2
+ require 'transform/railtie'
@@ -1,7 +1,28 @@
1
- class Railtie < Rails::Railtie
2
- initializer "application_controller.initialize_transform" do
3
- ActiveSupport.on_load(:active_record) do
4
- extend Methods
1
+ module Transform
2
+
3
+ class Railtie < Rails::Railtie
4
+
5
+ def self.extend_active_record
6
+
7
+ ActiveRecord::Base.class_eval do
8
+
9
+ def self.transform(params={})
10
+ instance_eval do
11
+ @@tc = TransformerChain.new(params)
12
+ end
13
+ self.column_names.each do |c|
14
+ class_eval do
15
+ define_method("#{c}=".to_sym) do |v|
16
+ self[c.to_sym]= @@tc.apply c.to_sym, v
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
5
23
  end
24
+
25
+ Railtie.extend_active_record
26
+
6
27
  end
7
- end
28
+ end
@@ -0,0 +1,45 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'transform/transformers/upcase_transformer'
3
+ require 'transform/transformers/strip_transformer'
4
+ require 'transform/transformers/remove_accent_transformer'
5
+
6
+ module Transform
7
+ class TransformerChain
8
+
9
+ def initialize(chain)
10
+ @@upt = UpcaseTransformer.new
11
+ @@stt = StripTransformer.new
12
+ @@rat = RemoveAccentTransformer.new
13
+ @chain = chain
14
+ end
15
+
16
+ def apply(field, value)
17
+ value = _apply(:all, value) if @chain.key?(:all) unless excepts(field)
18
+ value = _apply(field, value) if @chain.key?(field)
19
+ value
20
+ end
21
+
22
+ def _apply(field, value)
23
+ @chain[field].each do |transformation|
24
+ value = get_transformer(transformation).apply value
25
+ end
26
+ value
27
+ end
28
+
29
+ def excepts(field)
30
+ return @chain[:except].include?(field) if @chain.key?(:except)
31
+ return false
32
+ end
33
+
34
+ def get_transformer(identity)
35
+ case identity
36
+ when :upcase then
37
+ @@upt
38
+ when :strip then
39
+ @@stt
40
+ when :remove_accent then
41
+ @@rat
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class RemoveAccentTransformer
3
+
4
+ BEFORE = %(AEIOUAEIOUAEIOUAEIOUAONCaeiouaeiouaeiouaeiouaonc)
5
+ AFTER = %(ÁÉÍÓÚÂÊÎÔÛÀÈÌÒÙÄËÏÖÜÃÕÑÇáéíóúâêîôûàèìòùäëïöüãõñç)
6
+
7
+ def apply(v)
8
+ v.tr!(AFTER, BEFORE) if is_applyable?(v)
9
+ return v
10
+ end
11
+
12
+ def is_applyable?(v)
13
+ v && v.is_a?(String)
14
+ end
15
+
16
+ end
@@ -0,0 +1,20 @@
1
+ class StripTransformer
2
+
3
+ SPECIAL_CHARACTERS = /[_]|[-]|[.]/
4
+
5
+ def apply(v)
6
+ v.gsub!(regex, '') if is_applyable?(v)
7
+ return v
8
+ end
9
+
10
+ def is_applyable?(v)
11
+ v && v.is_a?(String)
12
+ end
13
+
14
+ private
15
+
16
+ def regex
17
+ Regexp.new(SPECIAL_CHARACTERS)
18
+ end
19
+
20
+ end
@@ -0,0 +1,12 @@
1
+ class UpcaseTransformer
2
+
3
+ def apply(v)
4
+ v.upcase! if is_applyable?(v)
5
+ return v
6
+ end
7
+
8
+ def is_applyable?(v)
9
+ v && v.is_a?(String)
10
+ end
11
+
12
+ end
@@ -1,3 +1,3 @@
1
1
  module Transform
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'fileutils'
3
+ require 'bundler/setup'
4
+ require 'active_support'
5
+ require "active_record/railtie"
6
+ require 'transform'
7
+
8
+ schema_path = 'spec/support/schema.rb'
9
+ database_path = 'spec/support/test.sqlite3'
10
+
11
+ FileUtils.rm(database_path) if File.exists?(database_path)
12
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => database_path
13
+ load(schema_path)
14
+
15
+ RSpec.configure do |config|
16
+ end
@@ -0,0 +1,11 @@
1
+ ActiveRecord::Schema.define(:version => 1) do
2
+
3
+ create_table "people", :force => true do |t|
4
+ t.integer "person_id"
5
+ t.string "name"
6
+ t.string "address"
7
+ t.datetime "created_at"
8
+ t.datetime "updated_at"
9
+ end
10
+
11
+ end
Binary file
@@ -0,0 +1,18 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ class Person < ActiveRecord::Base
5
+
6
+ transform :all => [:remove_accent, :upcase, :strip],
7
+ :except => [:address],
8
+ :address => [:strip]
9
+
10
+ end
11
+
12
+ describe Transform do
13
+ subject { Person.new }
14
+ it do
15
+ subject.attributes={:name => 'b-á.ssc_s', :address=>'c._a'}
16
+ puts subject.inspect
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-28 00:00:00.000000000 -03:00
12
+ date: 2011-07-11 00:00:00.000000000 -03:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
  description: Several ActiveRecord data transformations
@@ -23,9 +23,16 @@ files:
23
23
  - Gemfile
24
24
  - Rakefile
25
25
  - lib/transform.rb
26
- - lib/transform/methods.rb
27
26
  - lib/transform/railtie.rb
27
+ - lib/transform/transformer_chain.rb
28
+ - lib/transform/transformers/remove_accent_transformer.rb
29
+ - lib/transform/transformers/strip_transformer.rb
30
+ - lib/transform/transformers/upcase_transformer.rb
28
31
  - lib/transform/version.rb
32
+ - spec/spec_helper.rb
33
+ - spec/support/schema.rb
34
+ - spec/support/test.sqlite3
35
+ - spec/transform/transform_spec.rb
29
36
  - transform.gemspec
30
37
  has_rdoc: true
31
38
  homepage: https://github.com/marcelloma/transform
@@ -52,4 +59,8 @@ rubygems_version: 1.6.2
52
59
  signing_key:
53
60
  specification_version: 3
54
61
  summary: Several ActiveRecord data transformations
55
- test_files: []
62
+ test_files:
63
+ - spec/spec_helper.rb
64
+ - spec/support/schema.rb
65
+ - spec/support/test.sqlite3
66
+ - spec/transform/transform_spec.rb
@@ -1,59 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- #transform :upcase # all valid fields
3
- #transform :upcase, :except => [:foo] # all valid fields except given fields
4
- #transform :upcase, :only => [:foo] # only given fields
5
- #transform :remove_chars, :only => [:social_security_number], :characters => %W(- . _) # removes all the given characters from fields
6
-
7
- module Methods
8
-
9
- SPECIAL_CHARACTERS = %(_-.)
10
- LETTERS = %(aeiouaeiouaeiouaeiouaonc)
11
- LETTERS_2 = %(áéíóúâêîôûàèìòùäëïöüãõñç)
12
- LETTERS_3 = %(AEIOUAEIOUAEIOUAEIOUAONC#{LETTERS})
13
- LETTERS_4 = %(ÁÉÍÓÚÂÊÎÔÛÀÈÌÒÙÄËÏÖÜÃÕÑÇ#{LETTERS_2})
14
-
15
- def transform(type, params={})
16
- include InstanceMethods
17
- class_attribute :_transformations unless self.respond_to?('_transformations')
18
- before_validation :do_transformations!
19
- self._transformations = [] if self._transformations == nil
20
- params[:only] = params[:only].map { |v| v.to_s } if params[:only]
21
- params[:except] = params[:except].map { |v| v.to_s } if params[:except]
22
- self._transformations << { type => params }
23
- end
24
-
25
- module InstanceMethods
26
- def do_transformations!
27
- return if _transformations.empty?
28
- _transformations.each do |transformation|
29
- transformation.each do |type, params|
30
- scope = self.attributes.clone
31
- if only = params[:only]
32
- scope.slice!(*only)
33
- elsif except = params[:except]
34
- scope.except!(*except)
35
- end
36
- tmp={}
37
- scope.each do |attrk,attrv|
38
- self.send(attrk+'=', self.send(type.to_sym, attrv, params))
39
- end
40
- end
41
- end
42
- end
43
- def remove_accent(v, params)
44
- v.tr(LETTERS_4,LETTERS_3) if v && v.is_a?(String)
45
- end
46
- def upcase(v, params)
47
- v.upcase if v && v.is_a?(String)
48
- end
49
- def remove_chars(v, params)
50
- chars = params[:characters]
51
- chars ||= SPECIAL_CHARACTERS
52
- str=""
53
- chars.each_char do |v| str << "[#{v}]|" end
54
- str[str.size-1] = ""
55
- return v.gsub(Regexp.new(str), '') if v && v.is_a?(String)
56
- end
57
- end
58
-
59
- end