Class: CartoDB::Model::Scope

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Constants
Defined in:
lib/cartodb-rb-client/cartodb/model/scope.rb

Constant Summary

Constants included from Constants

Constants::CARTODB_TYPES, Constants::DEFAULT_ROWS_PER_PAGE, Constants::GEOMETRY_COLUMN, Constants::INVALID_COLUMNS, Constants::RGEO_FACTORY

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Scope

Returns a new instance of Scope.



9
10
11
12
13
14
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 9

def initialize(model)
  @model         = model
  @records       = nil
  @custom_fields = nil
  @rows_per_page = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



83
84
85
86
87
88
89
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 83

def method_missing(method, *args, &block)
  if Array.method_defined?(method)
    to_a.send(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#build_sqlObject Also known as: to_sql



97
98
99
100
101
102
103
104
105
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 97

def build_sql
  select     = build_select
  from       = build_from
  where      = build_where
  order      = build_order
  pagination = build_pagination

  sql = "#{select} #{from} #{where} #{order} #{pagination}"
end

#lengthObject Also known as: size, count



33
34
35
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 33

def length
  to_a.length
end

#order(order_clause) ⇒ Object



76
77
78
79
80
81
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 76

def order(order_clause)
  @order_clauses ||= []
  @order_clauses << order_clause

  self
end

#page(page_number) ⇒ Object



64
65
66
67
68
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 64

def page(page_number)
  @page = page_number

  self
end

#per_page(ammount) ⇒ Object



70
71
72
73
74
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 70

def per_page(ammount)
  @rows_per_page = ammount

  self
end

#select(*fields) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 39

def select(*fields)
  case fields
  when String
    @custom_fields = fields
  when Array
    @custom_fields = fields.join(', ')
  end

  self
end

#to_aObject Also known as: all



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 16

def to_a
  @records ||= begin

    results = connection.query build_sql

    if results && results.rows
      results.rows.map{|r| @model.new(r)}
    else
      []
    end
  rescue Exception => e
    []
  end

end

#where(attributes = nil, *rest) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cartodb-rb-client/cartodb/model/scope.rb', line 50

def where(attributes = nil, *rest)
  @records = nil
  return all if attributes.nil? || (attributes.is_a?(Hash) && attributes.empty?) || (attributes.is_a?(Integer) && attributes <= 0)

  if attributes.is_a?(Integer) || (attributes.length == 1 && (attributes[:cartodb_id] || attributes[:id]))
    row_id = attributes.is_a?(Integer) ? attributes : (attributes[:cartodb_id] || attributes[:id])
    return @model.new(connection.row(table_name, row_id))
  end

  create_filters(attributes, rest)

  self
end