serialized_attributes 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -28,9 +28,9 @@ Jeweler::Tasks.new do |gem|
28
28
  gem.files = [
29
29
  "Gemfile",
30
30
  "Gemfile.lock",
31
- "init.rb",
32
- "lib/has_references_to.rb",
33
31
  "lib/serialized_attributes.rb",
32
+ "lib/serialized_attributes/has_references_to.rb",
33
+ "lib/serialized_attributes/serialized_attributes.rb",
34
34
  "LICENSE.txt",
35
35
  "Rakefile",
36
36
  "README.md",
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -1,89 +1,2 @@
1
- module SerializedAttributes
2
- def self.included(base)
3
- return if base.respond_to?(:serialized_attributes_definition)
4
-
5
- base.class_eval do
6
- class_inheritable_hash :serialized_attributes_definition
7
- write_inheritable_attribute(:serialized_attributes_definition, {})
8
- cattr_accessor :serialized_attributes_column
9
- self.serialized_attributes_column = :serialized_attributes
10
-
11
- serialize serialized_attributes_column, Hash
12
-
13
-
14
- base.extend ClassMethods
15
- include InstanceMethods
16
- end
17
- end
18
-
19
- module ClassMethods
20
- def serialized_attributes_definition
21
- read_inheritable_attribute(:serialized_attributes_definition)
22
- end
23
-
24
- def instantiate(record)
25
- object = super(record)
26
- object.unpack_serialized_attributes!
27
- object
28
- end
29
-
30
- def accessible_attribute(name, type, opts = {})
31
- attribute(name, type, opts.merge({:attr_accessible => true}))
32
- end
33
-
34
- def attribute(name, type, opts = {})
35
- name = name.to_s
36
- type = SerializedAttributes.type_to_sqltype(type)
37
- serialized_attributes_definition[name] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, opts[:default], type.to_s, nil)
38
-
39
- define_method("#{name.to_s}=".to_sym) { |value| @attributes[name] = value }
40
- define_method(name) { self.class.serialized_attributes_definition[name].type_cast(@attributes[name]) }
41
-
42
- attr_accessible name if opts[:attr_accessible]
43
- end
44
- end
45
-
46
- module InstanceMethods
47
- def create_or_update
48
- pack_serialized_attributes!
49
- super
50
- end
51
-
52
- def unpack_serialized_attributes!
53
- if @attributes.has_key?(serialized_attributes_column.to_s) && attributes = (self[serialized_attributes_column] || {})
54
- serialized_attributes_definition.each do |key, column|
55
- loaded_value = attributes.has_key?(key) ? attributes[key] : column.default
56
- @attributes[key] = attributes.has_key?(key) ? attributes[key] : column.default
57
- end
58
- attributes.slice!(*serialized_attributes_definition.keys)
59
- end
60
- end
61
-
62
- def pack_serialized_attributes!
63
- if @attributes.has_key?(serialized_attributes_column.to_s)
64
- attributes = self[serialized_attributes_column] ||= {}
65
- serialized_attributes_definition.each do |key, column|
66
- attributes[key] = self.send key
67
- end
68
- end
69
- attributes.slice!(*serialized_attributes_definition.keys)
70
- end
71
- end
72
-
73
- def to_variable(sym)
74
- "@#{sym.to_s}".to_sym
75
- end
76
-
77
- def self.type_to_sqltype(type)
78
- return type if type.is_a?(Symbol)
79
- {
80
- String => :string, Boolean => :boolean,
81
- Fixnum => :integer, Integer => :integer, BigDecimal => :decimal, Float => :float,
82
- Date => :date, Time => :time, DateTime => :time
83
- }[type] || type
84
- end
85
-
86
- module Boolean
87
- end
88
-
89
- end
1
+ require File.dirname(__FILE__) + '/serialized_attributes/has_references_to'
2
+ require File.dirname(__FILE__) + '/serialized_attributes/serialized_attributes'
@@ -0,0 +1,89 @@
1
+ module SerializedAttributes
2
+ def self.included(base)
3
+ return if base.respond_to?(:serialized_attributes_definition)
4
+
5
+ base.class_eval do
6
+ class_inheritable_hash :serialized_attributes_definition
7
+ write_inheritable_attribute(:serialized_attributes_definition, {})
8
+ cattr_accessor :serialized_attributes_column
9
+ self.serialized_attributes_column = :serialized_attributes
10
+
11
+ serialize serialized_attributes_column, Hash
12
+
13
+
14
+ base.extend ClassMethods
15
+ include InstanceMethods
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+ def serialized_attributes_definition
21
+ read_inheritable_attribute(:serialized_attributes_definition)
22
+ end
23
+
24
+ def instantiate(record)
25
+ object = super(record)
26
+ object.unpack_serialized_attributes!
27
+ object
28
+ end
29
+
30
+ def accessible_attribute(name, type, opts = {})
31
+ attribute(name, type, opts.merge({:attr_accessible => true}))
32
+ end
33
+
34
+ def attribute(name, type, opts = {})
35
+ name = name.to_s
36
+ type = SerializedAttributes.type_to_sqltype(type)
37
+ serialized_attributes_definition[name] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, opts[:default], type.to_s, nil)
38
+
39
+ define_method("#{name.to_s}=".to_sym) { |value| @attributes[name] = value }
40
+ define_method(name) { self.class.serialized_attributes_definition[name].type_cast(@attributes[name]) }
41
+
42
+ attr_accessible name if opts[:attr_accessible]
43
+ end
44
+ end
45
+
46
+ module InstanceMethods
47
+ def create_or_update
48
+ pack_serialized_attributes!
49
+ super
50
+ end
51
+
52
+ def unpack_serialized_attributes!
53
+ if @attributes.has_key?(serialized_attributes_column.to_s) && attributes = (self[serialized_attributes_column] || {})
54
+ serialized_attributes_definition.each do |key, column|
55
+ loaded_value = attributes.has_key?(key) ? attributes[key] : column.default
56
+ @attributes[key] = attributes.has_key?(key) ? attributes[key] : column.default
57
+ end
58
+ attributes.slice!(*serialized_attributes_definition.keys)
59
+ end
60
+ end
61
+
62
+ def pack_serialized_attributes!
63
+ if @attributes.has_key?(serialized_attributes_column.to_s)
64
+ attributes = self[serialized_attributes_column] ||= {}
65
+ serialized_attributes_definition.each do |key, column|
66
+ attributes[key] = self.send key
67
+ end
68
+ end
69
+ attributes.slice!(*serialized_attributes_definition.keys)
70
+ end
71
+ end
72
+
73
+ def to_variable(sym)
74
+ "@#{sym.to_s}".to_sym
75
+ end
76
+
77
+ def self.type_to_sqltype(type)
78
+ return type if type.is_a?(Symbol)
79
+ {
80
+ String => :string, Boolean => :boolean,
81
+ Fixnum => :integer, Integer => :integer, BigDecimal => :decimal, Float => :float,
82
+ Date => :date, Time => :time, DateTime => :time
83
+ }[type] || type
84
+ end
85
+
86
+ module Boolean
87
+ end
88
+
89
+ end
data/test/simple_test.rb CHANGED
@@ -5,7 +5,7 @@ require 'logger'
5
5
 
6
6
  ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
7
7
 
8
- require File.dirname(__FILE__) + "/../init" # load plugin
8
+ require File.dirname(__FILE__) + "/../lib/serialized_attributes" # load plugin
9
9
 
10
10
  class DocumentsSchema < ActiveRecord::Migration
11
11
  def self.up
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialized_attributes
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Emma Persky
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-03 00:00:00 -05:00
18
+ date: 2011-03-04 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -154,9 +154,9 @@ files:
154
154
  - README.md
155
155
  - Rakefile
156
156
  - VERSION
157
- - init.rb
158
- - lib/has_references_to.rb
159
157
  - lib/serialized_attributes.rb
158
+ - lib/serialized_attributes/has_references_to.rb
159
+ - lib/serialized_attributes/serialized_attributes.rb
160
160
  - test/simple_test.rb
161
161
  has_rdoc: true
162
162
  homepage: http://github.com/emmapersky/serialized_attributes
data/init.rb DELETED
@@ -1,2 +0,0 @@
1
- require File.dirname(__FILE__) + '/lib/has_references_to'
2
- require File.dirname(__FILE__) + '/lib/serialized_attributes'