serializable_attributes 0.9.0

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.
@@ -0,0 +1,107 @@
1
+ require 'rubygems'
2
+
3
+ if ENV['VERSION']
4
+ gem 'activerecord', ENV['VERSION']
5
+ end
6
+
7
+ require 'test/unit'
8
+ require 'active_record'
9
+ require 'active_support/test_case'
10
+
11
+ require File.dirname(__FILE__) + '/../init'
12
+
13
+ begin
14
+ require 'ruby-debug'
15
+ Debugger.start
16
+ rescue LoadError
17
+ end
18
+
19
+ module SerializableMethods
20
+ def table_exists?
21
+ false
22
+ end
23
+
24
+ def columns
25
+ []
26
+ end
27
+
28
+ def column_defaults
29
+ {}
30
+ end
31
+
32
+ def columns_hash
33
+ {}
34
+ end
35
+
36
+ def primary_key
37
+ "id"
38
+ end
39
+
40
+ def transaction
41
+ yield
42
+ rescue ActiveRecord::Rollback
43
+ end
44
+ end
45
+
46
+ class SerializedRecord < ActiveRecord::Base
47
+ extend SerializableMethods
48
+
49
+ class << self
50
+ attr_accessor :stubbed_raw_data
51
+ end
52
+
53
+ def self.find(n, options)
54
+ if n != 481516 && options != 2342
55
+ raise ArgumentError, "This is supposed to be a test!"
56
+ end
57
+ r = new
58
+ r.id = 481516
59
+ r.raw_data = @stubbed_raw_data
60
+ r
61
+ end
62
+
63
+ attr_accessor :raw_data
64
+
65
+ serialize_attributes :data do
66
+ string :title, :body
67
+ integer :age
68
+ float :average
69
+ time :birthday
70
+ boolean :active
71
+ boolean :default_in_my_favor, :default => true
72
+ array :names
73
+ array :lottery_picks, :type => :integer
74
+ hash :extras, :types => {
75
+ :num => :integer,
76
+ :started_at => :time
77
+ }
78
+ end
79
+
80
+ before_save { |r| false } # cancel the save
81
+
82
+ def add_to_transaction
83
+ end
84
+ end
85
+
86
+ class SerializedRecordWithDefaults < ActiveRecord::Base
87
+ extend SerializableMethods
88
+
89
+ attr_accessor :raw_data
90
+
91
+ serialize_attributes :data do
92
+ string :title, :body, :default => 'blank'
93
+ integer :age, :default => 18
94
+ float :average, :default => 5.2
95
+ time :birthday, :default => Time.utc(2009, 1, 1)
96
+ boolean :active, :default => true
97
+ array :names, :default => %w(a b c)
98
+ hash :extras, :default => {:a => 1}
99
+ boolean :clearance, :default => nil
100
+ string :symbol, :default => :foo
101
+ end
102
+
103
+ before_save { |r| false } # cancel the save
104
+
105
+ def add_to_transaction
106
+ end
107
+ end
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path("../test_helper", __FILE__)
3
+
4
+ class SerializedAttributesTypesTest < ActiveSupport::TestCase
5
+
6
+ test "boolean type encodes nil properly" do
7
+ type = SerializedAttributes::Boolean.new
8
+
9
+ assert_equal nil, type.encode(nil)
10
+ end
11
+
12
+ test "boolean type encodes blank string properly" do
13
+ type = SerializedAttributes::Boolean.new
14
+
15
+ assert_equal nil, type.encode("")
16
+ end
17
+
18
+ test "boolean type handles strings that look like booleans" do
19
+ type = SerializedAttributes::Boolean.new
20
+
21
+ assert_equal 0, type.encode("false")
22
+ assert_equal 1, type.encode("true")
23
+ end
24
+
25
+ test "boolean type encodes booleans properly" do
26
+ type = SerializedAttributes::Boolean.new
27
+
28
+ assert_equal 0, type.encode(false)
29
+ assert_equal 1, type.encode(true)
30
+ end
31
+
32
+ test "boolean type parses properly" do
33
+ type = SerializedAttributes::Boolean.new
34
+
35
+ assert_equal false, type.parse(0)
36
+ assert_equal true, type.parse(1)
37
+ assert_equal false, type.parse("0")
38
+ assert_equal true, type.parse("1")
39
+ assert_equal nil, type.parse("")
40
+ end
41
+
42
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serializable_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rick Olson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70317221367420 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.2.0
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: 3.2.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: *70317221367420
28
+ description: A bridge between using AR and a full blown schema-free db.
29
+ email: technoweenie@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - gemfiles/ar-2.2.gemfile
38
+ - gemfiles/ar-2.3.gemfile
39
+ - gemfiles/ar-3.0.gemfile
40
+ - gemfiles/ar-3.1.gemfile
41
+ - init.rb
42
+ - lib/serializable_attributes.rb
43
+ - lib/serializable_attributes/duplicable.rb
44
+ - lib/serializable_attributes/format/active_support_json.rb
45
+ - lib/serializable_attributes/schema.rb
46
+ - lib/serializable_attributes/types.rb
47
+ - rails_init.rb
48
+ - script/setup
49
+ - serializable_attributes.gemspec
50
+ - test/serialized_attributes_test.rb
51
+ - test/test_helper.rb
52
+ - test/types_test.rb
53
+ homepage: http://github.com/technoweenie/serialized_attributes
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: 1.3.5
71
+ requirements: []
72
+ rubyforge_project: serializable_attributes
73
+ rubygems_version: 1.8.11
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: Store a serialized hash of attributes in a single ActiveRecord column.
77
+ test_files:
78
+ - test/serialized_attributes_test.rb
79
+ - test/test_helper.rb
80
+ - test/types_test.rb