jsapi 1.1.1 → 1.2

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: c5106832b9b6b3b6747fc83b4469176532e56a84d2fe1779cdad6e6e34d9da4b
4
- data.tar.gz: 1d760c1fbb5556dfac42203ae05c483f2fcf99e3aa0c6bfc34c374d7093fbd0c
3
+ metadata.gz: 40ceeb6c8774b09bfb3fbfe2bc4550444a179158b43cac27d6f9923cead5971c
4
+ data.tar.gz: f6cc387571c73b93cdcc121c1c46267424a46362f4c400dee8bd53ec2e0af561
5
5
  SHA512:
6
- metadata.gz: 3261a33d232a31b42f334d0ef13e36d5e0dd92c2863948fdcb54dee791e6580a2adf26c7fe1b0685837fb01d69cf3dc1b6f6f25557840c10c6ade65725ee6aaf
7
- data.tar.gz: baaf0e9710ac6909ff970534f758a539835807d0e61515440759245960aac83e9477c231e42f48c03822a10e04afaaef19fbf3320972c303821f2e23aa4e4bfb
6
+ metadata.gz: 437497757705da6bfeddab26b70ab0e4df3b7abe11f78b4433f6b5576b783f8d576c0c3b2e6612b4e941d58136e965b705e5db37176f59ee672c58bf64e5bb42
7
+ data.tar.gz: d25820eec66069cba5bb25eb2a0c1cb262835a57ec60eef94199ab247332aa296ba5228ebf0a61c1bbc80c9b350aa19cfd2339cb11188c260973f3607cd6da5f
@@ -20,6 +20,10 @@ module Jsapi
20
20
  "#<#{self.class.name} [#{@json_values.map(&:inspect).join(', ')}]>"
21
21
  end
22
22
 
23
+ def serializable_value(**options) # :nodoc:
24
+ @json_values.map { |element| element.serializable_value(**options) }
25
+ end
26
+
23
27
  def validate(errors) # :nodoc:
24
28
  return false unless super
25
29
 
@@ -6,6 +6,8 @@ module Jsapi
6
6
  class Object < Value
7
7
  include Model::Nestable
8
8
 
9
+ alias serializable_value serializable_hash
10
+
9
11
  attr_reader :raw_additional_attributes, :raw_attributes
10
12
 
11
13
  def initialize(hash, schema, definitions, context: nil)
@@ -29,6 +29,11 @@ module Jsapi
29
29
  false
30
30
  end
31
31
 
32
+ # Returns a serializable representation of +value+.
33
+ def serializable_value(**options)
34
+ options[:jsonify_values] == true ? value.as_json : value
35
+ end
36
+
32
37
  # Validates it against #schema. Returns true if it is valid, false otherwise.
33
38
  # Detected errors are added to +errors+.
34
39
  def validate(errors)
@@ -25,7 +25,43 @@ module Jsapi
25
25
  extend ActiveModel::Translation
26
26
  include ActiveModel::Validations
27
27
 
28
- delegate :[], :additional_attributes, :attribute?, :attributes, to: :@nested
28
+ ##
29
+ # :method: []
30
+ # :call-seq: [](name)
31
+ #
32
+ # Returns the value assigned to +name+.
33
+
34
+ ##
35
+ # :method: additional_attributes
36
+ #
37
+ # Returns a hash containing the additional attributes.
38
+
39
+ ##
40
+ # :method: attribute?
41
+ # :call-seq: attribute?(name)
42
+ #
43
+ # Returns +true+ if +name+ is present, +false+ otherwise.
44
+
45
+ ##
46
+ # :method: attributes
47
+ #
48
+ # Returns a hash containing all attributes.
49
+
50
+ ##
51
+ # :method: serializable_hash
52
+ # :call-seq: serializable_hash(**options)
53
+ #
54
+ # Returns a hash containing serializable representations of all attributes.
55
+ #
56
+ # Possible options are:
57
+ #
58
+ # - +:only+ - The hash contains the given attributes only.
59
+ # - +:except+ - The hash does not contain the given attributes.
60
+ # - +:symbolize_names+ - If set to true, keys are symbols.
61
+ # - +:jsonify_values+ - If set to true, values are converted by +as_json+.
62
+
63
+ delegate :[], :additional_attributes, :attribute?, :attributes,
64
+ :serializable_hash, to: :@nested
29
65
 
30
66
  validate :_nested_validity
31
67
 
@@ -13,7 +13,7 @@ module Jsapi
13
13
  raw_additional_attributes.transform_values(&:value)
14
14
  end
15
15
 
16
- # Returns +true+ if +name+ is present, false +otherwise+.
16
+ # Returns +true+ if +name+ is present, +false+ otherwise.
17
17
  def attribute?(name)
18
18
  raw_attributes.key?(name&.to_s)
19
19
  end
@@ -32,6 +32,32 @@ module Jsapi
32
32
  }>"
33
33
  end
34
34
 
35
+ # Returns a hash containing serializable representations of all attributes.
36
+ #
37
+ # Possible options are:
38
+ #
39
+ # - +:only+ - The hash contains the given attributes only.
40
+ # - +:except+ - The hash does not contain the given attributes.
41
+ # - +:symbolize_names+ - If set to true, keys are symbols.
42
+ # - +:jsonify_values+ - If set to true, values are converted by +as_json+.
43
+ def serializable_hash(**options)
44
+ options = options.dup
45
+ except = options.delete(:except)&.map(&:to_s)
46
+ only = options.delete(:only)&.map(&:to_s)
47
+ symbolize_names = options[:symbolize_names] == true
48
+
49
+ {}.tap do |hash|
50
+ [raw_attributes, raw_additional_attributes].each do |attributes|
51
+ attributes.each do |name, value|
52
+ next if except&.include?(name) || only&.exclude?(name)
53
+
54
+ name = name.to_sym if symbolize_names
55
+ hash[name] = value.serializable_value(**options)
56
+ end
57
+ end
58
+ end
59
+ end
60
+
35
61
  private
36
62
 
37
63
  def validate_attributes(errors)
data/lib/jsapi/version.rb CHANGED
@@ -5,6 +5,6 @@ module Jsapi
5
5
  # NOTE: See https://bundler.io/guides/creating_gem.html
6
6
 
7
7
  # The current GEM version.
8
- VERSION = '1.1.1'
8
+ VERSION = '1.2'
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: '1.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Göller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-06-19 00:00:00.000000000 Z
11
+ date: 2025-08-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: denis@dmgoeller.de