attr_json 0.4.0 → 0.5.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
  SHA256:
3
- metadata.gz: 05247aceb96f0df4ce9f36a4f6c56099700a33aeddcc0d2a9596330f0ecb23c9
4
- data.tar.gz: ed213264805ed42866d00c6e03e5504a63c96763ac1f83a3518302dbb5c51954
3
+ metadata.gz: 80130e194fdf715bf967ea25fc6fff1d7e043f53479c1e539d9b9e2122233404
4
+ data.tar.gz: df2d7d442df1dfec49becd935321a0fbdc8aebec5892e34d84d415c4f5a87471
5
5
  SHA512:
6
- metadata.gz: 193073a8b1ac277d2f062bb8fae958ec2be300e53216e29fd7af5cf8e70db96e39da92c23ee2f452907b31e55fca0966dff7dd6e45c270028dd2c400a4f53477
7
- data.tar.gz: d92e6761a2dfab4db80931a5af85bbad112631b12f0c62536fcd5ff532df41ffb475a0bea3cd10ac7d013b7ab4d779a19e2af980ab00fa320168715840e32fcc
6
+ metadata.gz: 1e2712bca13ff4fa2b874b5f141070cd5f8e5ab306c6ff9f5e41a7129c4a7b740f6ce0a79a7f9cdf759195cb1c7d144bd0d8ea7ef6d46682487f8d75a47a1c25
7
+ data.tar.gz: 1c86d1e8cea7a9413960fa357507fd5ba25e55c6f27820fb091bd8f4921c3aa7aa3274c90bd30f93b8cdde521d177d6a3cebe9154757038f2b30ba5cebbf970e
@@ -12,7 +12,9 @@
12
12
  attr_reader :name, :type, :original_args, :container_attribute
13
13
 
14
14
  # @param name [Symbol,String]
15
- # @param type [Symbol,ActiveModel::Type::Value]
15
+ # @param type [Symbol,ActiveModel::Type::Value] Symbol is looked up in
16
+ # ActiveRecord::Type.lookup, but with `adapter: nil` for no custom
17
+ # adapter-specific lookup.
16
18
  #
17
19
  # @option options store_key [Symbol,String]
18
20
  # @option options container_attribute [Symbol,ActiveModel::Type::Value]
@@ -40,7 +42,12 @@
40
42
  # ActiveModel::Type.lookup may make more sense, but ActiveModel::Type::Date
41
43
  # seems to have a bug with multi-param assignment. Mostly they return
42
44
  # the same types, but ActiveRecord::Type::Date works with multi-param assignment.
43
- type = ActiveRecord::Type.lookup(type)
45
+ #
46
+ # We pass `adapter: nil` to avoid triggering a db connection.
47
+ # See: https://github.com/jrochkind/attr_json/issues/41
48
+ # This is at the "cost" of not using any adapter-specific types... which
49
+ # maybe preferable anyway?
50
+ type = ActiveRecord::Type.lookup(type, adapter: nil)
44
51
  elsif ! type.is_a? ActiveModel::Type::Value
45
52
  raise ArgumentError, "Second argument (#{type}) must be a symbol or instance of an ActiveModel::Type::Value subclass"
46
53
  end
@@ -18,7 +18,7 @@ module AttrJson
18
18
  extend ActiveSupport::Concern
19
19
 
20
20
  included do
21
- unless self < ActiveRecord::Base
21
+ unless self <= ActiveRecord::Base
22
22
  raise TypeError, "AttrJson::Record can only be used with an ActiveRecord::Base model. #{self} does not appear to be one. Are you looking for ::AttrJson::Model?"
23
23
  end
24
24
 
@@ -8,6 +8,8 @@ module AttrJson
8
8
  # but normally that's only done in AttrJson::Model.to_type, there isn't
9
9
  # an anticipated need to create from any other place.
10
10
  class Model < ::ActiveModel::Type::Value
11
+ class BadCast < ArgumentError ; end
12
+
11
13
  attr_accessor :model
12
14
  def initialize(model)
13
15
  #TODO type check, it really better be a AttrJson::Model. maybe?
@@ -34,10 +36,11 @@ module AttrJson
34
36
  # TODO Maybe we ought not to do this on #to_h?
35
37
  model.new_from_serializable(v.to_h)
36
38
  else
37
- # Bad input? Most existing ActiveModel::Types seem to decide
38
- # either nil, or a base value like the empty string. They don't
39
- # raise. So we won't either, just nil.
40
- nil
39
+ # Bad input. Originally we were trying to return nil, to be like
40
+ # existing ActiveRecord which kind of silently does a basic value
41
+ # with null input. But that ended up making things confusing, let's
42
+ # just raise.
43
+ raise BadCast.new("Can not cast from #{v.inspect} to #{self.type}")
41
44
  end
42
45
  end
43
46
 
@@ -1,3 +1,3 @@
1
1
  module AttrJson
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attr_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Rochkind
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-29 00:00:00.000000000 Z
11
+ date: 2019-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -192,7 +192,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
192
  version: '0'
193
193
  requirements: []
194
194
  rubyforge_project:
195
- rubygems_version: 2.7.7
195
+ rubygems_version: 2.7.6
196
196
  signing_key:
197
197
  specification_version: 4
198
198
  summary: ActiveRecord attributes stored serialized in a json column, super smooth.