Module: Siren::JsonQuery::FieldAccess

Defined in:
lib/siren/json_query_nodes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.access(object, index) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/siren/json_query_nodes.rb', line 84

def self.access(object, index)
  return (Hash === object ? object.values : object) if index == :*
  return object[index] if Array === object and Numeric === index
  
  if Hash === object
    key = [index, index.to_s, index.to_sym].find { |i| object.has_key?(i) }
    return object[key] if key
  end
  
  return object.__send__(index) if object.respond_to?(index)
  object.instance_variable_get("@#{ index }")
end

Instance Method Details

#index(object, root, symbols, current = nil) ⇒ Object



71
72
73
74
75
# File 'lib/siren/json_query_nodes.rb', line 71

def index(object, root, symbols, current = nil)
  element = elements[1]
  return [element.text_value] if Symbol === element
  element.value(root, symbols, object)
end

#value(object, root, symbols, current = nil) ⇒ Object



77
78
79
80
81
82
# File 'lib/siren/json_query_nodes.rb', line 77

def value(object, root, symbols, current = nil)
  indexes = index(object, root, symbols, current)
  indexes.size == 1 ?
      FieldAccess.access(object, indexes.first) :
      indexes.map { |i| FieldAccess.access(object, i) }
end