Class: Arango::Result
- Inherits:
-
Object
show all
- Defined in:
- lib/arango/result.rb
Overview
Arango Result implements result representation from Arango::Request
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(result) ⇒ Result
Returns a new instance of Result.
7
8
9
10
11
12
13
14
15
|
# File 'lib/arango/result.rb', line 7
def initialize(result)
@result = result ? result : {}
@is_array = @result.class == Array
unless @is_array
@result.transform_keys!{ |k| k.to_s.underscore.to_sym }
@error = @result.key?(:error) ? @result.delete(:error) : false
@response_code = @result.key?(:code) ? @result.delete(:code) : false
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(field_name_or_index, *args, &block) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/arango/result.rb', line 48
def method_missing(field_name_or_index, *args, &block)
return self[field_name_or_index] = args[0] if field_name_or_index.to_s.end_with?('=')
return self[field_name_or_index] if @is_array
field_name_or_index = field_name_or_index.to_sym
return self[field_name_or_index] if key?(field_name_or_index)
@result.send(field_name_or_index, *args, &block)
end
|
Instance Attribute Details
#error ⇒ Object
Returns the value of attribute error.
17
18
19
|
# File 'lib/arango/result.rb', line 17
def error
@error
end
|
#response_code ⇒ Object
Returns the value of attribute response_code.
17
18
19
|
# File 'lib/arango/result.rb', line 17
def response_code
@response_code
end
|
Instance Method Details
#[](field_name_or_index) ⇒ Object
access to all other fields
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/arango/result.rb', line 24
def [](field_name_or_index)
return @result[field_name_or_index] if @is_array
field_name_y = field_name_or_index.to_sym
return @result[field_name_y] if @result.key?(field_name_y)
field_name_s = field_name_or_index.to_s
field_name_lcy = field_name_s.camelize(:lower).to_sym
return @result[field_name_lcy] if @result.key?(field_name_lcy)
field_name_ucy = field_name_s.camelize(:upper).to_sym
return @result[field_name_ucy] if @result.key?(field_name_ucy)
nil
end
|
#[]=(field_name_or_index, value) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/arango/result.rb', line 36
def []=(field_name_or_index, value)
return @result[field_name_or_index] = value if @is_array
field_name_y = field_name_or_index.to_sym
return @result[field_name_y] = value if @result.key?(field_name_y)
field_name_s = field_name_or_index.to_s
field_name_lcy = field_name_s.camelize(:lower).to_sym
return @result[field_name_lcy] = value if @result.key?(field_name_lcy)
field_name_ucy = field_name_s.camelize(:upper).to_sym
return @result[field_name_ucy] = value if @result.key?(field_name_ucy)
nil
end
|
#delete_if(*args, &block) ⇒ Object
56
57
58
|
# File 'lib/arango/result.rb', line 56
def delete_if(*args, &block)
@result.delete_if(*args, &block)
end
|
#empty? ⇒ Boolean
60
61
62
|
# File 'lib/arango/result.rb', line 60
def empty?
@result.empty?
end
|
#error? ⇒ Boolean
19
20
21
|
# File 'lib/arango/result.rb', line 19
def error?
!!@error
end
|
#first ⇒ Object
64
65
66
|
# File 'lib/arango/result.rb', line 64
def first
@result.first
end
|
#is_array? ⇒ Boolean
68
69
70
|
# File 'lib/arango/result.rb', line 68
def is_array?
@is_array
end
|
#key?(key) ⇒ Boolean
Also known as:
has_key?
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/arango/result.rb', line 72
def key?(key)
return false if @is_array
field_name_y = key.to_sym
return true if @result.key?(field_name_y)
field_name_s = key.to_s
field_name_lcy = field_name_s.camelize(:lower).to_sym
return true if @result.key?(field_name_lcy)
field_name_ucy = field_name_s.camelize(:upper).to_sym
return true if @result.key?(field_name_ucy)
false
end
|
#map(*args, &block) ⇒ Object
85
86
87
|
# File 'lib/arango/result.rb', line 85
def map(*args, &block)
@result.map(*args, &block)
end
|
#raw_result ⇒ Object
93
94
95
|
# File 'lib/arango/result.rb', line 93
def raw_result
@result
end
|
#result ⇒ Object
89
90
91
|
# File 'lib/arango/result.rb', line 89
def result
@result[:result]
end
|
#to_a ⇒ Object
103
104
105
106
|
# File 'lib/arango/result.rb', line 103
def to_a
return @result if @is_array
@result.to_a
end
|
#to_ary ⇒ Object
108
109
110
111
|
# File 'lib/arango/result.rb', line 108
def to_ary
return @result.to_ary if @is_array
to_a
end
|
#to_h ⇒ Object
Also known as:
to_hash
97
98
99
100
|
# File 'lib/arango/result.rb', line 97
def to_h
return @result unless @is_array
@result.to_h
end
|
#to_s ⇒ Object
113
114
115
|
# File 'lib/arango/result.rb', line 113
def to_s
@result.to_s
end
|