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:

  • name (String)

    the column name

Returns:



37
38
39
# File 'lib/redpear/schema/collection.rb', line 37

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

#include?(name) ⇒ Boolean

Returns if name is part of the collection.

Parameters:

  • name (String)

    the column name

Returns:

  • (Boolean)

    if name is part of the collection



31
32
33
# File 'lib/redpear/schema/collection.rb', line 31

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

#indiciesArray

Returns only the index columns.

Returns:

  • (Array)

    only the index columns



25
26
27
# File 'lib/redpear/schema/collection.rb', line 25

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



20
21
22
# File 'lib/redpear/schema/collection.rb', line 20

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



15
16
17
# File 'lib/redpear/schema/collection.rb', line 15

def names
  @names ||= lookup.keys
end

#reset!Object

Resets indexes and lookups



42
43
44
45
46
# File 'lib/redpear/schema/collection.rb', line 42

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

#store(klass, *args) ⇒ Object

Parameters:

  • klass (Class)
  • args (multiple)

    the column definition.

See Also:



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

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