Class: Cassanity::Schema

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Schema

Public: Initializes a Schema.

args - The Hash of arguments.

:primary_key - The String or Symbol key or Array of String/Symbol
               keys to use as primary key.
:columns - The Hash of columns where the name is the column name
           and the value is the column type.
:with - The Hash of options for the WITH clause.

Raises KeyError if missing required argument key. Raises ArgumentError if primary key is not included in the columns.



25
26
27
28
29
30
31
32
33
# File 'lib/cassanity/schema.rb', line 25

def initialize(args = {})
  @primary_keys = Array(args.fetch(:primary_key))
  @columns = args.fetch(:columns)

  ensure_primary_keys_are_columns

  @with = args[:with] || {}
  @composite_primary_key = @primary_keys.size > 1
end

Instance Attribute Details

#columnsObject (readonly)

Internal



10
11
12
# File 'lib/cassanity/schema.rb', line 10

def columns
  @columns
end

#primary_keysObject (readonly) Also known as: primary_key

Internal



4
5
6
# File 'lib/cassanity/schema.rb', line 4

def primary_keys
  @primary_keys
end

#withObject (readonly)

Internal



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

def with
  @with
end

Instance Method Details

#column_namesObject

Public: Returns an array of the column names



41
42
43
# File 'lib/cassanity/schema.rb', line 41

def column_names
  @column_names ||= @columns.keys
end

#column_typesObject

Public: Returns an array of the column types



46
47
48
# File 'lib/cassanity/schema.rb', line 46

def column_types
  @column_types ||= @columns.values
end

#composite_primary_key?Boolean

Public

Returns:

  • (Boolean)


36
37
38
# File 'lib/cassanity/schema.rb', line 36

def composite_primary_key?
  @composite_primary_key == true
end

#ensure_primary_keys_are_columnsObject

Private



51
52
53
54
55
# File 'lib/cassanity/schema.rb', line 51

def ensure_primary_keys_are_columns
  unless primary_keys_are_defined_as_columns?
    raise ArgumentError, "Not all primary keys (#{primary_keys.inspect}) were defined as columns (#{column_names.inspect})"
  end
end

#primary_keys_are_defined_as_columns?Boolean

Private

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/cassanity/schema.rb', line 58

def primary_keys_are_defined_as_columns?
  shared_columns = column_names & @primary_keys
  shared_columns == @primary_keys
end