stored_attribute 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/Gemfile +6 -0
- data/README.md +29 -0
- data/Rakefile +5 -0
- data/lib/stored_attribute/version.rb +3 -0
- data/lib/stored_attribute.rb +54 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/stored_attribute.sqlite3 +0 -0
- data/spec/stored_attribute_spec.rb +99 -0
- data/spec/support/models.rb +7 -0
- data/spec/support/schema.rb +8 -0
- data/stored_attribute.gemspec +27 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# stored_attribute [![Build Status](https://secure.travis-ci.org/Arjeno/stored_attribute.png?branch=master)](http://travis-ci.org/Arjeno/stored_attribute)
|
2
|
+
|
3
|
+
A simple extension to ActiveRecord for type casting serialized stored attributes using the `store` method.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Add to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'stored_attribute'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Example code
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class Post < ActiveRecord::Base
|
17
|
+
store :data, :accessors => [:show_date, :show_author, :background_color]
|
18
|
+
|
19
|
+
attribute :show_date, :type => :boolean, :default => false
|
20
|
+
attribute :show_author, :type => :boolean, :default => true
|
21
|
+
attribute :background_color, :type => :string, :default => '#FFFFFF'
|
22
|
+
end
|
23
|
+
|
24
|
+
post = Post.create
|
25
|
+
post.show_date # => false
|
26
|
+
|
27
|
+
post.update_attributes({ :show_date => true })
|
28
|
+
post.show_date # => true
|
29
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "stored_attribute/version"
|
3
|
+
|
4
|
+
module StoredAttribute
|
5
|
+
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
|
10
|
+
class_attribute :type_casted_attributes
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
|
16
|
+
def attribute(key, options)
|
17
|
+
self.type_casted_attributes ||= {}
|
18
|
+
self.type_casted_attributes[key] = options
|
19
|
+
|
20
|
+
getter = "#{key}"
|
21
|
+
setter = "#{getter}="
|
22
|
+
get_alias = "original_#{getter}"
|
23
|
+
set_alias = "#{get_alias}="
|
24
|
+
|
25
|
+
# Rename old methods to keep them accessible
|
26
|
+
alias_method get_alias, getter
|
27
|
+
alias_method set_alias, setter
|
28
|
+
|
29
|
+
column = ActiveRecord::ConnectionAdapters::Column.new(key, options[:default], options[:type].to_s)
|
30
|
+
|
31
|
+
define_method(getter) do
|
32
|
+
value = send(get_alias)
|
33
|
+
value.nil? ? column.default : column.type_cast(value)
|
34
|
+
end
|
35
|
+
|
36
|
+
define_method(setter) do |value|
|
37
|
+
send(set_alias, column.type_cast(value))
|
38
|
+
end
|
39
|
+
|
40
|
+
# Set default values
|
41
|
+
if ! options[:default].nil?
|
42
|
+
after_initialize "set_default_#{getter}"
|
43
|
+
|
44
|
+
define_method("set_default_#{getter}") do
|
45
|
+
self.send(setter, options[:default]) if self.send(get_alias).nil?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
ActiveRecord::Base.send(:include, StoredAttribute)
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
require 'stored_attribute'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
|
4
|
+
:database => File.dirname(__FILE__) + "/stored_attribute.sqlite3")
|
5
|
+
|
6
|
+
load File.dirname(__FILE__) + '/support/schema.rb'
|
7
|
+
load File.dirname(__FILE__) + '/support/models.rb'
|
Binary file
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StoredAttribute do
|
4
|
+
|
5
|
+
describe :default_values do
|
6
|
+
|
7
|
+
context :new do
|
8
|
+
|
9
|
+
subject { Post.new }
|
10
|
+
|
11
|
+
its(:show_date) { should be_false }
|
12
|
+
its(:show_author) { should be_true }
|
13
|
+
its(:background_color) { should == '#FFFFFF' }
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
context :create do
|
18
|
+
|
19
|
+
subject { Post.create }
|
20
|
+
|
21
|
+
its(:show_date) { should be_false }
|
22
|
+
its(:show_author) { should be_true }
|
23
|
+
its(:background_color) { should == '#FFFFFF' }
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe :changing_values do
|
30
|
+
|
31
|
+
context :new do
|
32
|
+
|
33
|
+
subject { Post.new }
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
subject.show_date = true
|
37
|
+
subject.show_author = false
|
38
|
+
subject.background_color = '#000000'
|
39
|
+
end
|
40
|
+
|
41
|
+
its(:show_date) { should be_true }
|
42
|
+
its(:show_author) { should be_false }
|
43
|
+
its(:background_color) { should == '#000000' }
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
context :create do
|
48
|
+
|
49
|
+
subject { Post.create }
|
50
|
+
|
51
|
+
before(:each) do
|
52
|
+
subject.show_date = true
|
53
|
+
subject.show_author = false
|
54
|
+
subject.background_color = '#000000'
|
55
|
+
end
|
56
|
+
|
57
|
+
its(:show_date) { should be_true }
|
58
|
+
its(:show_author) { should be_false }
|
59
|
+
its(:background_color) { should == '#000000' }
|
60
|
+
|
61
|
+
describe 'on initialize' do
|
62
|
+
|
63
|
+
subject do
|
64
|
+
Post.create({
|
65
|
+
:show_date => true,
|
66
|
+
:show_author => false,
|
67
|
+
:background_color => '#000000'
|
68
|
+
})
|
69
|
+
end
|
70
|
+
|
71
|
+
its(:show_date) { should be_true }
|
72
|
+
its(:show_author) { should be_false }
|
73
|
+
its(:background_color) { should == '#000000' }
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'with update' do
|
78
|
+
|
79
|
+
subject do
|
80
|
+
post = Post.create
|
81
|
+
post.update_attributes({
|
82
|
+
:show_date => true,
|
83
|
+
:show_author => false,
|
84
|
+
:background_color => '#000000'
|
85
|
+
})
|
86
|
+
post
|
87
|
+
end
|
88
|
+
|
89
|
+
its(:show_date) { should be_true }
|
90
|
+
its(:show_author) { should be_false }
|
91
|
+
its(:background_color) { should == '#000000' }
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
class Post < ActiveRecord::Base
|
2
|
+
store :data, :accessors => [:show_date, :show_author, :background_color]
|
3
|
+
|
4
|
+
attribute :show_date, :type => :boolean, :default => false
|
5
|
+
attribute :show_author, :type => :boolean, :default => true
|
6
|
+
attribute :background_color, :type => :string, :default => '#FFFFFF'
|
7
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "stored_attribute/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "stored_attribute"
|
7
|
+
s.version = StoredAttribute::VERSION
|
8
|
+
s.authors = ["Arjen Oosterkamp"]
|
9
|
+
s.email = ["mail@arjen.me"]
|
10
|
+
s.homepage = "https://github.com/Arjeno/stored_attribute"
|
11
|
+
s.summary = %q{ActiveRecord extension for type casting serialized attributes}
|
12
|
+
s.description = %q{ActiveRecord extension for type casting serialized attributes}
|
13
|
+
|
14
|
+
s.rubyforge_project = "stored_attribute"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
22
|
+
s.add_development_dependency "rspec", "~> 2.3"
|
23
|
+
|
24
|
+
s.add_development_dependency "sqlite3"
|
25
|
+
|
26
|
+
s.add_dependency "activerecord", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stored_attribute
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Arjen Oosterkamp
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-04 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &70223862623920 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70223862623920
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70223862623420 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.3'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70223862623420
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sqlite3
|
38
|
+
requirement: &70223862623040 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70223862623040
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: activerecord
|
49
|
+
requirement: &70223862622500 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70223862622500
|
58
|
+
description: ActiveRecord extension for type casting serialized attributes
|
59
|
+
email:
|
60
|
+
- mail@arjen.me
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/stored_attribute.rb
|
70
|
+
- lib/stored_attribute/version.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- spec/stored_attribute.sqlite3
|
73
|
+
- spec/stored_attribute_spec.rb
|
74
|
+
- spec/support/models.rb
|
75
|
+
- spec/support/schema.rb
|
76
|
+
- stored_attribute.gemspec
|
77
|
+
homepage: https://github.com/Arjeno/stored_attribute
|
78
|
+
licenses: []
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project: stored_attribute
|
97
|
+
rubygems_version: 1.8.10
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: ActiveRecord extension for type casting serialized attributes
|
101
|
+
test_files:
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
- spec/stored_attribute.sqlite3
|
104
|
+
- spec/stored_attribute_spec.rb
|
105
|
+
- spec/support/models.rb
|
106
|
+
- spec/support/schema.rb
|