transform 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in transform.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,59 @@
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
@@ -0,0 +1,7 @@
1
+ class Railtie < Rails::Railtie
2
+ initializer "application_controller.initialize_transform" do
3
+ ActiveSupport.on_load(:active_record) do
4
+ extend Methods
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Transform
2
+ VERSION = "0.0.1"
3
+ end
data/lib/transform.rb ADDED
@@ -0,0 +1,4 @@
1
+ module Transform
2
+ require 'transform/methods'
3
+ require 'transform/railtie' if defined?(Rails)
4
+ end
data/transform.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "transform/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "transform"
7
+ s.version = Transform::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Marcello Milhomem Albuquerque"]
10
+ s.email = ["marcello.m.albuquerque@gmail.com"]
11
+ s.homepage = "https://github.com/marcelloma/transform"
12
+ s.summary = %q{Several ActiveRecord data transformations}
13
+ s.description = %q{Several ActiveRecord data transformations}
14
+
15
+ s.rubyforge_project = "transform"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcello Milhomem Albuquerque
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-28 00:00:00.000000000 -03:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Several ActiveRecord data transformations
16
+ email:
17
+ - marcello.m.albuquerque@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - Rakefile
25
+ - lib/transform.rb
26
+ - lib/transform/methods.rb
27
+ - lib/transform/railtie.rb
28
+ - lib/transform/version.rb
29
+ - transform.gemspec
30
+ has_rdoc: true
31
+ homepage: https://github.com/marcelloma/transform
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project: transform
51
+ rubygems_version: 1.6.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Several ActiveRecord data transformations
55
+ test_files: []