packed_fields 0.0.1
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/.rspec +1 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +44 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +28 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/active_record/packed_fields.rb +34 -0
- data/lib/packed_fields.rb +2 -0
- data/packed_fields.gemspec +59 -0
- data/spec/active_record/packed_fields_spec.rb +55 -0
- data/spec/spec_helper.rb +34 -0
- metadata +113 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.9)
|
5
|
+
activesupport (= 3.0.9)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.5.0)
|
8
|
+
activerecord (3.0.9)
|
9
|
+
activemodel (= 3.0.9)
|
10
|
+
activesupport (= 3.0.9)
|
11
|
+
arel (~> 2.0.10)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.9)
|
14
|
+
arel (2.0.10)
|
15
|
+
builder (2.1.2)
|
16
|
+
diff-lcs (1.1.2)
|
17
|
+
git (1.2.5)
|
18
|
+
i18n (0.5.0)
|
19
|
+
jeweler (1.6.4)
|
20
|
+
bundler (~> 1.0)
|
21
|
+
git (>= 1.2.5)
|
22
|
+
rake
|
23
|
+
rake (0.9.2)
|
24
|
+
rspec (2.6.0)
|
25
|
+
rspec-core (~> 2.6.0)
|
26
|
+
rspec-expectations (~> 2.6.0)
|
27
|
+
rspec-mocks (~> 2.6.0)
|
28
|
+
rspec-core (2.6.4)
|
29
|
+
rspec-expectations (2.6.0)
|
30
|
+
diff-lcs (~> 1.1.2)
|
31
|
+
rspec-mocks (2.6.0)
|
32
|
+
sqlite3 (1.3.3)
|
33
|
+
sqlite3-ruby (1.3.3)
|
34
|
+
sqlite3 (>= 1.3.3)
|
35
|
+
tzinfo (0.3.29)
|
36
|
+
|
37
|
+
PLATFORMS
|
38
|
+
ruby
|
39
|
+
|
40
|
+
DEPENDENCIES
|
41
|
+
activerecord
|
42
|
+
jeweler
|
43
|
+
rspec
|
44
|
+
sqlite3-ruby
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 holysugar
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the "Software"),
|
5
|
+
to deal in the Software without restriction, including without limitation
|
6
|
+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
7
|
+
and/or sell copies of the Software, and to permit persons to whom the
|
8
|
+
Software is furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included
|
11
|
+
in all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
14
|
+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
18
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
19
|
+
DEALINGS IN THE SOFTWARE.
|
20
|
+
|
data/README.markdown
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
packed_fields
|
2
|
+
============
|
3
|
+
|
4
|
+
Wrapper of ActiveRecord::Base.serialize
|
5
|
+
|
6
|
+
example
|
7
|
+
-------
|
8
|
+
|
9
|
+
schema
|
10
|
+
|
11
|
+
ActiveRecord::Schema.define(:version => 1) do
|
12
|
+
create_table :mixins do |t|
|
13
|
+
t.column :packed, :text
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
model
|
18
|
+
|
19
|
+
class Mixin < ActiveRecord::Base
|
20
|
+
packed :fields => [:foo, :bar]
|
21
|
+
end
|
22
|
+
|
23
|
+
and using
|
24
|
+
|
25
|
+
m = Mixins.new
|
26
|
+
m.foo = 'blah'
|
27
|
+
m.save #=> #<Mixin id: 1, packed: {:foo=>"blah"}>
|
28
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
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 = "packed_fields"
|
18
|
+
gem.homepage = "http://github.com/holysugar/packed_fields"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Wrapper of ActiveRecord::Base#serialize}
|
21
|
+
gem.description = %Q{to access hashed fields more easily}
|
22
|
+
gem.email = "holysugar@gmail.com"
|
23
|
+
gem.authors = ["holysugar"]
|
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
|
+
RDoc::Task.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "packed_fields #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
50
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module PackedFields
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def packed(options)
|
6
|
+
column = options[:column] || :packed
|
7
|
+
fields = options[:fields]
|
8
|
+
|
9
|
+
serialize column, Hash
|
10
|
+
|
11
|
+
fields.each do |field|
|
12
|
+
define_method(field) {
|
13
|
+
hash = read_attribute(column)
|
14
|
+
if hash
|
15
|
+
hash[field]
|
16
|
+
end
|
17
|
+
}
|
18
|
+
define_method("#{field}="){|value|
|
19
|
+
hash = read_attribute(column)
|
20
|
+
if hash
|
21
|
+
hash[field] = value
|
22
|
+
else
|
23
|
+
write_attribute(column, { field => value })
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.included(base)
|
31
|
+
base.extend ClassMethods
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,59 @@
|
|
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{packed_fields}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["holysugar"]
|
12
|
+
s.date = %q{2011-07-12}
|
13
|
+
s.description = %q{to access hashed fields more easily}
|
14
|
+
s.email = %q{holysugar@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.markdown"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".rspec",
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"MIT-LICENSE",
|
23
|
+
"README.markdown",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/active_record/packed_fields.rb",
|
27
|
+
"lib/packed_fields.rb",
|
28
|
+
"packed_fields.gemspec",
|
29
|
+
"spec/active_record/packed_fields_spec.rb",
|
30
|
+
"spec/spec_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/holysugar/packed_fields}
|
33
|
+
s.licenses = ["MIT"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.6.2}
|
36
|
+
s.summary = %q{Wrapper of ActiveRecord::Base#serialize}
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
43
|
+
s.add_runtime_dependency(%q<rspec>, [">= 0"])
|
44
|
+
s.add_runtime_dependency(%q<sqlite3-ruby>, [">= 0"])
|
45
|
+
s.add_runtime_dependency(%q<jeweler>, [">= 0"])
|
46
|
+
else
|
47
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
48
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
49
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
50
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
51
|
+
end
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
54
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
55
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
56
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'packed_fields'
|
3
|
+
|
4
|
+
class ModelWithPacked < ActiveRecord::Base
|
5
|
+
set_table_name 'mixins'
|
6
|
+
|
7
|
+
packed :fields => [:foo, :bar]
|
8
|
+
end
|
9
|
+
|
10
|
+
class CanSetColumnName < ActiveRecord::Base
|
11
|
+
set_table_name 'second_mixins'
|
12
|
+
|
13
|
+
packed :column => :serialized, :fields => [:foo, :bar]
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
describe ModelWithPacked do
|
18
|
+
before(:all) { setup_db }
|
19
|
+
after(:all) { teardown_db }
|
20
|
+
|
21
|
+
let(:model) { ModelWithPacked.new }
|
22
|
+
|
23
|
+
describe "setting foo 'blah' and bar 1" do
|
24
|
+
subject {
|
25
|
+
model.foo = "blah"
|
26
|
+
model.bar = 1
|
27
|
+
model.save
|
28
|
+
model.reload
|
29
|
+
}
|
30
|
+
its(:foo) { should eq 'blah' }
|
31
|
+
its(:bar) { should eq 1 }
|
32
|
+
|
33
|
+
it "packed[:foo] should == 'blah'" do
|
34
|
+
subject.packed[:foo].should eq 'blah'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "set foo and save changes packed" do
|
39
|
+
expect { model.foo = "blah" ; model.save }.to change { model.packed }
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
describe CanSetColumnName do
|
46
|
+
before(:all) { setup_db }
|
47
|
+
after(:all) { teardown_db }
|
48
|
+
let(:model) { CanSetColumnName.new }
|
49
|
+
|
50
|
+
it "saving packed changes given column name" do
|
51
|
+
expect { model.foo = "blah" ; model.save }.to change { model.serialized }
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'active_record'
|
5
|
+
require 'packed_fields'
|
6
|
+
|
7
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
14
|
+
|
15
|
+
def setup_db
|
16
|
+
ActiveRecord::Migration.suppress_messages do
|
17
|
+
ActiveRecord::Schema.define(:version => 1) do
|
18
|
+
create_table :mixins do |t|
|
19
|
+
t.column :packed, :text
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table :second_mixins do |t|
|
23
|
+
t.column :serialized, :text
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def teardown_db
|
30
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
31
|
+
ActiveRecord::Base.connection.drop_table(table)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: packed_fields
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- holysugar
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-12 00:00:00 +09:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: sqlite3-ruby
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: jeweler
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :runtime
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
description: to access hashed fields more easily
|
61
|
+
email: holysugar@gmail.com
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files:
|
67
|
+
- README.markdown
|
68
|
+
files:
|
69
|
+
- .rspec
|
70
|
+
- Gemfile
|
71
|
+
- Gemfile.lock
|
72
|
+
- MIT-LICENSE
|
73
|
+
- README.markdown
|
74
|
+
- Rakefile
|
75
|
+
- VERSION
|
76
|
+
- lib/active_record/packed_fields.rb
|
77
|
+
- lib/packed_fields.rb
|
78
|
+
- packed_fields.gemspec
|
79
|
+
- spec/active_record/packed_fields_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
has_rdoc: true
|
82
|
+
homepage: http://github.com/holysugar/packed_fields
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
hash: 3629343655210914779
|
96
|
+
segments:
|
97
|
+
- 0
|
98
|
+
version: "0"
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: "0"
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.6.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: Wrapper of ActiveRecord::Base#serialize
|
112
|
+
test_files: []
|
113
|
+
|