denormalize-field 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+ gem "activerecord", "~> 3.2.0"
3
+
4
+ group :development do
5
+ gem 'pry'
6
+ gem 'sqlite3'
7
+ gem "rspec", "~> 2.8.0"
8
+ gem "rdoc", "~> 3.12"
9
+ gem "jeweler", "~> 1.8.4"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,57 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.8)
5
+ activesupport (= 3.2.8)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.8)
8
+ activemodel (= 3.2.8)
9
+ activesupport (= 3.2.8)
10
+ arel (~> 3.0.2)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.8)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ arel (3.0.2)
16
+ builder (3.0.3)
17
+ coderay (1.0.7)
18
+ diff-lcs (1.1.3)
19
+ git (1.2.5)
20
+ i18n (0.6.1)
21
+ jeweler (1.8.4)
22
+ bundler (~> 1.0)
23
+ git (>= 1.2.5)
24
+ rake
25
+ rdoc
26
+ json (1.7.5)
27
+ method_source (0.8)
28
+ multi_json (1.3.6)
29
+ pry (0.9.10)
30
+ coderay (~> 1.0.5)
31
+ method_source (~> 0.8)
32
+ slop (~> 3.3.1)
33
+ rake (0.9.2.2)
34
+ rdoc (3.12)
35
+ json (~> 1.4)
36
+ rspec (2.8.0)
37
+ rspec-core (~> 2.8.0)
38
+ rspec-expectations (~> 2.8.0)
39
+ rspec-mocks (~> 2.8.0)
40
+ rspec-core (2.8.0)
41
+ rspec-expectations (2.8.0)
42
+ diff-lcs (~> 1.1.2)
43
+ rspec-mocks (2.8.0)
44
+ slop (3.3.3)
45
+ sqlite3 (1.3.6)
46
+ tzinfo (0.3.33)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ activerecord (~> 3.2.0)
53
+ jeweler (~> 1.8.4)
54
+ pry
55
+ rdoc (~> 3.12)
56
+ rspec (~> 2.8.0)
57
+ sqlite3
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Len Smith
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,15 @@
1
+ = denormalize-field
2
+
3
+ Denormalizes fields in ActiveRecord models in order to avoid SQL joins and hydrating Ruby Objects to obtain a simple fields.
4
+
5
+ = Usage
6
+
7
+ class Post < ActiveRecord::Base
8
+ belongs_to :category
9
+ denormalizes :category => :name
10
+ end
11
+
12
+ category = Category.create(:name => "News")
13
+ post = Post.create(:category => category)
14
+
15
+ post.category_name # "News" (you have to create the migration to add Post#category_name manually)
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "denormalize-field"
18
+ gem.homepage = "http://github.com/Barrison/denormalize-field"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Denormalize ActiveRecord fields}
21
+ gem.description = %Q{Denormalize ActiveRecord fields for performance reasons}
22
+ gem.email = "len@barrison.com"
23
+ gem.authors = ["Len Smith"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rdoc/task'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "denormalize-field #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,67 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "denormalize-field"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Len Smith"]
12
+ s.date = "2012-09-24"
13
+ s.description = "Denormalize ActiveRecord fields for performance reasons"
14
+ s.email = "len@barrison.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "denormalize-field.gemspec",
29
+ "lib/denormalize-field.rb",
30
+ "spec/denormalize-field_spec.rb",
31
+ "spec/schema.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = "http://github.com/Barrison/denormalize-field"
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = "1.8.23"
38
+ s.summary = "Denormalize ActiveRecord fields"
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.2.0"])
45
+ s.add_development_dependency(%q<pry>, [">= 0"])
46
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
47
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
48
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
50
+ else
51
+ s.add_dependency(%q<activerecord>, ["~> 3.2.0"])
52
+ s.add_dependency(%q<pry>, [">= 0"])
53
+ s.add_dependency(%q<sqlite3>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
55
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<activerecord>, ["~> 3.2.0"])
60
+ s.add_dependency(%q<pry>, [">= 0"])
61
+ s.add_dependency(%q<sqlite3>, [">= 0"])
62
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
63
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
65
+ end
66
+ end
67
+
@@ -0,0 +1,26 @@
1
+ require 'active_record'
2
+ require 'active_support'
3
+ require 'active_support/inflector'
4
+
5
+ module DenormalizeFields
6
+ def denormalizes(hash)
7
+ hash.keys.each do |key|
8
+ _field_name = hash[key]
9
+
10
+ before_save do
11
+ _denormalized_field_name = "#{key}_#{_field_name}"
12
+ self.send "#{_denormalized_field_name}=", self.send(key).send(_field_name)
13
+ _original_klass = self.class
14
+
15
+ _klass = key.to_s.camelize.constantize
16
+ _klass.after_save do
17
+ self.send(_original_klass.name.downcase.pluralize).each do |child|
18
+ child.update_attribute _denormalized_field_name, self.send(_field_name)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ ActiveRecord::Base.send :extend, DenormalizeFields
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class Category < ActiveRecord::Base
4
+ has_many :posts
5
+ end
6
+
7
+ class Post < ActiveRecord::Base
8
+ belongs_to :category
9
+ denormalizes category: :name
10
+ end
11
+
12
+ describe "DenormalizeField" do
13
+ let(:category) { Category.new(name: "News") }
14
+ let(:post) { Post.new(category: category) }
15
+
16
+ it "denormalizes fields on save" do
17
+ post.save!
18
+ post.reload
19
+ post.category_name.should == "News"
20
+ end
21
+
22
+ it "updates the denormalized field when the column changes" do
23
+ post.save!
24
+ category = Category.first
25
+ category.name = "Sports"
26
+ category.save!
27
+ post.reload
28
+ post.category_name.should == "Sports"
29
+ end
30
+ end
data/spec/schema.rb ADDED
File without changes
@@ -0,0 +1,30 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'denormalize-field'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ config.before(:each) do
12
+ Post.delete_all
13
+ Category.delete_all
14
+ end
15
+ end
16
+
17
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
18
+ :database => File.dirname(__FILE__) + "/denormalize-field.sqlite3")
19
+
20
+ ActiveRecord::Base.connection.drop_table(:categories)
21
+ ActiveRecord::Base.connection.drop_table(:posts)
22
+ ActiveRecord::Base.connection.create_table(:categories) do |t|
23
+ t.string :name
24
+ end
25
+ ActiveRecord::Base.connection.create_table(:posts) do |t|
26
+ t.string :category_id
27
+ t.string :category_name
28
+ end
29
+
30
+
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: denormalize-field
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Len Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: pry
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.8.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.8.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: rdoc
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '3.12'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '3.12'
94
+ - !ruby/object:Gem::Dependency
95
+ name: jeweler
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.8.4
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.8.4
110
+ description: Denormalize ActiveRecord fields for performance reasons
111
+ email: len@barrison.com
112
+ executables: []
113
+ extensions: []
114
+ extra_rdoc_files:
115
+ - LICENSE.txt
116
+ - README.rdoc
117
+ files:
118
+ - .document
119
+ - .rspec
120
+ - Gemfile
121
+ - Gemfile.lock
122
+ - LICENSE.txt
123
+ - README.rdoc
124
+ - Rakefile
125
+ - VERSION
126
+ - denormalize-field.gemspec
127
+ - lib/denormalize-field.rb
128
+ - spec/denormalize-field_spec.rb
129
+ - spec/schema.rb
130
+ - spec/spec_helper.rb
131
+ homepage: http://github.com/Barrison/denormalize-field
132
+ licenses:
133
+ - MIT
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ segments:
145
+ - 0
146
+ hash: 2097104229179456767
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubyforge_project:
155
+ rubygems_version: 1.8.23
156
+ signing_key:
157
+ specification_version: 3
158
+ summary: Denormalize ActiveRecord fields
159
+ test_files: []