Class: Mongify::Mongoid::Model
- Inherits:
-
Object
- Object
- Mongify::Mongoid::Model
- Defined in:
- lib/mongify/mongoid/model.rb,
lib/mongify/mongoid/model/field.rb,
lib/mongify/mongoid/model/relation.rb
Overview
Class that will be used to define a mongoid model
Defined Under Namespace
Constant Summary collapse
- CREATED_AT_FIELD =
default created at field name
'created_at'
- UPDATED_AT_FIELD =
default update at field name
'updated_at'
- EXCLUDED_FIELDS =
List of fields to exclude from the model
[ 'id', CREATED_AT_FIELD, UPDATED_AT_FIELD ]
Instance Attribute Summary collapse
-
#class_name ⇒ Object
(also: #name)
Returns the value of attribute class_name.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#polymorphic_as ⇒ Object
Returns the value of attribute polymorphic_as.
-
#relations ⇒ Object
Returns the value of attribute relations.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
-
#add_field(name, type, options = {}) ⇒ Object
Adds a field definition to the class, e.g: add_field(“field_name”, “String”, “name”).
-
#add_relation(relation_name, association, options = {}) ⇒ Relation
Adds a relationship definition to the class, e.g: add_relation(“embedded_in”, “users”).
-
#get_binding ⇒ Binding
Returns binding for ERB template.
-
#has_both_timestamps? ⇒ Boolean
Returns true both timestamps are present (created_at and updated_at).
-
#has_created_at_timestamp? ⇒ Boolean
Returns true created_at timestamp exists.
-
#has_timestamps? ⇒ Boolean
Returns true if it has any timestamps.
-
#has_updated_at_timestamp? ⇒ Boolean
Returns true updated_at timestamp exists.
-
#initialize(options = {}) ⇒ Model
constructor
A new instance of Model.
-
#polymorphic? ⇒ Boolean
Returns true it is a polymorphic model.
-
#to_s ⇒ String
Returns an improved inspection output including fields and relations.
Constructor Details
#initialize(options = {}) ⇒ Model
Returns a new instance of Model.
48 49 50 51 52 53 54 55 |
# File 'lib/mongify/mongoid/model.rb', line 48 def initialize( = {}) @class_name = [:class_name].to_s @table_name = [:table_name].to_s self.polymorphic_as = [:polymorphic_as] @fields = {} @relations = [] end |
Instance Attribute Details
#class_name ⇒ Object Also known as: name
Returns the value of attribute class_name.
21 22 23 |
# File 'lib/mongify/mongoid/model.rb', line 21 def class_name @class_name end |
#fields ⇒ Object
Returns the value of attribute fields.
21 22 23 |
# File 'lib/mongify/mongoid/model.rb', line 21 def fields @fields end |
#polymorphic_as ⇒ Object
Returns the value of attribute polymorphic_as.
21 22 23 |
# File 'lib/mongify/mongoid/model.rb', line 21 def polymorphic_as @polymorphic_as end |
#relations ⇒ Object
Returns the value of attribute relations.
21 22 23 |
# File 'lib/mongify/mongoid/model.rb', line 21 def relations @relations end |
#table_name ⇒ Object
Returns the value of attribute table_name.
21 22 23 |
# File 'lib/mongify/mongoid/model.rb', line 21 def table_name @table_name end |
Instance Method Details
#add_field(name, type, options = {}) ⇒ Object
Adds a field definition to the class, e.g:
add_field("field_name", "String", {rename_to: "name"})
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mongify/mongoid/model.rb', line 61 def add_field(name, type, ={}) .stringify_keys! return if ['ignore'] || ['references'] || polymorphic_field?(name) (name) return if EXCLUDED_FIELDS.include?(name.to_s.downcase) name = ['rename_to'] if ['rename_to'].present? type = ['as'] if ['as'].present? begin @fields[name.to_sym] = Field.new(name, type, ) rescue InvalidField => e raise InvalidField, "Unknown field type #{type} for #{name} in #{class_name}" end end |
#add_relation(relation_name, association, options = {}) ⇒ Relation
Embedded relations will overpower related relations
Adds a relationship definition to the class, e.g:
add_relation("embedded_in", "users")
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mongify/mongoid/model.rb', line 81 def add_relation(relation_name, association, ={}) if existing = find_relation_by(association) if relation_name =~ /^embed/ delete_relation_for association else return end end Relation.new(relation_name.to_s, association, ).tap do |r| @relations << r end end |
#get_binding ⇒ Binding
Returns binding for ERB template
96 97 98 |
# File 'lib/mongify/mongoid/model.rb', line 96 def get_binding return binding() end |
#has_both_timestamps? ⇒ Boolean
Returns true both timestamps are present (created_at and updated_at)
29 30 31 |
# File 'lib/mongify/mongoid/model.rb', line 29 def && end |
#has_created_at_timestamp? ⇒ Boolean
Returns true created_at timestamp exists
34 35 36 |
# File 'lib/mongify/mongoid/model.rb', line 34 def !!@created_at end |
#has_timestamps? ⇒ Boolean
Returns true if it has any timestamps
24 25 26 |
# File 'lib/mongify/mongoid/model.rb', line 24 def || end |
#has_updated_at_timestamp? ⇒ Boolean
Returns true updated_at timestamp exists
39 40 41 |
# File 'lib/mongify/mongoid/model.rb', line 39 def !!@updated_at end |
#polymorphic? ⇒ Boolean
Returns true it is a polymorphic model
44 45 46 |
# File 'lib/mongify/mongoid/model.rb', line 44 def polymorphic? !!@polymorphic_as end |
#to_s ⇒ String
Returns an improved inspection output including fields and relations
102 103 104 |
# File 'lib/mongify/mongoid/model.rb', line 102 def to_s "#<Mongify::Mongoid::Model::#{name} fields=#{@fields.keys} relations=#{@relations.map{|r| "#{r.name} :#{r.association}"}}>" end |