coaster 1.4.10 → 1.4.12

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: 5b0833fa02d32ec450a4fe1733f2e958db39072b94aa9ead5673b3ec378fff8a
4
- data.tar.gz: 28c3acbdbbf7008217aee6f031bce8de94b1a42dee5a61f18c9d1ffdde0bd005
3
+ metadata.gz: 3fc17d468961bab6b4f93a732e8731d0b25eb3b25205f15e6ddbe464978fe744
4
+ data.tar.gz: 87bbf294791a39fde0d45ee3872f4566ca42bfa8c05af3860c9d9f136848d944
5
5
  SHA512:
6
- metadata.gz: f88ca92b03fa3af23ff20689b282f8b1515d992084b63b2b3b53c4d6f0ddcbf8e88f5a8e1ff5fc08f60f50a5dc68cee61275483182ae130ae63a744c2846feeb
7
- data.tar.gz: b1e7bd90031009c7ddfe925f3b160d3a794a649067505e672ac1a80c4182a0c8c5b07c484e19341ba9e4e90b482b6c020d6beab93e51b2e5267aeb5a339882b0
6
+ metadata.gz: '049c160b3d86b2ca1072d7b6b6fbefc44b03209c5a68411126cfc72c13b480d3d3b808ac9e02056d4582a6c7d642f50cccadc33f504a900fcfdf33012c67852d'
7
+ data.tar.gz: 4b392aa4f2dc03a25b02a18ed1ed76b7f94d271e2d37198983a3f7dbd08f3e2c8e195eab7b8f33f588af9ac948c2b43668a23cf57da7e625e815c2009b44407e
@@ -1,6 +1,12 @@
1
1
  class Date
2
- def to_time_range
3
- beginning_of_day...(self + 1.day).beginning_of_day
2
+ def to_time_range(timezone = nil)
3
+ if timezone
4
+ timezone = ActiveSupport::TimeZone[timezone] if timezone.is_a?(String)
5
+ b_day = in_time_zone(timezone)
6
+ b_day...(b_day + 1.day)
7
+ else
8
+ beginning_of_day...(self + 1.day).beginning_of_day
9
+ end
4
10
  end
5
11
 
6
12
  def prev_specific_date(day_num)
@@ -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.10'
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
data/test/test_date.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+ require 'minitest/autorun'
3
+
4
+ module Coaster
5
+ class TestMonth < Minitest::Test
6
+ def setup
7
+ Time.zone = 'Asia/Seoul'
8
+ end
9
+
10
+ def test_date_time_range
11
+ d = Date.parse('20200101')
12
+ assert_equal d.to_time_range, Time.zone.parse('2020-01-01 00:00:00')...Time.zone.parse('2020-01-02 00:00:00')
13
+ range = d.to_time_range('Pacific/Midway')
14
+ assert_equal range, ActiveSupport::TimeZone['Pacific/Midway'].parse('2020-01-01 00:00:00')...ActiveSupport::TimeZone['Pacific/Midway'].parse('2020-01-02 00:00:00')
15
+ end
16
+ end
17
+ end
@@ -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.10
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-05 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
@@ -214,6 +214,7 @@ files:
214
214
  - test/support/models.rb
215
215
  - test/support/schema.rb
216
216
  - test/test_backtrace.rb
217
+ - test/test_date.rb
217
218
  - test/test_git.rb
218
219
  - test/test_git_options.rb
219
220
  - test/test_git_repository.rb
@@ -254,6 +255,7 @@ test_files:
254
255
  - test/support/models.rb
255
256
  - test/support/schema.rb
256
257
  - test/test_backtrace.rb
258
+ - test/test_date.rb
257
259
  - test/test_git.rb
258
260
  - test/test_git_options.rb
259
261
  - test/test_git_repository.rb