Class: Flounder::Result::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/flounder/result/row.rb

Instance Method Summary collapse

Constructor Details

#initialize(node, row_idx) ⇒ Row

Returns a new instance of Row.



3
4
5
6
7
# File 'lib/flounder/result/row.rb', line 3

def initialize node, row_idx
  @root = node
  @row_idx = row_idx
  @attributes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

Primary resolution mechanism: Lazy lookup of fields.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/flounder/result/row.rb', line 11

def method_missing sym, *args, &block
  if @root.has_obj?(sym)
    return cache_attribute(sym)
  end

  if sym.to_s.end_with?(??)
    stripped = sym.to_s[0..-2]
    return @root.has_obj?(stripped) && !value_for(stripped).nil?
  end

  super
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flounder/result/row.rb', line 36

def == other
  if other.kind_of?(Row)
    other_root = other.instance_variable_get('@root')

    __columns__ == other.__columns__ && 
      __columns__.all? { |name| 
        my_value = self[name]
        other_value = other[name]
        my_value == other_value }
  else
    super
  end
end

#[](name) ⇒ Object



50
51
52
53
54
# File 'lib/flounder/result/row.rb', line 50

def [] name
  if @root.has_obj?(name)
    value_for(name)
  end
end

#__columns__Object

Returns all column names.



64
65
66
# File 'lib/flounder/result/row.rb', line 64

def __columns__
  @root.names
end

#inspectObject



32
33
34
# File 'lib/flounder/result/row.rb', line 32

def inspect
  "flounder/Row(#{__columns__.inspect})"
end

#methods(regular = true) ⇒ Object



28
29
30
# File 'lib/flounder/result/row.rb', line 28

def methods regular=true
  (__columns__ + super).uniq
end

#respond_to?(sym, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/flounder/result/row.rb', line 23

def respond_to? sym, include_all=false
  @attributes.has_key?(sym) ||
    @root.has_obj?(sym) || 
    super
end

#to_hObject

Turns this row into a hash, performing deep conversion of all field names. Use this to go into the Hash world and never come back.



71
72
73
# File 'lib/flounder/result/row.rb', line 71

def to_h
  __columns__.map { |name| [name, value_for(name)] }.to_h
end

#values_at(*keys) ⇒ Object

Returns values of given keys as an array.



58
59
60
# File 'lib/flounder/result/row.rb', line 58

def values_at *keys
  keys.map { |key| self[key] }
end