Method: Xjson#reference_handle

Defined in:
lib/xjson.rb

#reference_handle(data, ref_desc) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/xjson.rb', line 68

def reference_handle( data, ref_desc )
    if ref_desc[0] != ":"
        # [ data, ref_desc ]
        reference_handle( data, ":#{ref_desc}" )
    else
        # Relative reference from root.
        path = ref_desc.split( ":" )[1..-1]
        scope = data
        while path[0..-2].any?
            if path[0] == "*"
                # Wildcard for array.
                unless path[1] && path[2]
                    raise XjsonReferenceError,
                    "Invalid reference: \"#{ref_desc}\" in \"#{@cur_file[0]}\", missing match key and value ..."
                end
                index = find_in_array_of_hash( scope, path[1], path[2] )
                unless index
                    raise XjsonReferenceError,
                    "Invalid reference: \"#{ref_desc}\" in \"#{@cur_file[0]}\", key and value not matched ..."
                end
                scope = scope[ index ]
                path.shift( 2 )
            else
                begin
                    index = Integer( path[0] )
                    scope = scope[ index ]
                rescue
                    scope = scope[ path[0] ]
                end
            end
            path.shift
            unless scope
                raise XjsonReferenceError,
                "Invalid reference: \"#{ref_desc}\" in \"#{@cur_file[0]}\"..."
            end
        end
        [ scope, path[-1] ]
    end
end