coaster 1.3.18 → 1.3.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/coaster/safe_yaml_serializer.rb +47 -0
- data/lib/coaster/serialized_properties.rb +6 -1
- data/lib/coaster/util.rb +8 -1
- data/lib/coaster/version.rb +1 -1
- data/test/support/models.rb +14 -0
- data/test/support/schema.rb +11 -0
- data/test/test_helper.rb +6 -0
- data/test/test_serialized_property.rb +19 -0
- data/test/test_util.rb +1 -0
- metadata +37 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a7dffaea9663d2126ec52401350b70af96285a7476e2540ab9a6ba5b55ae6f8
|
4
|
+
data.tar.gz: c2bb68ce9c7e15c385229579a6ca77e74e75c13b39be31d65affd17f2f690495
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e28d5fe2efe1af4387b8134f594ee9ab136434bf09c08079e004525ee075c0d3605ca29333c669ebe60363ca0040441badeffc2f41495c06fe45a9ce5ddb00a
|
7
|
+
data.tar.gz: e6b2fe88e60f8f7e2eba5d3cdeedff765919a120d9f3aff01a4a244ac6b2bee32e44a64ca48535bb684491e1e5073af0de124d923a7123839813ae710d837944
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
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
|
+
class SafeYamlSerializer
|
18
|
+
class << self
|
19
|
+
def dump(obj)
|
20
|
+
return if obj.nil?
|
21
|
+
YAML.dump(JSON.load(obj.to_json))
|
22
|
+
end
|
23
|
+
|
24
|
+
def _load_hash(yaml)
|
25
|
+
return {} if yaml.nil?
|
26
|
+
return yaml unless yaml.is_a?(String) && yaml.start_with?("---")
|
27
|
+
|
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")
|
36
|
+
YAML.load(yaml) || {}
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# @return [HashWithIndifferentAccess]
|
42
|
+
def load(yaml)
|
43
|
+
(_load_hash(yaml) || {}).with_indifferent_access
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -78,7 +78,11 @@ module Coaster
|
|
78
78
|
if type == Time
|
79
79
|
_define_serialized_property serialize_column, key,
|
80
80
|
getter: Proc.new { |val| val.nil? ? nil : Time.zone.parse(val) },
|
81
|
-
setter: Proc.new { |val|
|
81
|
+
setter: Proc.new { |val|
|
82
|
+
if val.is_a?(Time)
|
83
|
+
val.respond_to?(:to_fs) ? val.to_fs(:default_tz) : val.to_s(:default_tz)
|
84
|
+
end
|
85
|
+
}
|
82
86
|
elsif type == Integer
|
83
87
|
_define_serialized_property serialize_column, key,
|
84
88
|
setter: proc { |val|
|
@@ -143,6 +147,7 @@ module Coaster
|
|
143
147
|
define_method key.to_sym do
|
144
148
|
hsh = send(serialize_column.to_sym)
|
145
149
|
hsh[key.to_s] ||= default.dup
|
150
|
+
hsh[key.to_s]
|
146
151
|
end
|
147
152
|
end
|
148
153
|
else
|
data/lib/coaster/util.rb
CHANGED
@@ -4,6 +4,9 @@ module Coaster
|
|
4
4
|
ARRAY_START = 0
|
5
5
|
|
6
6
|
class << self
|
7
|
+
# array_start가 0이면 array를 {'0'=>'blah', '1'=>'vlah'}
|
8
|
+
# array_start가 1이면 array를 {'1'=>'blah', '2'=>'vlah'}
|
9
|
+
# array_start가 nil이면 그대로 ['blah', 'vlah'] 로 처리
|
7
10
|
def flatten_hashify(object, delimiter: FLATTEN_HASH_DELIMITER, breadcrumbs: [], array_start: nil)
|
8
11
|
case object
|
9
12
|
when Hash
|
@@ -23,7 +26,11 @@ module Coaster
|
|
23
26
|
end
|
24
27
|
end
|
25
28
|
|
26
|
-
|
29
|
+
# 숫자로 된 키는 무조건 array로 처리함
|
30
|
+
# array_start는 nil일 경우 기본값은 Util::ARRAY_START = 0
|
31
|
+
def roughen_hashify(object, delimiter: FLATTEN_HASH_DELIMITER, array_start: nil)
|
32
|
+
array_start ||= ARRAY_START
|
33
|
+
|
27
34
|
step1 = object.each_with_object({}) do |(key, value), memo|
|
28
35
|
sp_keys = key.split(delimiter)
|
29
36
|
k = sp_keys.shift
|
data/lib/coaster/version.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'coaster/serialized_properties'
|
2
|
+
require 'coaster/safe_yaml_serializer'
|
3
|
+
|
4
|
+
class User < ActiveRecord::Base
|
5
|
+
serialize :data, Coaster::SafeYamlSerializer
|
6
|
+
extend Coaster::SerializedProperties
|
7
|
+
serialized_column :data
|
8
|
+
serialized_property :data, :appendix, default: {}
|
9
|
+
|
10
|
+
def init_appendix
|
11
|
+
appendix['test_key1'] ||= 0
|
12
|
+
appendix['test_key2'] ||= 0
|
13
|
+
end
|
14
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -8,6 +8,12 @@ require 'bundler/setup'
|
|
8
8
|
require 'coaster'
|
9
9
|
require 'logger'
|
10
10
|
|
11
|
+
require 'active_record'
|
12
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ":memory:"
|
13
|
+
load File.expand_path('../support/schema.rb', __FILE__)
|
14
|
+
load File.expand_path('../support/models.rb', __FILE__)
|
15
|
+
require 'rails'
|
16
|
+
|
11
17
|
class Raven
|
12
18
|
def self.capture_exception(*args)
|
13
19
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
module Coaster
|
5
|
+
class TestSerializedProperty < Minitest::Test
|
6
|
+
def setup
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_serialized
|
13
|
+
user = User.create(name: 'abc')
|
14
|
+
user.init_appendix
|
15
|
+
assert_equal 0, user.appendix['test_key1']
|
16
|
+
assert_equal 0, user.appendix['test_key2']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/test/test_util.rb
CHANGED
@@ -24,6 +24,7 @@ module Coaster
|
|
24
24
|
assert_equal({"a.0"=>1, "b.aa"=>1, "b.bb.0"=>2, "b.bb.1"=>1}, res)
|
25
25
|
b = Util.roughen_hashify(res)
|
26
26
|
assert_equal(a, b)
|
27
|
+
|
27
28
|
res = Util.flatten_hashify([{a: 1}, 22, [33, 44], {b: {c: [33, {d: 4}]}}])
|
28
29
|
assert_equal({""=>[{:a=>1}, 22, [33, 44], {:b=>{:c=>[33, {:d=>4}]}}]}, res) # no meaning result
|
29
30
|
res = Util.flatten_hashify([{a: 1}, 22, [33, 44], {b: {c: [33, {d: 4}]}}], array_start: 1)
|
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.3.
|
4
|
+
version: 1.3.20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- buzz jung
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -136,6 +136,34 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rails
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: sqlite3
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
139
167
|
description: Ruby Core Extensions
|
140
168
|
email: buzz@frograms.com
|
141
169
|
executables: []
|
@@ -157,14 +185,18 @@ files:
|
|
157
185
|
- lib/coaster/core_ext/standard_error/sentry.rb
|
158
186
|
- lib/coaster/rails_ext.rb
|
159
187
|
- lib/coaster/rails_ext/backtrace_cleaner.rb
|
188
|
+
- lib/coaster/safe_yaml_serializer.rb
|
160
189
|
- lib/coaster/serialized_properties.rb
|
161
190
|
- lib/coaster/util.rb
|
162
191
|
- lib/coaster/version.rb
|
163
192
|
- test/locales/en.yml
|
193
|
+
- test/support/models.rb
|
194
|
+
- test/support/schema.rb
|
164
195
|
- test/test_backtrace.rb
|
165
196
|
- test/test_helper.rb
|
166
197
|
- test/test_month.rb
|
167
198
|
- test/test_object_translation.rb
|
199
|
+
- test/test_serialized_property.rb
|
168
200
|
- test/test_standard_error.rb
|
169
201
|
- test/test_util.rb
|
170
202
|
homepage: http://github.com/frograms/coaster
|
@@ -192,9 +224,12 @@ specification_version: 4
|
|
192
224
|
summary: A little convenient feature for standard library
|
193
225
|
test_files:
|
194
226
|
- test/locales/en.yml
|
227
|
+
- test/support/models.rb
|
228
|
+
- test/support/schema.rb
|
195
229
|
- test/test_backtrace.rb
|
196
230
|
- test/test_helper.rb
|
197
231
|
- test/test_month.rb
|
198
232
|
- test/test_object_translation.rb
|
233
|
+
- test/test_serialized_property.rb
|
199
234
|
- test/test_standard_error.rb
|
200
235
|
- test/test_util.rb
|