@@ -3,7 +3,8 @@ class Element
33 def initialize ( origin_data , options = { } )
44 @origin_data = origin_data . with_indifferent_access
55 @options = options
6- @relationships = origin_data [ 'relationships' ] ? convert_relationships ( origin_data [ 'relationships' ] ) : { }
6+ @relationships = origin_data [ 'relationships' ] &.then { |rd | convert_relationships ( rd ) } || { }
7+ build_references
78 end
89
910 attr_reader :origin_data , :relationships
@@ -39,18 +40,31 @@ def type
3940
4041 private
4142
42- def method_missing ( symbol , *args )
43- return attributes [ symbol ] if attributes &.key? symbol
44- return relationships [ symbol ] if relationships &.key? symbol
43+ def build_references
44+ @references = { } . with_indifferent_access
45+ attributes &.each_key do |key |
46+ @references [ key ] ||= { attribute : key }
47+ @references [ key . underscore ] ||= { attribute : key }
48+ end
49+ relationships &.each_key do |key |
50+ @references [ key ] ||= { relationship : key }
51+ @references [ key . underscore ] ||= { relationship : key }
52+ end
53+ end
4554
46- safe_traverse? ? nil : super
55+ def method_missing ( symbol , *args )
56+ case @references [ symbol ]
57+ in attribute :
58+ attributes [ attribute ]
59+ in relationship :
60+ relationships [ relationship ]
61+ else
62+ safe_traverse? ? nil : super
63+ end
4764 end
4865
4966 def respond_to_missing? ( symbol , include_all = false )
50- return true if attributes &.key? ( symbol )
51- return true if relationships &.key? ( symbol )
52-
53- safe_traverse? || super
67+ @references . key? ( symbol ) || safe_traverse? || super
5468 end
5569
5670 def safe_traverse?
0 commit comments