ja2r 0.4.3 → 0.5.0

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: f7f251df9dbf3a9e7c098a87bf32cfb3128967eb5daef41617545c0998714c28
4
- data.tar.gz: e955a027273cd2157beaf532053f91308754bf868659b99c132ccd643dcdf2cd
3
+ metadata.gz: 2b11ae08daec17f6e284b5b693609cfe80a3c4cc1297ab3f4556a6179e8f7e57
4
+ data.tar.gz: 0ffe1c67097e3d9757952ec02afb65d7bd07779c9511f07781da3fe39dfcc7f4
5
5
  SHA512:
6
- metadata.gz: 7384e93ff7b863f1a4cb099929714cc28ad7aea60ec23074579b56e8792c4ee8853453a1489b3ef5d7a02507ed0fef9e58471d56a56a632c686c52bcb5079fdd
7
- data.tar.gz: 6ac8caa94054e25cc6518db78395469ed8e17e3d9d4210559ef6b28ce590f39fb4556e037d5be8ab48af433fcee9f2703c9e75fce90034f7901a4eec9119a602
6
+ metadata.gz: 52261c788ea4be45ae5036ee6b32ef811b53b5960a6e1759344e9e0c52932cb774616a0a7e9700834c47d70b45fd2c682baeea993d52fd8d3d6eb5a77a45914d
7
+ data.tar.gz: 97f6d511b64efad17d9fc67ba59a4703ddfa1965cc2c6152d0dbeb51e6e4b7451bc78864e4d73d598e96f69c0da34f3199840ae47329dddef034416c96272f59
data/README.md CHANGED
@@ -41,3 +41,18 @@ JSON
41
41
  bart = JA2R.parse(JSON.parse(json))
42
42
  bart.sister.name # Lisa
43
43
  ```
44
+
45
+ ## Options
46
+
47
+ You can pass options to `JA2R#parse` as second argument. Currently the only supported options are:
48
+
49
+ | Name | Type | Description |
50
+ |---------------|---------|------------------------------------------------------------------------------------------------------------------------|
51
+ | safe_traverse | boolean | When setting this option to true, unknown relationships or attributes will return nil instead of raising NoMethodError |
52
+
53
+ Example:
54
+
55
+ ```ruby
56
+ bart = JA2R.parse(hash, safe_traverse: true)
57
+ bart.uncle&.name # nil
58
+ ```
data/lib/ja2r.rb CHANGED
@@ -9,8 +9,8 @@ module JA2R
9
9
 
10
10
  module_function
11
11
 
12
- def parse(hash)
13
- Parser.new(hash).call
12
+ def parse(hash, options = {})
13
+ Parser.new(hash, options).call
14
14
  end
15
15
 
16
16
  def klass_for(hash)
data/lib/ja2r/element.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  module JA2R
2
2
  class Element
3
- def initialize(origin_data)
3
+ def initialize(origin_data, options = {})
4
4
  @origin_data = origin_data.with_indifferent_access
5
+ @options = options
5
6
  @relationships = origin_data['relationships'] ? convert_relationships(origin_data['relationships']) : {}
6
7
  end
7
8
 
@@ -42,22 +43,26 @@ module JA2R
42
43
  return attributes[symbol] if attributes&.key? symbol
43
44
  return relationships[symbol] if relationships&.key? symbol
44
45
 
45
- super
46
+ safe_traverse? ? nil : super
46
47
  end
47
48
 
48
49
  def respond_to_missing?(symbol, include_all = false)
49
50
  return true if attributes&.key?(symbol)
50
51
  return true if relationships&.key?(symbol)
51
52
 
52
- super
53
+ safe_traverse? ? true : super
54
+ end
55
+
56
+ def safe_traverse?
57
+ @options[:safe_traverse]
53
58
  end
54
59
 
55
60
  def convert_relationships(hash)
56
61
  hash.each_with_object(ActiveSupport::HashWithIndifferentAccess.new) do |(key, data), memo|
57
62
  memo[key] = if data&.[]('data').is_a?(Array)
58
- data['data'].map { |d| KlassRegistry.instantiate(d) }
63
+ data['data'].map { |d| KlassRegistry.instantiate(d, @options) }
59
64
  elsif data&.[]('data').is_a?(Hash)
60
- KlassRegistry.instantiate(data&.[]('data'))
65
+ KlassRegistry.instantiate(data&.[]('data'), @options)
61
66
  end
62
67
  end
63
68
  end
@@ -2,10 +2,10 @@ module JA2R
2
2
  module KlassRegistry
3
3
  extend self
4
4
 
5
- def instantiate(hash)
5
+ def instantiate(hash, options)
6
6
  return unless hash&.key? 'type'
7
7
 
8
- lookup(hash['type']).new(hash)
8
+ lookup(hash['type']).new(hash, options)
9
9
  end
10
10
 
11
11
  def lookup(type)
data/lib/ja2r/parser.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  module JA2R
2
2
  class Parser
3
- def initialize(hash)
3
+ def initialize(hash, options = {})
4
4
  @hash = hash
5
+ @options = options
5
6
  @object_space = []
6
7
  end
7
8
 
@@ -29,20 +30,20 @@ module JA2R
29
30
  end
30
31
 
31
32
  def parse_single
32
- root = KlassRegistry.instantiate(hash['data'])
33
+ root = KlassRegistry.instantiate(hash['data'], @options)
33
34
  object_space << root
34
35
  root
35
36
  end
36
37
 
37
38
  def parse_list
38
- root = hash['data'].map { |data| KlassRegistry.instantiate(data) }
39
+ root = hash['data'].map { |data| KlassRegistry.instantiate(data, @options) }
39
40
  object_space.push(*root)
40
41
  root
41
42
  end
42
43
 
43
44
  def parse_included
44
45
  hash['included']&.map do |data|
45
- object_space.push KlassRegistry.instantiate(data)
46
+ object_space.push KlassRegistry.instantiate(data, @options)
46
47
  end
47
48
  end
48
49
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ja2r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mkon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-22 00:00:00.000000000 Z
11
+ date: 2019-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -50,28 +50,28 @@ dependencies:
50
50
  requirements:
51
51
  - - '='
52
52
  - !ruby/object:Gem::Version
53
- version: 0.63.1
53
+ version: 0.73.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - '='
59
59
  - !ruby/object:Gem::Version
60
- version: 0.63.1
60
+ version: 0.73.0
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rubocop-rspec
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - '='
66
66
  - !ruby/object:Gem::Version
67
- version: 1.32.0
67
+ version: 1.33.0
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - '='
73
73
  - !ruby/object:Gem::Version
74
- version: 1.32.0
74
+ version: 1.33.0
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: simplecov
77
77
  requirement: !ruby/object:Gem::Requirement