has_serialized 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,8 @@
1
+ Autotest.add_hook :initialize do |autotest|
2
+ %w{.git .svn .hg .DS_Store ._* vendor tmp log doc db}.each do |exception|
3
+ autotest.add_exception(exception)
4
+ end
5
+ autotest.add_mapping(%r{^lib/.*\.rb$}) do |f, _|
6
+ Dir['spec/**/*.rb']
7
+ end
8
+ end
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ree-1.8.7-2011.03@has_serialized
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "activerecord", ">= 3.0.0"
5
+ # Add dependencies to develop your gem here.
6
+ # Include everything needed to run rake, tests, features, etc.
7
+ group :development do
8
+ gem "rspec", "~> 2.6.0"
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ gem "rcov", ">= 0"
12
+ gem "guard-rspec"
13
+ gem "libnotify"
14
+ gem "sqlite3"
15
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.7)
5
+ activesupport (= 3.0.7)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activerecord (3.0.7)
9
+ activemodel (= 3.0.7)
10
+ activesupport (= 3.0.7)
11
+ arel (~> 2.0.2)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.7)
14
+ arel (2.0.10)
15
+ builder (2.1.2)
16
+ diff-lcs (1.1.2)
17
+ ffi (1.0.8)
18
+ git (1.2.5)
19
+ guard (0.3.4)
20
+ thor (~> 0.14.6)
21
+ guard-rspec (0.3.1)
22
+ guard (>= 0.2.2)
23
+ i18n (0.5.0)
24
+ jeweler (1.5.2)
25
+ bundler (~> 1.0.0)
26
+ git (>= 1.2.5)
27
+ rake
28
+ libnotify (0.5.1)
29
+ ffi (~> 1.0)
30
+ rake (0.8.7)
31
+ rcov (0.9.9)
32
+ rspec (2.6.0)
33
+ rspec-core (~> 2.6.0)
34
+ rspec-expectations (~> 2.6.0)
35
+ rspec-mocks (~> 2.6.0)
36
+ rspec-core (2.6.1)
37
+ rspec-expectations (2.6.0)
38
+ diff-lcs (~> 1.1.2)
39
+ rspec-mocks (2.6.0)
40
+ sqlite3 (1.3.3)
41
+ thor (0.14.6)
42
+ tzinfo (0.3.27)
43
+
44
+ PLATFORMS
45
+ ruby
46
+
47
+ DEPENDENCIES
48
+ activerecord (>= 3.0.0)
49
+ bundler (~> 1.0.0)
50
+ guard-rspec
51
+ jeweler (~> 1.5.2)
52
+ libnotify
53
+ rcov
54
+ rspec (~> 2.6.0)
55
+ sqlite3
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb})
6
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 [name of plugin creator]
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.textile ADDED
@@ -0,0 +1,36 @@
1
+ h1. AttributeSerializer
2
+
3
+ Based on the Attribute Serializer article written by Greg Moreno
4
+ http://gregmoreno.ca/preventing-model-explosion-via-rails-serialization/
5
+
6
+ h2. Example
7
+
8
+ <pre>
9
+ class User < ActiveRecord::Base
10
+ has_serialized :preferences, :show_email => false, :locale => :en
11
+ end
12
+
13
+ u = User.new # automatically assigns the default preferences
14
+ u.preferences
15
+ => {:show_email => false, :locale => :en}
16
+
17
+ u.show_email = true # I can change it like an attribute via @user.update_attributes(params[:user])
18
+ u.preferences
19
+ => {:show_email => true, :locale => :en }
20
+ </pre>
21
+
22
+ h2. Specs
23
+
24
+ Run specs through @rake spec@ or @guard start@ for autotest-like testing.
25
+
26
+ h2. Contributing to has_serialized
27
+
28
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
29
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
30
+ * Fork the project
31
+ * Start a feature/bugfix branch
32
+ * Commit and push until you are happy with your contribution
33
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
34
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
35
+
36
+ Copyright (c) 2011 Greg Moreno, Adam Cuppy and Ramon Tayag. Released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "has_serialized"
16
+ gem.homepage = "http://github.com/ramon.tayag/has_serialized"
17
+ gem.license = "MIT"
18
+ gem.description = %q{Adds dynamic attributes through serialize. This way, it's in one table, and it's still trivial to track changes to your records.}
19
+ gem.summary = %Q{Adds dynamic attributes through serialize.}
20
+ gem.email = "ramon@tayag.net"
21
+ gem.authors = ["Ramon Tayag"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "has_serialized #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,81 @@
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 = %q{has_serialized}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ramon Tayag"]
12
+ s.date = %q{2011-05-20}
13
+ s.description = %q{Adds dynamic attributes through serialize. This way, it's in one table, and it's still trivial to track changes to your records.}
14
+ s.email = %q{ramon@tayag.net}
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ ".autotest",
20
+ ".rvmrc",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "Guardfile",
24
+ "MIT-LICENSE",
25
+ "README.textile",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "has_serialized.gemspec",
29
+ "lib/has_serialized.rb",
30
+ "lib/has_serialized/class_methods.rb",
31
+ "spec/fixtures/category.rb",
32
+ "spec/fixtures/page.rb",
33
+ "spec/has_serialized_spec.rb",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/ramon.tayag/has_serialized}
37
+ s.licenses = ["MIT"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.6.2}
40
+ s.summary = %q{Adds dynamic attributes through serialize.}
41
+ s.test_files = [
42
+ "spec/fixtures/category.rb",
43
+ "spec/fixtures/page.rb",
44
+ "spec/has_serialized_spec.rb",
45
+ "spec/spec_helper.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
53
+ s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
54
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
56
+ s.add_development_dependency(%q<rcov>, [">= 0"])
57
+ s.add_development_dependency(%q<guard-rspec>, [">= 0"])
58
+ s.add_development_dependency(%q<libnotify>, [">= 0"])
59
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
60
+ else
61
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
62
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
63
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
65
+ s.add_dependency(%q<rcov>, [">= 0"])
66
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
67
+ s.add_dependency(%q<libnotify>, [">= 0"])
68
+ s.add_dependency(%q<sqlite3>, [">= 0"])
69
+ end
70
+ else
71
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
72
+ s.add_dependency(%q<rspec>, ["~> 2.6.0"])
73
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
74
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
75
+ s.add_dependency(%q<rcov>, [">= 0"])
76
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
77
+ s.add_dependency(%q<libnotify>, [">= 0"])
78
+ s.add_dependency(%q<sqlite3>, [">= 0"])
79
+ end
80
+ end
81
+
@@ -0,0 +1,33 @@
1
+ module HasSerialized
2
+ module ClassMethods
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ private
7
+
8
+ # Creates the accessors so that you can assign values like so:
9
+ # record.my_own_attribute = value
10
+ # record.my_own_attribute # value
11
+ def self.serialized_attr_accessor(serialized, accessors)
12
+ accessors.keys.each do |k|
13
+ define_method("#{k}") do
14
+ self[serialized] && self[serialized][k]
15
+ end
16
+ define_method("#{k}=") do |value|
17
+ self[serialized][k] = value
18
+ end
19
+ end
20
+ end
21
+
22
+ # Sets the default value of the serialized field
23
+ def self.default_serialized_attr(serialized, accessors)
24
+ method_name = "set_default_#{serialized}"
25
+ after_initialize method_name
26
+
27
+ define_method(method_name) do
28
+ self[serialized] = accessors if self[serialized].nil?
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ require 'has_serialized/class_methods'
2
+ module HasSerialized
3
+ def has_serialized(serialized, serialized_accessors={})
4
+ include HasSerialized::ClassMethods
5
+
6
+ self.serialize serialized, serialized_accessors.class
7
+ self.serialized_attr_accessor serialized, serialized_accessors
8
+ self.default_serialized_attr serialized, serialized_accessors
9
+ end
10
+ end
11
+
12
+ # Apply HasSerialized module to ActiveRecord, so that has_serialized will be available to ActiveRecord
13
+ ActiveRecord::Base.extend HasSerialized
@@ -0,0 +1,2 @@
1
+ class Category < ActiveRecord::Base
2
+ end
@@ -0,0 +1,2 @@
1
+ class Page < ActiveRecord::Base
2
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasSerialized do
4
+ before do
5
+ reset_database
6
+ end
7
+
8
+ describe "accessing variables" do
9
+ before do
10
+ Page.class_eval do
11
+ has_serialized :dynamic_attributes, :gunslinger => true
12
+ end
13
+ @page = Page.create!
14
+ end
15
+
16
+ it "should serialize with default" do
17
+ @page.gunslinger.should be_true
18
+ end
19
+
20
+ it "should allow assignment" do
21
+ @page.gunslinger = false
22
+ @page.gunslinger.should be_false
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,39 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rubygems'
4
+ require 'active_record'
5
+ require 'rspec'
6
+ require 'has_serialized'
7
+ require 'spec/fixtures/category'
8
+ require 'spec/fixtures/page'
9
+
10
+ #Allow to connect to SQLite
11
+ root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
12
+ Dir.mkdir("db") unless File.directory?("db")
13
+ ActiveRecord::Base.establish_connection(
14
+ :adapter => "sqlite3",
15
+ :database => "#{root}/db/test.db"
16
+ )
17
+
18
+ # Requires supporting files with custom matchers and macros, etc,
19
+ # in ./support/ and its subdirectories.
20
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
21
+
22
+ RSpec.configure do |config|
23
+ end
24
+
25
+ def reset_database
26
+ %W(pages categories).each do |table_name|
27
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS '#{table_name}'")
28
+ end
29
+ ActiveRecord::Base.connection.create_table(:pages) do |t|
30
+ t.string :name
31
+ t.text :dynamic_attributes
32
+ t.integer :category_id
33
+ end
34
+ ActiveRecord::Base.connection.create_table(:categories) do |t|
35
+ t.string :name
36
+ t.text :definition
37
+ end
38
+ end
39
+
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_serialized
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Ramon Tayag
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-20 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 0
33
+ version: 3.0.0
34
+ name: activerecord
35
+ version_requirements: *id001
36
+ prerelease: false
37
+ - !ruby/object:Gem::Dependency
38
+ type: :development
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 2
47
+ - 6
48
+ - 0
49
+ version: 2.6.0
50
+ name: rspec
51
+ version_requirements: *id002
52
+ prerelease: false
53
+ - !ruby/object:Gem::Dependency
54
+ type: :development
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 23
61
+ segments:
62
+ - 1
63
+ - 0
64
+ - 0
65
+ version: 1.0.0
66
+ name: bundler
67
+ version_requirements: *id003
68
+ prerelease: false
69
+ - !ruby/object:Gem::Dependency
70
+ type: :development
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 7
77
+ segments:
78
+ - 1
79
+ - 5
80
+ - 2
81
+ version: 1.5.2
82
+ name: jeweler
83
+ version_requirements: *id004
84
+ prerelease: false
85
+ - !ruby/object:Gem::Dependency
86
+ type: :development
87
+ requirement: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ name: rcov
97
+ version_requirements: *id005
98
+ prerelease: false
99
+ - !ruby/object:Gem::Dependency
100
+ type: :development
101
+ requirement: &id006 !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ name: guard-rspec
111
+ version_requirements: *id006
112
+ prerelease: false
113
+ - !ruby/object:Gem::Dependency
114
+ type: :development
115
+ requirement: &id007 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ name: libnotify
125
+ version_requirements: *id007
126
+ prerelease: false
127
+ - !ruby/object:Gem::Dependency
128
+ type: :development
129
+ requirement: &id008 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ hash: 3
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ name: sqlite3
139
+ version_requirements: *id008
140
+ prerelease: false
141
+ description: Adds dynamic attributes through serialize. This way, it's in one table, and it's still trivial to track changes to your records.
142
+ email: ramon@tayag.net
143
+ executables: []
144
+
145
+ extensions: []
146
+
147
+ extra_rdoc_files:
148
+ - README.textile
149
+ files:
150
+ - .autotest
151
+ - .rvmrc
152
+ - Gemfile
153
+ - Gemfile.lock
154
+ - Guardfile
155
+ - MIT-LICENSE
156
+ - README.textile
157
+ - Rakefile
158
+ - VERSION
159
+ - has_serialized.gemspec
160
+ - lib/has_serialized.rb
161
+ - lib/has_serialized/class_methods.rb
162
+ - spec/fixtures/category.rb
163
+ - spec/fixtures/page.rb
164
+ - spec/has_serialized_spec.rb
165
+ - spec/spec_helper.rb
166
+ has_rdoc: true
167
+ homepage: http://github.com/ramon.tayag/has_serialized
168
+ licenses:
169
+ - MIT
170
+ post_install_message:
171
+ rdoc_options: []
172
+
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ none: false
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ hash: 3
181
+ segments:
182
+ - 0
183
+ version: "0"
184
+ required_rubygems_version: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ hash: 3
190
+ segments:
191
+ - 0
192
+ version: "0"
193
+ requirements: []
194
+
195
+ rubyforge_project:
196
+ rubygems_version: 1.6.2
197
+ signing_key:
198
+ specification_version: 3
199
+ summary: Adds dynamic attributes through serialize.
200
+ test_files:
201
+ - spec/fixtures/category.rb
202
+ - spec/fixtures/page.rb
203
+ - spec/has_serialized_spec.rb
204
+ - spec/spec_helper.rb