Class: Cursor

Inherits:
Object show all
Defined in:
lib/swiss_db/cursor.rb

Overview

end

Constant Summary collapse

FIELD_TYPE_BLOB =
4
FIELD_TYPE_FLOAT =
2
FIELD_TYPE_INTEGER =
1
FIELD_TYPE_NULL =
0
FIELD_TYPE_STRING =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cursor, model) ⇒ Cursor

Returns a new instance of Cursor.



31
32
33
34
35
# File 'lib/swiss_db/cursor.rb', line 31

def initialize(cursor, model)
  @cursor = cursor
  @model = model
  @values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methId, *args) ⇒ Object

todo: take out setter code. it’s not used anymore. leave the getter code. it is used. (see #to_hash)



80
81
82
83
84
85
86
87
88
# File 'lib/swiss_db/cursor.rb', line 80

def method_missing(methId, *args)
  method_name = methId.id2name
  # puts "cursor method missing #{method_name}"
  if valid_getter?(method_name)
    get_method(method_name)
  else
    super
  end
end

Instance Attribute Details

#cursorObject

Returns the value of attribute cursor.



29
30
31
# File 'lib/swiss_db/cursor.rb', line 29

def cursor
  @cursor
end

#modelObject

Returns the value of attribute model.



29
30
31
# File 'lib/swiss_db/cursor.rb', line 29

def model
  @model
end

Instance Method Details

#[](pos) ⇒ Object



53
54
55
56
57
# File 'lib/swiss_db/cursor.rb', line 53

def [](pos)
  return nil if count == 0
  cursor.moveToPosition(pos) ? self : nil
  model.new(to_hash, cursor)
end

#column_namesObject



120
121
122
# File 'lib/swiss_db/cursor.rb', line 120

def column_names
  cursor.getColumnNames
end

#countObject



116
117
118
# File 'lib/swiss_db/cursor.rb', line 116

def count
  cursor.getCount
end

#firstObject



41
42
43
44
45
# File 'lib/swiss_db/cursor.rb', line 41

def first
  return nil if count == 0
  cursor.moveToFirst ? self : nil
  model.new(to_hash, cursor)
end

#get_method(method_name) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/swiss_db/cursor.rb', line 98

def get_method(method_name)
  index = cursor.getColumnIndex(method_name)
  type = cursor.getType(index)
  # puts "getting field #{method_name} at index #{index} of type #{type}"

  if type == FIELD_TYPE_STRING
    cursor.getString(index)
  elsif type == FIELD_TYPE_INTEGER
    cursor.getInt(index)
  elsif type == FIELD_TYPE_NULL
    nil #??
  elsif type == FIELD_TYPE_FLOAT
    cursor.getFloat(index)
  elsif type == FIELD_TYPE_BLOB
    cursor.getBlob(index)
  end
end

#is_setter?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/swiss_db/cursor.rb', line 94

def is_setter?(method_name)
  method_name[-1] == '='
end

#lastObject



47
48
49
50
51
# File 'lib/swiss_db/cursor.rb', line 47

def last
  return nil if count == 0
  cursor.moveToLast ? self : nil
  model.new(to_hash, cursor)
end

#to_aObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/swiss_db/cursor.rb', line 67

def to_a
  return nil if count == 0
  arr = []
  (0...count).each do |i|
    # puts i
    cursor.moveToPosition(i)
    arr << model.new(to_hash, cursor)
  end
  arr
end

#to_hashObject



59
60
61
62
63
64
65
# File 'lib/swiss_db/cursor.rb', line 59

def to_hash
    hash_obj = {}
    $current_schema[model.table_name].each do |k, v|
      hash_obj[k.to_sym] = self.send(k.to_sym)
    end
    hash_obj
end

#valid_getter?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/swiss_db/cursor.rb', line 90

def valid_getter?(method_name)
  column_names.include? method_name
end