Class: CouchTap::Builders::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_tap/builders/table.rb

Overview

Deal with a table definition that will automatically insert a new row into the table.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, opts = {}, &block) ⇒ Table

Returns a new instance of Table.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/couch_tap/builders/table.rb', line 15

def initialize(parent, name, opts = {}, &block)
  @_collections = []

  @parent = parent
  @data   = opts[:data] || parent.document
  @name   = name.to_sym

  @primary_keys = parent.primary_keys.dup
  unless opts[:primary_key] === false
    @primary_keys << (opts[:primary_key] || "#{@name.to_s.singularize}_id").to_sym
  end

  # Prepare the attributes
  @attributes = {}
  set_primary_keys
  set_attributes_from_data

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



12
13
14
# File 'lib/couch_tap/builders/table.rb', line 12

def attributes
  @attributes
end

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/couch_tap/builders/table.rb', line 13

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/couch_tap/builders/table.rb', line 13

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



13
14
15
# File 'lib/couch_tap/builders/table.rb', line 13

def parent
  @parent
end

#primary_keysObject (readonly)

Returns the value of attribute primary_keys.



13
14
15
# File 'lib/couch_tap/builders/table.rb', line 13

def primary_keys
  @primary_keys
end

Instance Method Details

#collection(field, opts = {}, &block) ⇒ Object



76
77
78
# File 'lib/couch_tap/builders/table.rb', line 76

def collection(field, opts = {}, &block)
  @_collections << Collection.new(self, field, opts, &block)
end

#column(*args) ⇒ Object

DSL Methods



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/couch_tap/builders/table.rb', line 64

def column(*args)
  column = args.first
  field  = args.last
  if block_given?
    set_attribute(column, yield)
  elsif field.is_a?(Symbol)
    set_attribute(column, data[field.to_s])
  elsif args.length > 1
    set_attribute(column, field)
  end
end

#databaseObject



48
49
50
# File 'lib/couch_tap/builders/table.rb', line 48

def database
  @database ||= handler.database
end

#documentObject Also known as: doc



43
44
45
# File 'lib/couch_tap/builders/table.rb', line 43

def document
  parent.document
end

#executeObject

Support Methods



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/couch_tap/builders/table.rb', line 82

def execute
  # Insert the record and prepare ID for sub-tables
  id = dataset.insert(attributes)
  set_attribute(primary_keys.last, id) unless id.blank?

  # Now go through each collection entry
  if @_collections.length > 0
    @_collections.each do |collection|
      collection.execute
    end
  end
end

#handlerObject



35
36
37
# File 'lib/couch_tap/builders/table.rb', line 35

def handler
  parent.handler
end

#idObject



39
40
41
# File 'lib/couch_tap/builders/table.rb', line 39

def id
  handler.id
end

#key_filterObject

Grab the latest set of values to filter with. This is only relevant in sub-tables.



54
55
56
57
58
59
60
# File 'lib/couch_tap/builders/table.rb', line 54

def key_filter
  hash = {}
  primary_keys.each do |k|
    hash[k] = attributes[k]
  end
  hash
end