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.



28
29
30
31
32
33
34
35
36
# File 'lib/cassanity/schema.rb', line 28

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



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

def columns
  @columns
end

#primary_keysObject (readonly) Also known as: primary_key

Internal



6
7
8
# File 'lib/cassanity/schema.rb', line 6

def primary_keys
  @primary_keys
end

#withObject (readonly)

Internal



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

def with
  @with
end

Instance Method Details

#column_namesObject

Public: Returns an array of the column names



44
45
46
# File 'lib/cassanity/schema.rb', line 44

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

#column_typesObject

Public: Returns an array of the column types



49
50
51
# File 'lib/cassanity/schema.rb', line 49

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

#composite_primary_key?Boolean

Public

Returns:

  • (Boolean)


39
40
41
# File 'lib/cassanity/schema.rb', line 39

def composite_primary_key?
  @composite_primary_key == true
end

#ensure_primary_keys_are_columnsObject

Private



54
55
56
57
58
# File 'lib/cassanity/schema.rb', line 54

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

#eql?(other) ⇒ Boolean Also known as: ==

Public: Is this schema equal to another object.

Returns:

  • (Boolean)


78
79
80
81
82
83
# File 'lib/cassanity/schema.rb', line 78

def eql?(other)
  self.class.eql?(other.class) &&
    @primary_keys == other.primary_keys &&
    @columns == other.columns &&
    @with == other.with
end

#inspectObject

Public



68
69
70
71
72
73
74
75
# File 'lib/cassanity/schema.rb', line 68

def inspect
  attributes = [
    "primary_keys=#{primary_keys.inspect}",
    "columns=#{columns.inspect}",
    "with=#{with.inspect}",
  ]
  "#<#{self.class.name}:#{object_id} #{attributes.join(', ')}>"
end

#primary_keys_are_defined_as_columns?Boolean

Private

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/cassanity/schema.rb', line 61

def primary_keys_are_defined_as_columns?
  flattened_primary_keys = @primary_keys.flatten
  shared_columns = column_names & flattened_primary_keys
  shared_columns.to_set == flattened_primary_keys.to_set
end