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.
-
#eql?(other) ⇒ Boolean
(also: #==)
Public: Is this schema equal to another object.
-
#initialize(args = {}) ⇒ Schema
constructor
Public: Initializes a Schema.
-
#inspect ⇒ Object
Public.
-
#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 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
#columns ⇒ Object (readonly)
Internal
12 13 14 |
# File 'lib/cassanity/schema.rb', line 12 def columns @columns end |
#primary_keys ⇒ Object (readonly) Also known as: primary_key
Internal
6 7 8 |
# File 'lib/cassanity/schema.rb', line 6 def primary_keys @primary_keys end |
#with ⇒ Object (readonly)
Internal
15 16 17 |
# File 'lib/cassanity/schema.rb', line 15 def with @with end |
Instance Method Details
#column_names ⇒ Object
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_types ⇒ Object
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
39 40 41 |
# File 'lib/cassanity/schema.rb', line 39 def composite_primary_key? @composite_primary_key == true end |
#ensure_primary_keys_are_columns ⇒ Object
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.
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 |
#inspect ⇒ Object
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
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 |