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
-
#field(options) ⇒ Object
Set Field: Configures a field on the current Model.
-
#field_schema ⇒ Hash
Get Field Schema: Builds a schema for the current model.
-
#fields ⇒ Hash
Get Fields: Simply returns the model’s field map.
-
#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).
Instance Method Details
#field(options) ⇒ Object
Set Field: Configures a field on the current Model.
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 # Internalise Options (so we can mess it up) = .clone # Pull Name name, ftype = .first .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] = end |
#field_schema ⇒ Hash
Get Field Schema: Builds a schema for the current model.
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 |
#fields ⇒ Hash
Get Fields: Simply returns the model’s field map.
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).
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 |