Class: MongoDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/models/mongo_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, collection, id = nil, modified_time = nil) ⇒ MongoDocument

Returns a new instance of MongoDocument.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/models/mongo_document.rb', line 6

def initialize doc, collection, id=nil, modified_time=nil
  @doc = doc
  @collection = collection
  
  if !(id == nil)
    @doc['_id'] = id
  end
      
  if !(modified_time==nil)
    @doc['_koda_last_modified']=modified_time
  end
end

Instance Attribute Details

#is_newObject

Returns the value of attribute is_new.



4
5
6
# File 'lib/models/mongo_document.rb', line 4

def is_new
  @is_new
end

Instance Method Details

#[](index) ⇒ Object



41
42
43
# File 'lib/models/mongo_document.rb', line 41

def [](index)
  @doc[index] 
end

#[]=(index, value) ⇒ Object



37
38
39
# File 'lib/models/mongo_document.rb', line 37

def []=(index, value)
  @doc[index] = value
end

#date_createdObject



68
69
70
71
# File 'lib/models/mongo_document.rb', line 68

def date_created
  return @doc['datecreated'] if(@doc['datecreated'])
  ''
end


89
90
91
92
93
94
95
# File 'lib/models/mongo_document.rb', line 89

def doc_link 
  if(@doc['_koda_doc_links'])
    @doc['_koda_doc_links'].to_s
  else
    ''
  end
end

#hiddenObject



81
82
83
84
85
86
87
# File 'lib/models/mongo_document.rb', line 81

def hidden 
  if(@doc['_koda_hidden_file'])
    true
  else
    false
  end
end

#idObject



25
26
27
# File 'lib/models/mongo_document.rb', line 25

def id
  @doc['_id']
end

#id=(value) ⇒ Object



29
30
31
# File 'lib/models/mongo_document.rb', line 29

def id=(value)
  @doc['_id'] = value
end

#last_modifiedObject



19
20
21
22
23
# File 'lib/models/mongo_document.rb', line 19

def last_modified
  if !(@doc['_koda_last_modified']==nil)
    Time.httpdate(@doc['_koda_last_modified'])
  end
end

#raw_documentObject



33
34
35
# File 'lib/models/mongo_document.rb', line 33

def raw_document
  @doc
end

#refObject



112
113
114
115
116
117
118
# File 'lib/models/mongo_document.rb', line 112

def ref
  if (@doc['alias'] == nil)
    @doc['_id'].to_s
  else
    @doc['alias'].to_s
  end
end

#ref=(value) ⇒ Object



108
109
110
# File 'lib/models/mongo_document.rb', line 108

def ref=(value)
    @doc['alias'] = value
end

#standardised_documentObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/models/mongo_document.rb', line 45

def standardised_document
  copy = @doc.clone
  copy['alias'] = ref
  copy.delete('_id')
  if(copy[:_id])
    copy.delete(:_id)
  end
  copy.delete('_koda_last_modified')
  copy
end

#stripped_documentObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/models/mongo_document.rb', line 97

def stripped_document
  copy = @doc.clone
  copy.delete('_id')
  copy.delete('_koda_last_modified')
  copy.delete '_koda_indexes'
  copy.delete '_koda_type'
  copy.delete '_koda_editor'
  copy.delete '_koda_doc_links'
  copy.to_obj
end

#titleObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/models/mongo_document.rb', line 56

def title
  if (@doc['name'])
    @doc['name']
  else
    if(@doc['_koda_title'])
      @doc['_koda_title']
    else
      ref
    end
  end
end

#typeObject



73
74
75
76
77
78
79
# File 'lib/models/mongo_document.rb', line 73

def type
  if(@doc['_koda_type'])
    @doc['_koda_type'].to_s
  else
    'anon'
  end
end

#urlObject



120
121
122
# File 'lib/models/mongo_document.rb', line 120

def url
  '/api/' + @collection + '/' + ref
end