active_type 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7180486f774d7be6e2e72c7a143ce953fa4dfb53
4
- data.tar.gz: a3e11be0ca7a917b94ee3d9dfe5de50cab2d2ef9
3
+ metadata.gz: 614f88d296a3773a1c03c6a3dad9473e345dcfd5
4
+ data.tar.gz: cccb5d3fb673acdde82a14c9b7156dfd5623587a
5
5
  SHA512:
6
- metadata.gz: 7f3a7e25ee5308b7391998653c11742ace74366ad096381306fb13692c2e58093a598c7a2e82963009e3f2daac59b161365082e1e032056cc475f8fe18340079
7
- data.tar.gz: 626eda35b1cf0536d46956cda85843d88e92a056b2a62ad1194310347f7c91d4614c02ffe473d9df71294246e86287bd35ffb8b913051413bac12879a89520f0
6
+ metadata.gz: 055145ac7031800c9a64ccb5b60dd16712eb02d2e0fac0fb5f4c5fbb3131188329b280482e76849d77abb6a78fd8a5dfa5e0bf0328908e125321e779dec4a68f
7
+ data.tar.gz: 0232544fbd3b0d789bfd272ebc28faf08b2327c1f452dca80aab8ba4fe27629d796efc4e3de0393411964d4b87b1aeb44b860e30f939018348f19d6390b63628
data/README.md CHANGED
@@ -274,7 +274,7 @@ Supported options for `nests_many` / `nests_one` are:
274
274
  Supported Rails versions
275
275
  ------------------------
276
276
 
277
- ActiveType is tested against ActiveRecord 3.2, 4.0 and 4.1.
277
+ ActiveType is tested against ActiveRecord 3.2, 4.0, 4.1 and 4.2beta.
278
278
 
279
279
  Later versions might work, earlier will not.
280
280
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- active_type (0.2.1)
4
+ active_type (0.3.0)
5
5
  activerecord (>= 3.2)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- active_type (0.2.1)
4
+ active_type (0.3.0)
5
5
  activerecord (>= 3.2)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- active_type (0.2.1)
4
+ active_type (0.3.0)
5
5
  activerecord (>= 3.2)
6
6
 
7
7
  GEM
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~>4.2.0.beta1'
4
+ gem 'rspec', '<2.99'
5
+ gem 'sqlite3'
6
+
7
+ gem 'active_type', :path => '..'
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ active_type (0.3.0)
5
+ activerecord (>= 3.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.0.beta1)
11
+ activesupport (= 4.2.0.beta1)
12
+ builder (~> 3.1)
13
+ activerecord (4.2.0.beta1)
14
+ activemodel (= 4.2.0.beta1)
15
+ activesupport (= 4.2.0.beta1)
16
+ arel (>= 6.0.0.beta1, < 6.1)
17
+ activesupport (4.2.0.beta1)
18
+ i18n (>= 0.7.0.beta1, < 0.8)
19
+ json (~> 1.7, >= 1.7.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.1)
22
+ tzinfo (~> 1.1)
23
+ arel (6.0.0.beta1)
24
+ builder (3.2.2)
25
+ diff-lcs (1.2.5)
26
+ i18n (0.7.0.beta1)
27
+ json (1.8.1)
28
+ minitest (5.4.1)
29
+ rspec (2.14.1)
30
+ rspec-core (~> 2.14.0)
31
+ rspec-expectations (~> 2.14.0)
32
+ rspec-mocks (~> 2.14.0)
33
+ rspec-core (2.14.8)
34
+ rspec-expectations (2.14.5)
35
+ diff-lcs (>= 1.1.3, < 2.0)
36
+ rspec-mocks (2.14.6)
37
+ sqlite3 (1.3.9)
38
+ thread_safe (0.3.4)
39
+ tzinfo (1.2.2)
40
+ thread_safe (~> 0.1)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ active_type!
47
+ activerecord (~> 4.2.0.beta1)
48
+ rspec (< 2.99)
49
+ sqlite3
@@ -106,7 +106,8 @@ module ActiveType
106
106
  end
107
107
 
108
108
  def truthy?(value)
109
- ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
109
+ @boolean_type_caster ||= TypeCaster.get(:boolean, @owner.connection)
110
+ @boolean_type_caster.type_cast_from_user(value)
110
111
  end
111
112
 
112
113
  def reject?(parent, attributes)
@@ -0,0 +1,81 @@
1
+ module ActiveType
2
+ class TypeCaster
3
+
4
+ def self.get(type, connection)
5
+ native_caster = if ActiveRecord::VERSION::STRING < '4.2'
6
+ NativeCasters::DelegateToColumn.new(type)
7
+ else
8
+ NativeCasters::DelegateToType.new(type, connection)
9
+ end
10
+ new(type, native_caster)
11
+ end
12
+
13
+ def initialize(type, native_caster)
14
+ @type = type
15
+ @native_caster = native_caster
16
+ end
17
+
18
+ def type_cast_from_user(value)
19
+ # For some reason, Rails defines additional type casting logic
20
+ # outside the classes that have that responsibility.
21
+ case @type
22
+ when :integer
23
+ if value == ''
24
+ nil
25
+ else
26
+ native_type_cast_from_user(value)
27
+ end
28
+ when :timestamp, :datetime
29
+ time = native_type_cast_from_user(value)
30
+ if time && ActiveRecord::Base.time_zone_aware_attributes
31
+ time = ActiveSupport::TimeWithZone.new(nil, Time.zone, time)
32
+ end
33
+ time
34
+ else
35
+ native_type_cast_from_user(value)
36
+ end
37
+ end
38
+
39
+ def native_type_cast_from_user(value)
40
+ @native_caster.type_cast_from_user(value)
41
+ end
42
+
43
+ module NativeCasters
44
+
45
+ # Adapter for Rails 3.0 - 4.1.
46
+ # In these versions, casting logic lives in ActiveRecord::ConnectionAdapters::Colum
47
+ class DelegateToColumn
48
+
49
+ def initialize(type)
50
+ # the Column initializer expects type as returned from the database, and
51
+ # resolves them to our types
52
+ # fortunately, for all types wie support, type.to_s is a valid sql_type
53
+ sql_type = type.to_s
54
+ @column = ActiveRecord::ConnectionAdapters::Column.new('foo', nil, sql_type)
55
+ end
56
+
57
+ def type_cast_from_user(value)
58
+ @column.type_cast(value)
59
+ end
60
+
61
+ end
62
+
63
+ # Adapter for Rails 4.2+.
64
+ # In these versions, casting logic lives in subclasses of ActiveRecord::Type::Value
65
+ class DelegateToType
66
+
67
+ def initialize(type, connection)
68
+ @active_record_type = connection.lookup_cast_type(type)
69
+ end
70
+
71
+ def type_cast_from_user(value)
72
+ @active_record_type.type_cast_from_user(value)
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveType
2
- VERSION = '0.2.1'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,3 +1,5 @@
1
+ require 'active_type/type_caster'
2
+
1
3
  module ActiveType
2
4
 
3
5
  class InvalidAttributeNameError < ::StandardError; end
@@ -6,43 +8,16 @@ module ActiveType
6
8
 
7
9
  module VirtualAttributes
8
10
 
9
- class VirtualColumn < ActiveRecord::ConnectionAdapters::Column
11
+ class VirtualColumn
10
12
 
11
- def initialize(name, type, options)
13
+ def initialize(name, type_caster, options)
12
14
  @name = name
13
- @type = type
15
+ @type_caster = type_caster
14
16
  @options = options
15
17
  end
16
18
 
17
19
  def type_cast(value)
18
- case @type
19
- when :integer
20
- case value
21
- when FalseClass
22
- 0
23
- when TrueClass
24
- 1
25
- when "", nil
26
- nil
27
- else
28
- value.to_i
29
- end
30
- when :timestamp, :datetime
31
- if ActiveRecord::Base.time_zone_aware_attributes
32
- time = super
33
- if time
34
- ActiveSupport::TimeWithZone.new(nil, Time.zone, time)
35
- else
36
- time
37
- end
38
- else
39
- super
40
- end
41
- when nil
42
- value
43
- else
44
- super
45
- end
20
+ @type_caster.type_cast_from_user(value)
46
21
  end
47
22
 
48
23
  def default_value(object)
@@ -70,7 +45,9 @@ module ActiveType
70
45
  private
71
46
 
72
47
  def add_virtual_column(name, type, options)
73
- @owner.virtual_columns_hash = @owner.virtual_columns_hash.merge(name.to_s => VirtualColumn.new(name, type, options.slice(:default)))
48
+ type_caster = TypeCaster.get(type, @owner.connection)
49
+ column = VirtualColumn.new(name, type_caster, options.slice(:default))
50
+ @owner.virtual_columns_hash = @owner.virtual_columns_hash.merge(name.to_s => column)
74
51
  end
75
52
 
76
53
  def build_reader(name)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Kraze
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-06 00:00:00.000000000 Z
12
+ date: 2014-09-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -72,6 +72,8 @@ files:
72
72
  - gemfiles/Gemfile.4.0.lock
73
73
  - gemfiles/Gemfile.4.1
74
74
  - gemfiles/Gemfile.4.1.lock
75
+ - gemfiles/Gemfile.4.2
76
+ - gemfiles/Gemfile.4.2.lock
75
77
  - lib/active_type.rb
76
78
  - lib/active_type/extended_record.rb
77
79
  - lib/active_type/extended_record/inheritance.rb
@@ -83,6 +85,7 @@ files:
83
85
  - lib/active_type/no_table.rb
84
86
  - lib/active_type/object.rb
85
87
  - lib/active_type/record.rb
88
+ - lib/active_type/type_caster.rb
86
89
  - lib/active_type/version.rb
87
90
  - lib/active_type/virtual_attributes.rb
88
91
  - spec/active_type/extended_record/single_table_inheritance_spec.rb