Class: ContentType

Inherits:
SimpleStruct show all
Includes:
HasConnection
Defined in:
lib/bottlerocket/models/content_type.rb

Instance Attribute Summary

Attributes inherited from SimpleStruct

#attributes

Instance Method Summary collapse

Methods included from HasConnection

#db

Methods inherited from SimpleStruct

#initialize, #method_missing

Constructor Details

This class inherits a constructor from SimpleStruct

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class SimpleStruct

Instance Method Details

#collectionObject



18
19
20
# File 'lib/bottlerocket/models/content_type.rb', line 18

def collection
  db.collection name
end

#create(attrs) ⇒ Object



22
23
24
# File 'lib/bottlerocket/models/content_type.rb', line 22

def create(attrs)
  find_by_id collection.insert(attrs)
end

#field_namesObject



6
7
8
# File 'lib/bottlerocket/models/content_type.rb', line 6

def field_names
  fields.collect { |hash| hash.keys.first }
end

#find(*args) ⇒ Object Also known as: all



50
51
52
# File 'lib/bottlerocket/models/content_type.rb', line 50

def find(*args)
  collection.find(*args).collect { |attrs| Entity.new attrs.merge(:content_type => self) }
end

#find_by_id(id) ⇒ Object



36
37
38
# File 'lib/bottlerocket/models/content_type.rb', line 36

def find_by_id(id)
  find_one :_id => mongo_object_id(id)
end

#find_one(*args) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/bottlerocket/models/content_type.rb', line 40

def find_one(*args)
  result = collection.find_one(*args)
  result.merge!({:content_type => self}) if result.present?
  if result.nil? or result.empty?
    nil
  else
    Entity.new result
  end
end

#mongo_object_id(id) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/bottlerocket/models/content_type.rb', line 56

def mongo_object_id(id)
  if id.is_a? String
    BSON::ObjectId.from_string(id)
  else
    id
  end
end

#remove(attrs) ⇒ Object



32
33
34
# File 'lib/bottlerocket/models/content_type.rb', line 32

def remove(attrs)
  collection.remove(attrs)
end

#single_titleObject



14
15
16
# File 'lib/bottlerocket/models/content_type.rb', line 14

def single_title
  name.singularize.humanize
end

#titleObject



10
11
12
# File 'lib/bottlerocket/models/content_type.rb', line 10

def title
  name.humanize
end

#update(attrs) ⇒ Object



26
27
28
29
30
# File 'lib/bottlerocket/models/content_type.rb', line 26

def update(attrs)
  a = attrs.dup
  a.delete :content_type
  collection.update({:_id => mongo_object_id(a[:_id])}, a)
end