Module: JsonQuery::FieldAccess

Defined in:
lib/json_query/nodes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.access(object, index) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/json_query/nodes.rb', line 52

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



39
40
41
42
43
# File 'lib/json_query/nodes.rb', line 39

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



45
46
47
48
49
50
# File 'lib/json_query/nodes.rb', line 45

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