Class: CouchTap::Builders::Table
- Inherits:
-
Object
- Object
- CouchTap::Builders::Table
- 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
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#primary_keys ⇒ Object
readonly
Returns the value of attribute primary_keys.
Instance Method Summary collapse
- #collection(field, opts = {}, &block) ⇒ Object
-
#column(*args) ⇒ Object
DSL Methods.
- #database ⇒ Object
- #document ⇒ Object (also: #doc)
-
#execute ⇒ Object
Support Methods.
- #handler ⇒ Object
- #id ⇒ Object
-
#initialize(parent, name, opts = {}, &block) ⇒ Table
constructor
A new instance of Table.
-
#key_filter ⇒ Object
Grab the latest set of values to filter with.
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
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
12 13 14 |
# File 'lib/couch_tap/builders/table.rb', line 12 def attributes @attributes end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/couch_tap/builders/table.rb', line 13 def data @data end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/couch_tap/builders/table.rb', line 13 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
13 14 15 |
# File 'lib/couch_tap/builders/table.rb', line 13 def parent @parent end |
#primary_keys ⇒ Object (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 |
#database ⇒ Object
48 49 50 |
# File 'lib/couch_tap/builders/table.rb', line 48 def database @database ||= handler.database end |
#document ⇒ Object Also known as: doc
43 44 45 |
# File 'lib/couch_tap/builders/table.rb', line 43 def document parent.document end |
#execute ⇒ Object
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 |
#handler ⇒ Object
35 36 37 |
# File 'lib/couch_tap/builders/table.rb', line 35 def handler parent.handler end |
#id ⇒ Object
39 40 41 |
# File 'lib/couch_tap/builders/table.rb', line 39 def id handler.id end |
#key_filter ⇒ Object
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 |