ar_attribute_serializer 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'activerecord', '>=3.0.0'
4
+
5
+ group :development do
6
+ gem 'bundler', '~> 1.0.0'
7
+ gem 'jeweler', '~> 1.5.2'
8
+ end
9
+
10
+ group :test do
11
+ gem 'sqlite3'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,33 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.5)
5
+ activesupport (= 3.0.5)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4)
8
+ activerecord (3.0.5)
9
+ activemodel (= 3.0.5)
10
+ activesupport (= 3.0.5)
11
+ arel (~> 2.0.2)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.5)
14
+ arel (2.0.9)
15
+ builder (2.1.2)
16
+ git (1.2.5)
17
+ i18n (0.5.0)
18
+ jeweler (1.5.2)
19
+ bundler (~> 1.0.0)
20
+ git (>= 1.2.5)
21
+ rake
22
+ rake (0.8.7)
23
+ sqlite3 (1.3.3)
24
+ tzinfo (0.3.25)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ activerecord (>= 3.0.0)
31
+ bundler (~> 1.0.0)
32
+ jeweler (~> 1.5.2)
33
+ sqlite3
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Oleg Khabarov
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.md ADDED
@@ -0,0 +1,19 @@
1
+ AR Attribute Serializer
2
+ =======================
3
+
4
+ **ar_attribute_serializer** is a simple method that allows you to serialize random attributes and use them just like any other attribute given model has.
5
+
6
+ Installation
7
+ ------------
8
+ Define the following in your Gemfile and run `bundle install`
9
+
10
+ gem 'ar_attribute_serializer'
11
+
12
+ Usage
13
+ -----
14
+
15
+
16
+
17
+ Copyright
18
+ =========
19
+ (c) 2011 Oleg Khabarov, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
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 = 'ar_attribute_serializer'
18
+ gem.homepage = 'http://github.com/GBH/ar_attribute_serializer'
19
+ gem.license = 'MIT'
20
+ gem.summary = 'Attribute serializer for AR models'
21
+ gem.description = 'Attribute serializer for AR models'
22
+ gem.email = "oleg@khabarov.ca"
23
+ gem.authors = ['Oleg Khabarov']
24
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
25
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
26
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
27
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/letmein.gemspec ADDED
@@ -0,0 +1,57 @@
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{letmein}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Oleg Khabarov"]
12
+ s.date = %q{2011-04-05}
13
+ s.description = %q{Serialization for AR Model accessors}
14
+ s.email = %q{oleg@khabarov.ca}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/ar_attribute_serializer.rb",
27
+ "test/ar_attribute_serializer_test.rb"
28
+ ]
29
+ s.homepage = %q{http://github.com/GBH/ar_attribute_serializer}
30
+ s.licenses = ["MIT"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.7}
33
+ s.summary = %q{Serialization for AR Model accessors}
34
+ s.test_files = [
35
+ "test/ar_attribute_serializer_test.rb"
36
+ ]
37
+
38
+ if s.respond_to? :specification_version then
39
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
46
+ else
47
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
48
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
50
+ end
51
+ else
52
+ s.add_dependency(%q<activerecord>, [">= 3.0.0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ end
56
+ end
57
+
@@ -0,0 +1,34 @@
1
+ require 'active_record'
2
+
3
+ module ArAttributeSerializer
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ # You need a blob field to store serialized data, and a list of attributes
12
+ # and their types. For example, with field called 'data':
13
+ # attribute_serializer :data, :name, :bio
14
+ def attribute_serializer(field, *attributes)
15
+
16
+ serialize field, Hash
17
+
18
+ attributes.each do |attribute|
19
+ class_eval %Q^
20
+ def #{attribute}
21
+ self.#{field} ||= { }
22
+ self.#{field}[:#{attribute}]
23
+ end
24
+ def #{attribute}=(value)
25
+ self.#{field} ||= { }
26
+ self.#{field}[:#{attribute}] = value
27
+ end
28
+ ^
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ ActiveRecord::Base.send :include, ArAttributeSerializer
@@ -0,0 +1,57 @@
1
+ require 'test/unit'
2
+ require 'ar_attribute_serializer'
3
+ require 'sqlite3'
4
+
5
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
6
+ $stdout_orig = $stdout
7
+ $stdout = StringIO.new
8
+
9
+ class Model < ActiveRecord::Base
10
+ attribute_serializer :data, :attr_1, :attr_2
11
+ end
12
+
13
+ class LetMeInTest < Test::Unit::TestCase
14
+
15
+ def setup
16
+ ActiveRecord::Base.logger
17
+ ActiveRecord::Schema.define(:version => 1) do
18
+ create_table :models do |t|
19
+ t.column :attr_0, :string
20
+ t.column :data, :text
21
+ end
22
+ end
23
+ end
24
+
25
+ def teardown
26
+ ActiveRecord::Base.connection.tables.each do |table|
27
+ ActiveRecord::Base.connection.drop_table(table)
28
+ end
29
+ end
30
+
31
+ def test_attribute_serializer
32
+ model = Model.new
33
+ assert model.respond_to?(:attr_0)
34
+ assert model.respond_to?(:data)
35
+ assert model.respond_to?(:attr_1)
36
+ assert model.respond_to?(:attr_2)
37
+
38
+ assert_equal nil, model.attr_1
39
+ assert_equal nil, model.attr_2
40
+
41
+ model.attr_0 = 'data_0'
42
+ model.attr_1 = 'data_1'
43
+ model.attr_2 = 'data_2'
44
+
45
+ assert_equal ({ :attr_1 => 'data_1', :attr_2 => 'data_2' }), model.data
46
+ assert_equal 'data_0', model.attr_0
47
+ assert_equal 'data_1', model.attr_1
48
+ assert_equal 'data_2', model.attr_2
49
+
50
+ model.save!
51
+
52
+ model.attr_1 = 'data_updated'
53
+ assert_equal 'data_updated', model.attr_1
54
+ assert_equal ({ :attr_1 => 'data_updated', :attr_2 => 'data_2' }), model.data
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_attribute_serializer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Oleg Khabarov
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-04-05 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ version: 3.0.0
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: jeweler
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 5
60
+ - 2
61
+ version: 1.5.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ description: Attribute serializer for AR models
66
+ email: oleg@khabarov.ca
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.md
74
+ files:
75
+ - Gemfile
76
+ - Gemfile.lock
77
+ - LICENSE
78
+ - README.md
79
+ - Rakefile
80
+ - VERSION
81
+ - letmein.gemspec
82
+ - lib/ar_attribute_serializer.rb
83
+ - test/ar_attribute_serializer_test.rb
84
+ has_rdoc: true
85
+ homepage: http://github.com/GBH/ar_attribute_serializer
86
+ licenses:
87
+ - MIT
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: -2666110992195777503
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.3.7
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Attribute serializer for AR models
117
+ test_files:
118
+ - test/ar_attribute_serializer_test.rb