serialize_attributes 0.3.1 → 0.4.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: 4c06deef436a52e15560111b4055b706d769d684106edaa4d224a7121367cfde
4
- data.tar.gz: e8bf6db9353e825f8df2cae7f79634c39826c69793a91a8dd3dba60a71c6039d
3
+ metadata.gz: c505eb52395e2c8fe3786c3ef97dd04e5a56f978072d839ba517c0069f9dce93
4
+ data.tar.gz: 5ea660579c45ebd3cca56da5c3ea7e07fe9af5fb38a05fd62d3133f118f3d8df
5
5
  SHA512:
6
- metadata.gz: ff687f3298a043cfac0a8aa00931a2175640ff538c7753be1397d15a11995b234c778e2f6454ae5c4f64dd55fe6c3774beefbff349b4c4025f3020406fddaa51
7
- data.tar.gz: 0471ec6fb74122457517c3dceb602000114257b3cf98d5f11cac75506da0a7e0ab74554545c26caf34f16edc70f6b0f21ae8e5f81a7ffc6324e5009b7e871cb0
6
+ metadata.gz: 311846e88d0c92cf41bf5adbd32c1515fbd1d131df0d733cfd444963bedc8594620d05cb12cfe701b40fba851195931f6f53673dcbe25593bf257e2b28c183dc
7
+ data.tar.gz: c5e2e60ba000e0afe423cee6c37e20fae5b590a39bcadab76d65712db712359e1d4ae3c0532e44565d673cb1c8f55cd4d535b9fdc057f50d11d3e3744f0803e9
data/README.md CHANGED
@@ -7,6 +7,7 @@ class MyModel
7
7
  serialize_attributes :settings do
8
8
  attribute :user_name, :string
9
9
  attribute :subscribed, :boolean, default: false
10
+ attribute :subscriptions, :string, array: true
10
11
  end
11
12
  end
12
13
  ```
@@ -4,7 +4,8 @@ module SerializeAttributes
4
4
  # SerializeAttributes::Store is the individual store, keyed by name. You can get a
5
5
  # reference to the store by calling `Model.serialized_attributes_store(column_name)`.
6
6
  class Store
7
- def initialize(model_class, column_name, &block) # :nodoc:
7
+ def initialize(model_class, column_name, &block)
8
+ # :nodoc:
8
9
  @model_class = model_class
9
10
  @column_name = column_name
10
11
  @attributes = {}
@@ -19,30 +20,28 @@ module SerializeAttributes
19
20
  # to filter attributes by their type.
20
21
  #
21
22
  # Model.serialize_attributes_store(:settings).attribute_names
22
- # => [:user_name, :subscribed]
23
+ # => [:user_name, :subscribed, :subscriptions]
23
24
  #
24
- # Model.serialize_attributes_store(:settings).attribute_names(:string)
25
- # => [:user_name]
26
- def attribute_names(type = nil)
25
+ # Model.serialize_attributes_store(:settings).attribute_names(type: :string)
26
+ # => [:user_name, :subscriptions]
27
+ #
28
+ # Model.serialize_attributes_store(:settings).attribute_names(type: :string, array: true)
29
+ # => [:subscriptions]
30
+ #
31
+ # Model.serialize_attributes_store(:settings).attribute_names(type: :string, array: false)
32
+ # => [:user_name]
33
+ #
34
+ #
35
+ def attribute_names(type: nil, array: nil)
36
+ attributes = @attributes
37
+ attributes = @attributes.select { |_, v| v.is_a?(ArrayWrapper) == array } unless array.nil?
27
38
  if type
28
- type = ActiveModel::Type.lookup(type)&.class if type.is_a?(Symbol)
29
- @attributes.select do |_, v|
30
- v.is_a?(type)
31
- end
39
+ attributes_for_type(attributes, type)
32
40
  else
33
- @attributes
41
+ attributes
34
42
  end.keys
35
43
  end
36
44
 
37
- # Get a list of attributes of a certain type
38
- #
39
- # Model.serialize_attributes_store(:settings).attributes_of_type(:string)
40
- # => [:user_name]
41
- def attributes_of_type(type)
42
- type = ActiveModel::Type.lookup(type) if type.is_a?(Symbol)
43
- @attributes.select { |_, v| v.is_a?(type) }.keys
44
- end
45
-
46
45
  # Cast a stored attribute against a given name
47
46
  #
48
47
  # Model.serialized_attributes_store(:settings).cast(:user_name, 42)
@@ -73,9 +72,17 @@ module SerializeAttributes
73
72
 
74
73
  private
75
74
 
75
+ def attributes_for_type(attributes, type)
76
+ type = ActiveModel::Type.lookup(type)&.class if type.is_a?(Symbol)
77
+ attributes.select do |_, v|
78
+ v = v.__getobj__ if v.is_a?(ArrayWrapper)
79
+ v.is_a?(type)
80
+ end
81
+ end
82
+
76
83
  def attribute(name, type, **options)
77
84
  name = name.to_sym
78
- arr = options.delete(:array) { false }
85
+ arr = options.delete(:array) { false }
79
86
  type = ActiveModel::Type.lookup(type, **options.except(:default)) if type.is_a?(Symbol)
80
87
  type = ArrayWrapper.new(type) if arr
81
88
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SerializeAttributes
4
- VERSION = "0.3.1"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -37,7 +37,7 @@ module SerializeAttributes
37
37
  # Person.serialized_attribute_names(:settings, :string)
38
38
  # => [:user_name]
39
39
  def serialized_attribute_names(column_name, type = nil)
40
- serialized_attributes_store(column_name).attribute_names(type)
40
+ serialized_attributes_store(column_name).attribute_names(type: type)
41
41
  end
42
42
  end
43
43
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serialize_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaikio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-25 00:00:00.000000000 Z
11
+ date: 2022-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel