Class: Cassanity::Schema
- Inherits:
-
Object
- Object
- Cassanity::Schema
- Defined in:
- lib/cassanity/schema.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Internal.
-
#primary_keys ⇒ Object
(also: #primary_key)
readonly
Internal.
-
#with ⇒ Object
readonly
Internal.
Instance Method Summary collapse
-
#column_names ⇒ Object
Public: Returns an array of the column names.
-
#column_types ⇒ Object
Public: Returns an array of the column types.
-
#composite_primary_key? ⇒ Boolean
Public.
-
#ensure_primary_keys_are_columns ⇒ Object
Private.
-
#initialize(args = {}) ⇒ Schema
constructor
Public: Initializes a Schema.
-
#primary_keys_are_defined_as_columns? ⇒ Boolean
Private.
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
#columns ⇒ Object (readonly)
Internal
10 11 12 |
# File 'lib/cassanity/schema.rb', line 10 def columns @columns end |
#primary_keys ⇒ Object (readonly) Also known as: primary_key
Internal
4 5 6 |
# File 'lib/cassanity/schema.rb', line 4 def primary_keys @primary_keys end |
#with ⇒ Object (readonly)
Internal
13 14 15 |
# File 'lib/cassanity/schema.rb', line 13 def with @with end |
Instance Method Details
#column_names ⇒ Object
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_types ⇒ Object
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
36 37 38 |
# File 'lib/cassanity/schema.rb', line 36 def composite_primary_key? @composite_primary_key == true end |
#ensure_primary_keys_are_columns ⇒ Object
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
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 |