store_model 1.2.0 → 1.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
  SHA256:
3
- metadata.gz: 6139e784c55ab957dc9de25a43b4727c91333dde4b7c92f4f22ea2634c204bf5
4
- data.tar.gz: 04ae362612c4032445da8b05320e0d95f29a4b825bba751fa72811aabae263bd
3
+ metadata.gz: f464327535385fc17ade5f91d0c88ac4cc09233f995bc48d4e926ecd6cab4b67
4
+ data.tar.gz: 4497217fa0c66f0e8587e1e66e7ace5a83209835901ed300f15bab8a6651990b
5
5
  SHA512:
6
- metadata.gz: fc7e96d96c1d4e8b7074b7d4000755137b3199b55769ff5e57ad59235e79cde8441e80d59133a54981788a94202df8b698484a896d4ea2d22dd90fdb3633736f
7
- data.tar.gz: '001499ba6ff97ee59dc16fca855fbd9128fe9f6f85d4fe37438f4ea13bdea5ae81bf6da9281b8436b816573be6c79a15f68c6fdb0c3b610e7d23a897ba762b6b'
6
+ metadata.gz: b19c7f3fa44d7fc49925d450d1b97318002174b38dff46ae84611d61eb857833fd660570a1a4244eb8b21b60d9242d33c7e6e2e62471a3306bd8c46389308727
7
+ data.tar.gz: b710ea76a0ecb1ac7193dc5e536b947456895c1839b6efd92b5e06a5dec560d7d67f019c7c92e5c92ab5cd36df96ef730ff03fb638cbff933419fd066e3583df
@@ -23,7 +23,7 @@ module StoreModel
23
23
 
24
24
  attr_accessor :parent
25
25
 
26
- delegate :each_value, :fetch, to: :attributes
26
+ delegate :each_value, to: :attributes
27
27
 
28
28
  # Returns a hash representing the model. Some configuration can be
29
29
  # passed through +options+.
@@ -43,6 +43,21 @@ module StoreModel
43
43
  result.as_json(options)
44
44
  end
45
45
 
46
+ # Returns an Object, similar to Hash#fetch, raises
47
+ # a KeyError if attr_name doesn't exist.
48
+ # @param attr_name [String, Symbol]
49
+ #
50
+ # @return Object
51
+ def fetch(attr_name)
52
+ stringified_key = attr_name.to_s
53
+ if attribute_names.include?(stringified_key) || attribute_aliases.key?(stringified_key)
54
+ public_send(stringified_key)
55
+ else
56
+ message = attr_name.is_a?(Symbol) ? "key not found: :#{attr_name}" : "key not found: #{attr_name}"
57
+ raise KeyError, message
58
+ end
59
+ end
60
+
46
61
  # Compares two StoreModel::Model instances
47
62
  #
48
63
  # @param other [StoreModel::Model]
@@ -57,6 +72,8 @@ module StoreModel
57
72
 
58
73
  # Accessing attribute using brackets
59
74
  #
75
+ # @param attr_name [String, Symbol]
76
+ #
60
77
  # @return [Object]
61
78
  def [](attr_name)
62
79
  @attributes.fetch_value(attr_name.to_s)
@@ -50,16 +50,9 @@ module StoreModel
50
50
  #
51
51
  # @return [String] serialized value
52
52
  def serialize(value)
53
- case value
54
- when Hash
55
- ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
56
- else
57
- if implements_model?(value.class)
58
- return ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
59
- end
53
+ return super unless value.is_a?(Hash) || implements_model?(value.class)
60
54
 
61
- super
62
- end
55
+ ActiveSupport::JSON.encode(value, serialize_unknown_attributes: true)
63
56
  end
64
57
 
65
58
  protected
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StoreModel # :nodoc:
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: store_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-18 00:00:00.000000000 Z
11
+ date: 2022-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord