coaster 1.4.11 → 1.4.12

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 672b2d8abf215219eb2fd1b925d3ed0ab658cef0ab54f449daeb8d99260d4fcb
4
- data.tar.gz: 6b9fc26bcaaad1a9df56e8ee15e917c960ce463d642c5e0047420738cbffade4
3
+ metadata.gz: 3fc17d468961bab6b4f93a732e8731d0b25eb3b25205f15e6ddbe464978fe744
4
+ data.tar.gz: 87bbf294791a39fde0d45ee3872f4566ca42bfa8c05af3860c9d9f136848d944
5
5
  SHA512:
6
- metadata.gz: b2f83e5b8274e241f85102da046bb559aa6888e4f87eec0a0153322c85ef48977c734e5deccf8ff013d7cdd718c49ce65198aa0f7e9ef3e24962daa821402b32
7
- data.tar.gz: f473793222297836169f893954e09ab93b80b0e367d86cb62a48a48fe7e76728b4950076a992d4eb5eaa3cf4a21e64ffb2ffe8be4648bce5d8c7c35ffdf07584
6
+ metadata.gz: '049c160b3d86b2ca1072d7b6b6fbefc44b03209c5a68411126cfc72c13b480d3d3b808ac9e02056d4582a6c7d642f50cccadc33f504a900fcfdf33012c67852d'
7
+ data.tar.gz: 4b392aa4f2dc03a25b02a18ed1ed76b7f94d271e2d37198983a3f7dbd08f3e2c8e195eab7b8f33f588af9ac948c2b43668a23cf57da7e625e815c2009b44407e
@@ -1,19 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Coaster
4
- # +ActiveRecord::AttributeMethods::Serialization#serialize+ 적용시 YAML
5
- # 시리얼라이저로 지정한다. 다음과 같은 특징이 있다.
6
- # * 안전한 YAML(+Psych.safe_load+ 가능한 YAML)로 시리얼라이즈한다.
7
- # * +Hash+ 만 지원한다.
8
- # * YAML 데이터가 nil 인 경우 +load+ 하면 빈 해시를 반환한다.
9
- # * +load+ 는 +ActiveSupport::HashWithIndifferentAccess+ 를 반환한다.
10
- # ==== 주의
11
- # * 해시 값에 지정된 객체는 기본 타입(String으로 변환된다
12
- #
13
- # ==== Example
14
- # class User < ApplicationRecord
15
- # serialize :data, SafeYamlSerializer
16
- # end
17
4
  class SafeYamlSerializer
18
5
  class << self
19
6
  def dump(obj)
@@ -25,14 +12,12 @@ module Coaster
25
12
  return {} if yaml.nil?
26
13
  return yaml unless yaml.is_a?(String) && yaml.start_with?("---")
27
14
 
28
- if Rails.env.production?
29
- YAML.load(yaml)
30
- else
31
- begin
32
- YAML.safe_load(yaml, [], [], true) || {}
33
- rescue Psych::DisallowedClass => e
34
- # Rails.logger.warn e.inspect
35
- # Rails.logger.warn e.backtrace.join("\n")
15
+ begin
16
+ YAML.safe_load(yaml, permitted_classes: [Date, Time], aliases: true) || {}
17
+ rescue Psych::DisallowedClass => e
18
+ if YAML.respond_to?(:unsafe_load)
19
+ YAML.unsafe_load(yaml) || {}
20
+ else
36
21
  YAML.load(yaml) || {}
37
22
  end
38
23
  end
@@ -40,7 +25,9 @@ module Coaster
40
25
 
41
26
  # @return [HashWithIndifferentAccess]
42
27
  def load(yaml)
43
- (_load_hash(yaml) || {}).with_indifferent_access
28
+ obj = _load_hash(yaml)
29
+ obj = obj.with_indifferent_access if obj.is_a?(Hash)
30
+ obj
44
31
  end
45
32
  end
46
33
  end
@@ -106,7 +106,7 @@ module Coaster
106
106
  _define_serialized_property(serialize_column, key, default: default || [])
107
107
  elsif type.respond_to?(:serialized_property_serializer) && (serializer = type.serialized_property_serializer)
108
108
  _define_serialized_property(serialize_column, key, getter: serializer[:getter], setter: serializer[:setter], setter_callback: serializer[:setter_callback], default: default)
109
- elsif type.is_a?(Class) && type < ActiveRecord::Base
109
+ elsif (type.is_a?(Symbol) && (t = type.to_s.constantize rescue nil)) || (type.is_a?(Class) && type < ActiveRecord::Base && (t = type))
110
110
  _define_serialized_property serialize_column, "#{key}_id", default: default
111
111
 
112
112
  define_method key.to_sym do
@@ -116,10 +116,10 @@ module Coaster
116
116
  if key_id.nil?
117
117
  instance_val = nil
118
118
  else
119
- instance_val = type.find(key_id) rescue nil
120
- instance_variable_set("@#{key}".to_sym, instance_val)
119
+ instance_val = t.find(key_id) rescue nil
121
120
  end
122
121
  instance_val = getter.call(instance_val) if getter
122
+ instance_variable_set("@#{key}".to_sym, instance_val)
123
123
  instance_val
124
124
  end
125
125
 
@@ -130,8 +130,8 @@ module Coaster
130
130
  instance_variable_set("@#{key}".to_sym, nil)
131
131
  send("#{key}_id=".to_sym, nil)
132
132
  else
133
- unless val.is_a?(type)
134
- raise ActiveRecord::AssociationTypeMismatch, "#{type}(##{type.object_id}) expected, got #{val.class.name}(#{val.class.object_id})"
133
+ unless val.is_a?(t)
134
+ raise ActiveRecord::AssociationTypeMismatch, "#{t}(##{t.object_id}) expected, got #{val.class.name}(#{val.class.object_id})"
135
135
  end
136
136
  instance_variable_set("@#{key}".to_sym, val)
137
137
  send("#{key}_id=".to_sym, (Integer(val.id) rescue val.id))
@@ -1,3 +1,3 @@
1
1
  module Coaster
2
- VERSION = '1.4.11'
2
+ VERSION = '1.4.12'
3
3
  end
@@ -6,6 +6,8 @@ class User < ActiveRecord::Base
6
6
  extend Coaster::SerializedProperties
7
7
  serialized_column :data
8
8
  serialized_property :data, :appendix, default: {}
9
+ serialized_property :data, :father, type: :User
10
+ serialized_property :data, :mother, type: self
9
11
 
10
12
  def init_appendix
11
13
  appendix['test_key1'] ||= 0
@@ -14,6 +14,20 @@ module Coaster
14
14
  user.init_appendix
15
15
  assert_equal 0, user.appendix['test_key1']
16
16
  assert_equal 0, user.appendix['test_key2']
17
+ father = User.create(name: 'father')
18
+ user.father = father
19
+ assert_equal father, user.father
20
+ assert_equal father.id, user.father_id
21
+ mother = User.create(name: 'mother')
22
+ user.mother = mother
23
+ assert_equal mother, user.mother
24
+ assert_equal mother.id, user.mother_id
25
+ assert_equal({"appendix"=>{"test_key1"=>0, "test_key2"=>0}, "father_id"=>father.id, "mother_id"=>mother.id}, user.data)
26
+ user.save!
27
+ user = User.find(user.id)
28
+ assert_equal({"appendix"=>{"test_key1"=>0, "test_key2"=>0}, "father_id"=>father.id, "mother_id"=>mother.id}, user.data)
29
+ assert_equal mother, user.mother
30
+ assert_equal father, user.father
17
31
  end
18
32
  end
19
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: coaster
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.11
4
+ version: 1.4.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - buzz jung
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-18 00:00:00.000000000 Z
11
+ date: 2024-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj