Module: Sack::Database::Model::ClassMethods

Defined in:
lib/sack/database/model.rb

Overview

Class Methods: Collection of methods to be injected into anything that includes this module.

Instance Method Summary collapse

Instance Method Details

#field(options) ⇒ Object

Set Field: Configures a field on the current Model.

Parameters:

  • options (Hash)

    Options defining the field - see README



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sack/database/model.rb', line 73

def field options

	# Internalise Options (so we can mess it up)
	options = options.clone

	# Pull Name
	name, ftype = options.first
	options.delete name

	# Check Primary Type
	raise "Invalid Field Primary Type [#{ftype.first}] for [#{name}] in #{mod_name}" unless FTYPES_CLASSES.include? ftype.first

	# Collect Validation Rules
	@fields ||= {}
	@fields[name] ||= {}
	@fields[name][:ftype] = ftype
	@fields[name][:rules] = options
end

#field_schemaHash

Get Field Schema: Builds a schema for the current model.

Returns:

  • (Hash)

    The current model’s schema



102
103
104
# File 'lib/sack/database/model.rb', line 102

def field_schema
	Hash[*(@fields.inject([]) { |a, e| a << e[0] << e[1][:ftype] })]
end

#fieldsHash

Get Fields: Simply returns the model’s field map.

Returns:

  • (Hash)

    The field map for the current model



95
96
97
# File 'lib/sack/database/model.rb', line 95

def fields
	@fields
end

#table_name(name = nil) ⇒ Symbol

Get / Set Table Name: Determines the default table name through introspection, or overrides the default table name (if name is provided).

Parameters:

  • name (Symbol) (defaults to: nil)

    Override table name with this

Returns:

  • (Symbol)

    The table name



59
60
61
62
63
64
65
66
67
68
# File 'lib/sack/database/model.rb', line 59

def table_name name = nil

	# Introspect Default Name
	@table_name ||= self.mod_name.snakecase

	# Override with custom name
	@table_name = name if name

	@table_name.to_sym
end