module Sack::Database::Model::ClassMethods
Class Methods: Collection of methods to be injected into anything that includes this module.
Public Instance Methods
field(options)
click to toggle source
Set Field: Configures a field on the current Model
. @param [Hash] options Options defining the field - see README
# 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_schema()
click to toggle source
Get Field Schema: Builds a schema for the current model. @return [Hash] The current model's schema
# File lib/sack/database/model.rb, line 102 def field_schema Hash[*(@fields.inject([]) { |a, e| a << e[0] << e[1][:ftype] })] end
fields()
click to toggle source
Get Fields: Simply returns the model's field map. @return [Hash] The field map for the current model
# File lib/sack/database/model.rb, line 95 def fields @fields end
table_name(name = nil)
click to toggle source
Get / Set Table Name: Determines the default table name through introspection, or overrides the default table name (if name is provided). @param [Symbol] name Override table name with this @return [Symbol] The table name
# 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