Class: Barely::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/barely.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(visitor = nil) ⇒ Connection

Returns a new instance of Connection.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/barely.rb', line 14

def initialize(visitor = nil)
  @tables = %w{ users photos developers products}
  @columns = {
    'users' => [
      Column.new('id', :integer),
      Column.new('name', :string),
      Column.new('bool', :boolean),
      Column.new('created_at', :date)
    ],
    'products' => [
      Column.new('id', :integer),
      Column.new('price', :decimal)
    ]
  }
  @columns_hash = {
    'users' => Hash[@columns['users'].map { |x| [x.name, x] }],
    'products' => Hash[@columns['products'].map { |x| [x.name, x] }]
  }
  @primary_keys = {
    'users' => 'id',
    'products' => 'id'
  }
  @visitor = visitor
end

Instance Attribute Details

#tablesObject (readonly)

Returns the value of attribute tables.



11
12
13
# File 'lib/barely.rb', line 11

def tables
  @tables
end

#visitorObject

Returns the value of attribute visitor.



12
13
14
# File 'lib/barely.rb', line 12

def visitor
  @visitor
end

Instance Method Details

#columns(name, message = nil) ⇒ Object



51
52
53
# File 'lib/barely.rb', line 51

def columns name, message = nil
  @columns[name.to_s]
end

#columns_hash(table_name) ⇒ Object



39
40
41
# File 'lib/barely.rb', line 39

def columns_hash table_name
  @columns_hash[table_name]
end

#primary_key(name) ⇒ Object



43
44
45
# File 'lib/barely.rb', line 43

def primary_key name
  @primary_keys[name.to_s]
end

#quote(thing, column = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/barely.rb', line 67

def quote thing, column = nil
  if column && !thing.nil?
    case column.type
    when :integer
      thing = thing.to_i
    when :string
      thing = thing.to_s
    end
  end

  case thing
  when DateTime
    "'#{thing.strftime("%Y-%m-%d %H:%M:%S")}'"
  when Date
    "'#{thing.strftime("%Y-%m-%d")}'"
  when true
    "'t'"
  when false
    "'f'"
  when nil
    'NULL'
  when Numeric
    thing
  else
    "'#{thing.to_s.gsub("'", "\\\\'")}'"
  end
end

#quote_column_name(name) ⇒ Object



59
60
61
# File 'lib/barely.rb', line 59

def quote_column_name name
  "\"#{name.to_s}\""
end

#quote_table_name(name) ⇒ Object



55
56
57
# File 'lib/barely.rb', line 55

def quote_table_name name
  "\"#{name.to_s}\""
end

#schema_cacheObject



63
64
65
# File 'lib/barely.rb', line 63

def schema_cache
  self
end

#table_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/barely.rb', line 47

def table_exists? name
  @tables.include? name.to_s
end