Class: Record

Inherits:
Object
  • Object
show all
Defined in:
lib/active_recorder/record.rb

Overview

An record class to represent an ActiveRecord table

Constant Summary collapse

VALID_TYPES =

Array with valid ActiveRecord datatypes

%w(
  binary boolean date datetime decimal float integer
  primary_key references string text time timestamp
).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Record

Record constructor to initialize Record with name



13
14
15
16
17
18
19
20
# File 'lib/active_recorder/record.rb', line 13

def initialize(name)
  # Validate name
  fail ArgumentError, 'Name cannot be nil!' if name.nil?
  fail ArgumentError, 'Name cannot be empty!' if name.empty?
  # Initialize name and columns
  @name = name
  @columns = {}
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



5
6
7
# File 'lib/active_recorder/record.rb', line 5

def columns
  @columns
end

#nameObject (readonly)

Instance variable: name of the Model using table



4
5
6
# File 'lib/active_recorder/record.rb', line 4

def name
  @name
end

Instance Method Details

#add_column(col, type) ⇒ Object

Adds a column to record



23
24
25
26
27
28
# File 'lib/active_recorder/record.rb', line 23

def add_column(col, type)
  # Check whether the column added is a valid ActiveRecord type
  fail ArgumentError, 'Invalid ActiveRecord datatype!' unless VALID_TYPES.include?(type)
  # Add column to record
  @columns[col] = type
end