Class: Mongify::Mongoid::Model

Inherits:
Object
  • Object
show all
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

Classes: Field, Relation

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

Instance Method Summary collapse

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(options = {})
  @class_name = options[:class_name].to_s
  @table_name = options[:table_name].to_s
  self.polymorphic_as = options[:polymorphic_as]

  @fields = {}
  @relations = []
end

Instance Attribute Details

#class_nameObject 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

#fieldsObject

Returns the value of attribute fields.



21
22
23
# File 'lib/mongify/mongoid/model.rb', line 21

def fields
  @fields
end

#polymorphic_asObject

Returns the value of attribute polymorphic_as.



21
22
23
# File 'lib/mongify/mongoid/model.rb', line 21

def polymorphic_as
  @polymorphic_as
end

#relationsObject

Returns the value of attribute relations.



21
22
23
# File 'lib/mongify/mongoid/model.rb', line 21

def relations
  @relations
end

#table_nameObject

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, options={})
  options.stringify_keys!
  return if options['ignore'] || options['references'] || polymorphic_field?(name)
  check_for_timestamp(name)
  return if EXCLUDED_FIELDS.include?(name.to_s.downcase)
  name = options['rename_to'] if options['rename_to'].present?
  type = options['as'] if options['as'].present?
  begin
    @fields[name.to_sym] = Field.new(name, type, options)
  rescue InvalidField => e
    raise InvalidField, "Unknown field type #{type} for #{name} in #{class_name}"
  end
end

#add_relation(relation_name, association, options = {}) ⇒ Relation

Note:

Embedded relations will overpower related relations

Adds a relationship definition to the class, e.g:

add_relation("embedded_in", "users")

Returns:



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, options={})
  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, options).tap do |r|
    @relations << r
  end
end

#get_bindingBinding

Returns binding for ERB template

Returns:

  • (Binding)


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)

Returns:

  • (Boolean)


29
30
31
# File 'lib/mongify/mongoid/model.rb', line 29

def has_both_timestamps?
  has_created_at_timestamp? && has_updated_at_timestamp?
end

#has_created_at_timestamp?Boolean

Returns true created_at timestamp exists

Returns:

  • (Boolean)


34
35
36
# File 'lib/mongify/mongoid/model.rb', line 34

def has_created_at_timestamp?
  !!@created_at
end

#has_timestamps?Boolean

Returns true if it has any timestamps

Returns:

  • (Boolean)


24
25
26
# File 'lib/mongify/mongoid/model.rb', line 24

def has_timestamps?
  has_created_at_timestamp? || has_updated_at_timestamp?
end

#has_updated_at_timestamp?Boolean

Returns true updated_at timestamp exists

Returns:

  • (Boolean)


39
40
41
# File 'lib/mongify/mongoid/model.rb', line 39

def has_updated_at_timestamp?
  !!@updated_at
end

#polymorphic?Boolean

Returns true it is a polymorphic model

Returns:

  • (Boolean)


44
45
46
# File 'lib/mongify/mongoid/model.rb', line 44

def polymorphic?
  !!@polymorphic_as
end

#to_sString

Returns an improved inspection output including fields and relations

Returns:

  • (String)

    String representation of the model



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