serialize_virtual_attributes 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 46531c23ca6e85014e42461dcc33f17bf8a3e9e4
4
+ data.tar.gz: cf1ef697be33b5c5b2944b59f0d14fca30c14068
5
+ SHA512:
6
+ metadata.gz: a30fe8b26645dcbeab8d84032016ae732e6d9c98259317c83c1dd959c65dbcb913939852ab1d672f9903289b101893b564dbe740bd31c3e1412ebde662e10538
7
+ data.tar.gz: 50819e5f0dbfb3fe48728c1ce1276acc2bbb325862681a7c7e4a1da409f79472d2853d129510a5613105f0bd728990a26e48d4df6cfb73a70af6c39a0b969874
data/.gitignore ADDED
@@ -0,0 +1,33 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ spec/test.sqlite3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in serialize_virtual_attributes.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,90 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ serialize_virtual_attributes (0.0.1)
5
+ activerecord (~> 3.1)
6
+ activesupport (~> 3.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (3.2.18)
12
+ activesupport (= 3.2.18)
13
+ builder (~> 3.0.0)
14
+ activerecord (3.2.18)
15
+ activemodel (= 3.2.18)
16
+ activesupport (= 3.2.18)
17
+ arel (~> 3.0.2)
18
+ tzinfo (~> 0.3.29)
19
+ activesupport (3.2.18)
20
+ i18n (~> 0.6, >= 0.6.4)
21
+ multi_json (~> 1.0)
22
+ arel (3.0.3)
23
+ builder (3.0.4)
24
+ byebug (3.1.2)
25
+ columnize (~> 0.8)
26
+ debugger-linecache (~> 1.2)
27
+ celluloid (0.15.2)
28
+ timers (~> 1.1.0)
29
+ coderay (1.1.0)
30
+ columnize (0.8.9)
31
+ debugger-linecache (1.2.0)
32
+ diff-lcs (1.2.5)
33
+ ffi (1.9.3)
34
+ formatador (0.2.5)
35
+ guard (2.6.1)
36
+ formatador (>= 0.2.4)
37
+ listen (~> 2.7)
38
+ lumberjack (~> 1.0)
39
+ pry (>= 0.9.12)
40
+ thor (>= 0.18.1)
41
+ guard-rspec (4.2.9)
42
+ guard (~> 2.1)
43
+ rspec (>= 2.14, < 4.0)
44
+ i18n (0.6.9)
45
+ listen (2.7.6)
46
+ celluloid (>= 0.15.2)
47
+ rb-fsevent (>= 0.9.3)
48
+ rb-inotify (>= 0.9)
49
+ lumberjack (1.0.6)
50
+ method_source (0.8.2)
51
+ multi_json (1.10.1)
52
+ pry (0.9.12.6)
53
+ coderay (~> 1.0)
54
+ method_source (~> 0.8)
55
+ slop (~> 3.4)
56
+ rake (10.3.2)
57
+ rb-fsevent (0.9.4)
58
+ rb-inotify (0.9.4)
59
+ ffi (>= 0.5.0)
60
+ rspec (3.0.0)
61
+ rspec-core (~> 3.0.0)
62
+ rspec-expectations (~> 3.0.0)
63
+ rspec-mocks (~> 3.0.0)
64
+ rspec-core (3.0.0)
65
+ rspec-support (~> 3.0.0)
66
+ rspec-expectations (3.0.0)
67
+ diff-lcs (>= 1.2.0, < 2.0)
68
+ rspec-support (~> 3.0.0)
69
+ rspec-mocks (3.0.0)
70
+ rspec-support (~> 3.0.0)
71
+ rspec-support (3.0.0)
72
+ slop (3.5.0)
73
+ sqlite3 (1.3.9)
74
+ terminal-notifier-guard (1.5.3)
75
+ thor (0.19.1)
76
+ timers (1.1.0)
77
+ tzinfo (0.3.39)
78
+
79
+ PLATFORMS
80
+ ruby
81
+
82
+ DEPENDENCIES
83
+ bundler (~> 1.3)
84
+ byebug
85
+ guard-rspec
86
+ rake
87
+ rspec
88
+ serialize_virtual_attributes!
89
+ sqlite3
90
+ terminal-notifier-guard
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec 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
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Hao Liu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # SerializeVirtualAttributes
2
+
3
+ Dump/Reload the specified virtual attributes into/from serialized column
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'serialize_virtual_attributes'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install serialize_virtual_attributes
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class Person < ActiveRecord::Base
23
+ serialize :fullname, Hash
24
+
25
+ serialize_virtual_attrs :first_name, :last_name, to: fullname
26
+ end
27
+
28
+ person = Person.create(first_name: "Hao", last_name: "Liu")
29
+
30
+ person.reload
31
+
32
+ person.first_name # "Hao"
33
+ person.last_name # "Liu"
34
+ ```
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,46 @@
1
+ require "active_support"
2
+ require "active_record"
3
+
4
+ require "serialize_virtual_attributes/version"
5
+
6
+ module SerializeVirtualAttributes
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def serialize_virtual_attrs(*attrs, opts)
11
+ if attrs.nil? or (attrs.compact! and attrs.empty?)
12
+ raise ArgumentError.new("No virtual attributes provided")
13
+ end
14
+
15
+ to = HashWithIndifferentAccess.new(opts).fetch(:to) rescue nil
16
+
17
+ if to.blank?
18
+ raise ArgumentError.new("No serialized column provided")
19
+ end
20
+
21
+ if self.column_names.index(to.to_s).nil?
22
+ raise ArgumentError.new("`#{to}` is not a valid column of table `#{self.table_name}`")
23
+ end
24
+
25
+ attrs.each do |attr|
26
+ if self.column_names.index(attr.to_s).present?
27
+ raise ArgumentError.new("`#{attr}` is not supposed to be a virtual attribute")
28
+ end
29
+
30
+ if !self.accessible_attributes.include?(attr)
31
+ self.accessible_attributes << attr
32
+ end
33
+
34
+ define_method "#{attr}=" do |val|
35
+ self.public_send(to).send('[]=', attr, val)
36
+ end
37
+
38
+ define_method attr do
39
+ self.public_send(to).send('[]', attr)
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ ActiveRecord::Base.send(:include, SerializeVirtualAttributes)
@@ -0,0 +1,3 @@
1
+ module SerializeVirtualAttributes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'serialize_virtual_attributes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "serialize_virtual_attributes"
8
+ spec.version = SerializeVirtualAttributes::VERSION
9
+ spec.authors = ["Hao Liu"]
10
+ spec.email = ["leomayleomay@gmail.com"]
11
+ spec.description = %q{virtual attributes accessor via a serialized Hash column}
12
+ spec.summary = %q{Serialize specified virtual attributes into a Hash column}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", "~> 3.1"
22
+ spec.add_dependency "activesupport", "~> 3.1"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "guard-rspec"
28
+ spec.add_development_dependency "terminal-notifier-guard"
29
+ spec.add_development_dependency "sqlite3"
30
+ spec.add_development_dependency "byebug"
31
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe SerializeVirtualAttributes do
4
+ describe ".serialize_virtual_attrs" do
5
+ it "raises error if no virtual attributes provided" do
6
+ expect do
7
+ class Person < ActiveRecord::Base
8
+ serialize_virtual_attrs nil, to: :fullname
9
+ end
10
+ end.to raise_error(
11
+ ArgumentError,
12
+ "No virtual attributes provided"
13
+ )
14
+ end
15
+
16
+ it "raises error the virtual attributes are not that virtual" do
17
+ expect do
18
+ class Person < ActiveRecord::Base
19
+ serialize_virtual_attrs :fullname, to: :fullname
20
+ end
21
+ end.to raise_error(
22
+ ArgumentError,
23
+ "`fullname` is not supposed to be a virtual attribute"
24
+ )
25
+ end
26
+
27
+ it "raises error if no destination provided" do
28
+ expect do
29
+ class Person < ActiveRecord::Base
30
+ serialize_virtual_attrs :first_name, :last_name, to: nil
31
+ end
32
+ end.to raise_error(
33
+ ArgumentError,
34
+ "No serialized column provided"
35
+ )
36
+ end
37
+
38
+ it "raises error if the destination is not a field of the object" do
39
+ expect do
40
+ class Person < ActiveRecord::Base
41
+ serialize_virtual_attrs :first_name, :last_name, to: :body
42
+ end
43
+ end.to raise_error(
44
+ ArgumentError,
45
+ "`body` is not a valid column of table `people`"
46
+ )
47
+ end
48
+
49
+ it "appends the virtual attributes to the attr_accessible" do
50
+ class Person < ActiveRecord::Base
51
+ serialize_virtual_attrs :first_name, :last_name, to: :fullname
52
+ end
53
+
54
+ expect(Person.accessible_attributes).to include(:first_name)
55
+ expect(Person.accessible_attributes).to include(:last_name)
56
+ end
57
+
58
+ it "saves the virtual attributes in the serialized column" do
59
+ class Person < ActiveRecord::Base
60
+ serialize :fullname, Hash
61
+ serialize_virtual_attrs :first_name, :last_name, to: :fullname
62
+ end
63
+
64
+ person = Person.new(first_name: "hao", last_name: "liu")
65
+
66
+ expect(person.fullname[:first_name]).to eq "hao"
67
+ expect(person.fullname[:last_name]).to eq "liu"
68
+ end
69
+
70
+ it "loads the virtual attributes from the serialized column" do
71
+ class Person < ActiveRecord::Base
72
+ serialize :fullname, Hash
73
+ serialize_virtual_attrs :first_name, :last_name, to: :fullname
74
+ end
75
+
76
+ person = Person.create(first_name: "hao", last_name: "liu")
77
+ person.reload
78
+
79
+ expect(person.first_name).to eq "hao"
80
+ expect(person.last_name).to eq "liu"
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+
5
+ require 'serialize_virtual_attributes'
6
+
7
+ RSpec.configure do |config|
8
+ # Use color not only in STDOUT but also in pagers and files
9
+ config.tty = true
10
+
11
+ # Use the specified formatter
12
+ config.formatter = :documentation # :progress, :html
13
+ end
14
+
15
+ ActiveRecord::Base.establish_connection(
16
+ adapter: "sqlite3",
17
+ database: File.dirname(__FILE__) + "/test.sqlite3"
18
+ )
19
+
20
+ load File.dirname(__FILE__) + '/support/schema.rb'
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ create_table :people, force: true do |t|
5
+ t.string :fullname
6
+
7
+ t.timestamps
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serialize_virtual_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hao Liu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: terminal-notifier-guard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: virtual attributes accessor via a serialized Hash column
140
+ email:
141
+ - leomayleomay@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - Gemfile
148
+ - Gemfile.lock
149
+ - Guardfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - lib/serialize_virtual_attributes.rb
154
+ - lib/serialize_virtual_attributes/version.rb
155
+ - serialize_virtual_attributes.gemspec
156
+ - spec/lib/serialize_virtual_attributes_spec.rb
157
+ - spec/spec_helper.rb
158
+ - spec/support/schema.rb
159
+ homepage: ''
160
+ licenses:
161
+ - MIT
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.2.2
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: Serialize specified virtual attributes into a Hash column
183
+ test_files:
184
+ - spec/lib/serialize_virtual_attributes_spec.rb
185
+ - spec/spec_helper.rb
186
+ - spec/support/schema.rb