extra_attributes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +60 -0
- data/LICENSE +7 -0
- data/README.md +58 -0
- data/Rakefile +11 -0
- data/extra_attributes.gemspec +31 -0
- data/lib/extra_attribute.rb +24 -0
- data/lib/version.rb +3 -0
- data/spec/db/schema.rb +15 -0
- data/spec/lib/extra_attribute_spec.rb +59 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/support/child.rb +3 -0
- data/spec/support/person.rb +11 -0
- metadata +146 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use --create ruby-1.9.3@extra_attributes
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
extra_attributes (0.0.1)
|
5
|
+
activerecord (~> 3.1)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.2.8)
|
11
|
+
activesupport (= 3.2.8)
|
12
|
+
builder (~> 3.0.0)
|
13
|
+
activerecord (3.2.8)
|
14
|
+
activemodel (= 3.2.8)
|
15
|
+
activesupport (= 3.2.8)
|
16
|
+
arel (~> 3.0.2)
|
17
|
+
tzinfo (~> 0.3.29)
|
18
|
+
activesupport (3.2.8)
|
19
|
+
i18n (~> 0.6)
|
20
|
+
multi_json (~> 1.0)
|
21
|
+
archive-tar-minitar (0.5.2)
|
22
|
+
arel (3.0.2)
|
23
|
+
builder (3.0.0)
|
24
|
+
columnize (0.3.6)
|
25
|
+
diff-lcs (1.1.3)
|
26
|
+
i18n (0.6.0)
|
27
|
+
linecache19 (0.5.12)
|
28
|
+
ruby_core_source (>= 0.1.4)
|
29
|
+
multi_json (1.3.6)
|
30
|
+
rake (0.9.2.2)
|
31
|
+
rspec (2.6.0)
|
32
|
+
rspec-core (~> 2.6.0)
|
33
|
+
rspec-expectations (~> 2.6.0)
|
34
|
+
rspec-mocks (~> 2.6.0)
|
35
|
+
rspec-core (2.6.4)
|
36
|
+
rspec-expectations (2.6.0)
|
37
|
+
diff-lcs (~> 1.1.2)
|
38
|
+
rspec-mocks (2.6.0)
|
39
|
+
ruby-debug-base19 (0.11.25)
|
40
|
+
columnize (>= 0.3.1)
|
41
|
+
linecache19 (>= 0.5.11)
|
42
|
+
ruby_core_source (>= 0.1.4)
|
43
|
+
ruby-debug19 (0.11.6)
|
44
|
+
columnize (>= 0.3.1)
|
45
|
+
linecache19 (>= 0.5.11)
|
46
|
+
ruby-debug-base19 (>= 0.11.19)
|
47
|
+
ruby_core_source (0.1.5)
|
48
|
+
archive-tar-minitar (>= 0.5.2)
|
49
|
+
sqlite3 (1.3.6)
|
50
|
+
tzinfo (0.3.33)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
extra_attributes!
|
57
|
+
rake
|
58
|
+
rspec (~> 2.6.0)
|
59
|
+
ruby-debug19
|
60
|
+
sqlite3 (~> 1.3.3)
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Manic Chuang
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# extra\_attributes
|
2
|
+
|
3
|
+
by Manic <http://tech.manic.tw>
|
4
|
+
|
5
|
+
## LINKS:
|
6
|
+
|
7
|
+
* [github](https://github.com/manic/extra_attributes)
|
8
|
+
|
9
|
+
## INSTALLATION:
|
10
|
+
|
11
|
+
gem install extra_attributes
|
12
|
+
|
13
|
+
## USAGE:
|
14
|
+
|
15
|
+
### Create the migration for your model
|
16
|
+
|
17
|
+
create_table :people do |t|
|
18
|
+
t.text :extra_data # Name this column whatever you like just *make sure* it is a TEXT field
|
19
|
+
....
|
20
|
+
end
|
21
|
+
|
22
|
+
### Configure extra\_attributes in your model
|
23
|
+
|
24
|
+
class Person < ActiveRecord::Base
|
25
|
+
include ExtraAttribute
|
26
|
+
|
27
|
+
extra_attributes :extra_data, [:name, :nickname] # This sets up the column for your custom attributes
|
28
|
+
end
|
29
|
+
|
30
|
+
### Set and Get custom attributes as if they were a normal attribute on your model
|
31
|
+
|
32
|
+
person = Person.new
|
33
|
+
person.name = 'Joe'
|
34
|
+
person.nickname = 'Joey'
|
35
|
+
person.name ~> 'Joe'
|
36
|
+
person.nickname ~> 'Joey'
|
37
|
+
|
38
|
+
person = Person.new :name => 'Joe'
|
39
|
+
person.save!
|
40
|
+
person.name ~> 'Joe'
|
41
|
+
|
42
|
+
## LICENSE:
|
43
|
+
|
44
|
+
Apache License 2.0
|
45
|
+
|
46
|
+
Copyright (c) 2012, PIXNET
|
47
|
+
|
48
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
49
|
+
you may not use this file except in compliance with the License.
|
50
|
+
You may obtain a copy of the License at
|
51
|
+
|
52
|
+
<http://www.apache.org/licenses/LICENSE-2.0>
|
53
|
+
|
54
|
+
Unless required by applicable law or agreed to in writing, software
|
55
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
56
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
57
|
+
See the License for the specific language governing permissions and
|
58
|
+
limitations under the License.
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "extra_attributes"
|
7
|
+
s.version = ExtraAttribute::VERSION
|
8
|
+
s.authors = ["Manic"]
|
9
|
+
s.email = ["maniclf@gmail.com"]
|
10
|
+
s.homepage = %q{http://github.com/manic/extra_attributes}
|
11
|
+
s.summary = %q{A simple alternative to acts_as_eav_model.}
|
12
|
+
s.description = %q{A simple alternative to acts_as_eav_model. Use JSON to store extra attrubutes.}
|
13
|
+
|
14
|
+
s.files = [".gitignore", ".rvmrc", "Gemfile", "Gemfile.lock", "LICENSE", "README.md", "Rakefile", "extra_attributes.gemspec", "lib/extra_attribute.rb", "lib/version.rb"]
|
15
|
+
s.test_files = ["spec/db/schema.rb", "spec/lib/extra_attribute_spec.rb", "spec/spec_helper.rb", "spec/support/child.rb", "spec/support/person.rb"]
|
16
|
+
s.executables = []
|
17
|
+
s.extra_rdoc_files = [ "README.md" ]
|
18
|
+
s.require_path = "lib"
|
19
|
+
|
20
|
+
s.add_dependency 'activerecord', '~> 3.1'
|
21
|
+
unless ENV["CI"]
|
22
|
+
if RUBY_VERSION <= '1.9.1'
|
23
|
+
s.add_development_dependency 'ruby-debug'
|
24
|
+
else
|
25
|
+
s.add_development_dependency 'ruby-debug19'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
s.add_development_dependency 'sqlite3', '~> 1.3.3'
|
29
|
+
s.add_development_dependency 'rake'
|
30
|
+
s.add_development_dependency 'rspec', '~> 2.6.0'
|
31
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ExtraAttribute
|
2
|
+
module ClassMethods
|
3
|
+
def extra_attributes(column, attribute_arrays)
|
4
|
+
@@column = column.to_s
|
5
|
+
attribute_arrays.each do |attr|
|
6
|
+
define_method "#{attr}" do
|
7
|
+
extra_data ||= JSON.parse(attributes[@@column] ? attributes[@@column] : '{}')
|
8
|
+
return extra_data[attr.to_s].present? ? extra_data[attr.to_s] : nil
|
9
|
+
end
|
10
|
+
|
11
|
+
define_method "#{attr}=" do |val|
|
12
|
+
extra_data ||= JSON.parse(attributes[@@column] ? attributes[@@column] : '{}')
|
13
|
+
extra_data[attr.to_s] = val
|
14
|
+
self.send("#{@@column}=", extra_data.to_json)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.included(base)
|
21
|
+
base.extend ClassMethods
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/lib/version.rb
ADDED
data/spec/db/schema.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Schema.define do
|
4
|
+
create_table 'people', :force => true do |t|
|
5
|
+
t.string 'age'
|
6
|
+
t.text 'extra_attributes'
|
7
|
+
t.datetime 'created_at'
|
8
|
+
t.datetime 'updated_at'
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table 'children', :force => true do |t|
|
12
|
+
t.string 'name'
|
13
|
+
t.belongs_to :person
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ExtraAttribute do
|
4
|
+
describe "Expected ActiveRecord behavior" do
|
5
|
+
describe "common" do
|
6
|
+
it "handles an empty string of attributes" do
|
7
|
+
person = Person.create(:name =>'')
|
8
|
+
person.should_not be_new_record
|
9
|
+
end
|
10
|
+
it "sets all of the attributes" do
|
11
|
+
person = Person.create!({
|
12
|
+
:age=>99,
|
13
|
+
:name => "John"
|
14
|
+
})
|
15
|
+
person.age.should eql(99)
|
16
|
+
person.name.should eql('John')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "A custom attribute" do
|
22
|
+
describe "John is a Person" do
|
23
|
+
before( :each ) do
|
24
|
+
@person = Person.new
|
25
|
+
end
|
26
|
+
it "who knows his name" do
|
27
|
+
@person.name = 'John'
|
28
|
+
@person.name.should eql('John')
|
29
|
+
end
|
30
|
+
|
31
|
+
it "who can change his name" do
|
32
|
+
@person.name = 'John'
|
33
|
+
@person.name = 'Joe'
|
34
|
+
@person.name.should eql('Joe')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "John is a Person whose name is changed" do
|
39
|
+
before( :each ) do
|
40
|
+
@person = Person.create(:name => "John")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "who changes his name but not save." do
|
44
|
+
@person.name = 'Koh'
|
45
|
+
@person.name.should eql('Koh')
|
46
|
+
@person.reload
|
47
|
+
@person.name.should eql('John')
|
48
|
+
end
|
49
|
+
|
50
|
+
it "who changes his name and saved." do
|
51
|
+
@person.name = 'Koh'
|
52
|
+
@person.name.should eql('Koh')
|
53
|
+
@person.save
|
54
|
+
@person.reload
|
55
|
+
@person.name.should eql('Koh')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
Bundler.setup
|
5
|
+
require 'rspec'
|
6
|
+
|
7
|
+
require 'active_record'
|
8
|
+
require 'sqlite3'
|
9
|
+
require 'logger'
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection({
|
12
|
+
:adapter => 'sqlite3',
|
13
|
+
:database => ":memory:"
|
14
|
+
})
|
15
|
+
load('db/schema.rb')
|
16
|
+
|
17
|
+
$:.push File.expand_path("../lib", __FILE__)
|
18
|
+
require 'support/person'
|
19
|
+
require 'support/child'
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.mock_with :rspec
|
23
|
+
end
|
24
|
+
|
25
|
+
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: extra_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Manic
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-14 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.1'
|
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.1'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: ruby-debug19
|
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: 1.3.3
|
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: 1.3.3
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '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: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 2.6.0
|
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: 2.6.0
|
94
|
+
description: A simple alternative to acts_as_eav_model. Use JSON to store extra attrubutes.
|
95
|
+
email:
|
96
|
+
- maniclf@gmail.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files:
|
100
|
+
- README.md
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- .rvmrc
|
104
|
+
- Gemfile
|
105
|
+
- Gemfile.lock
|
106
|
+
- LICENSE
|
107
|
+
- README.md
|
108
|
+
- Rakefile
|
109
|
+
- extra_attributes.gemspec
|
110
|
+
- lib/extra_attribute.rb
|
111
|
+
- lib/version.rb
|
112
|
+
- spec/db/schema.rb
|
113
|
+
- spec/lib/extra_attribute_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
- spec/support/child.rb
|
116
|
+
- spec/support/person.rb
|
117
|
+
homepage: http://github.com/manic/extra_attributes
|
118
|
+
licenses: []
|
119
|
+
post_install_message:
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ! '>='
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.8.24
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: A simple alternative to acts_as_eav_model.
|
141
|
+
test_files:
|
142
|
+
- spec/db/schema.rb
|
143
|
+
- spec/lib/extra_attribute_spec.rb
|
144
|
+
- spec/spec_helper.rb
|
145
|
+
- spec/support/child.rb
|
146
|
+
- spec/support/person.rb
|