Class: Redpear::Schema::Collection

Inherits:
Array
  • Object
show all
Defined in:
lib/redpear/schema/collection.rb

Overview

Stores the column information

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Redpear::Schema::Column

Returns the column for the given name.

Parameters:

  • the (String)

    column name

Returns:



35
36
37
# File 'lib/redpear/schema/collection.rb', line 35

def [](name)
  lookup[name.to_s]
end

#include?(name) ⇒ Boolean

Returns if name is part of the collection.

Parameters:

  • the (String)

    column name

Returns:

  • (Boolean)

    if name is part of the collection



29
30
31
# File 'lib/redpear/schema/collection.rb', line 29

def include?(name)
  lookup.key?(name.to_s)
end

#indiciesArray

Returns only the index columns.

Returns:

  • (Array)

    only the index columns



23
24
25
# File 'lib/redpear/schema/collection.rb', line 23

def indicies
  @indicies ||= to_a.select {|i| i.is_a?(Redpear::Schema::Index) }
end

#lookupHash

Returns the column lookup, indexed by name.

Returns:

  • (Hash)

    the column lookup, indexed by name



18
19
20
# File 'lib/redpear/schema/collection.rb', line 18

def lookup
  @lookup ||= inject({}) {|r, c| r.update c.to_s => c }
end

#namesArray

Returns the names of the columns.

Returns:

  • (Array)

    the names of the columns



13
14
15
# File 'lib/redpear/schema/collection.rb', line 13

def names
  @names ||= lookup.keys
end

#reset!Object

Resets indexes and lookups



40
41
42
43
44
# File 'lib/redpear/schema/collection.rb', line 40

def reset!
  instance_variables.each do |name|
    instance_variable_set name, nil
  end
end

#store(klass, *args) ⇒ Object

Parameters:

  • the (multiple)

    column definition. Please see Redpear::Schema::Column#initialize



5
6
7
8
9
10
# File 'lib/redpear/schema/collection.rb', line 5

def store(klass, *args)
  reset!
  klass.new(*args).tap do |col|
    self << col
  end
end