Class: StrokeDB::Document::Metas

Inherits:
Array show all
Defined in:
lib/document/document.rb

Overview

Collection of meta documents

Constant Summary

Constants inherited from Array

Array::SDATPTAGS

Instance Method Summary collapse

Methods inherited from Array

#stroke_diff, #stroke_merge, #stroke_patch

Constructor Details

#initialize(document) ⇒ Metas

:nodoc:



82
83
84
85
86
# File 'lib/document/document.rb', line 82

def initialize(document)
  @document = document
  _meta = document[:meta]
  concat [_meta].flatten.compact.map{|v| v.is_a?(DocumentReferenceValue) ? v.load : v}
end

Instance Method Details

#<<(meta) ⇒ Object



88
89
90
# File 'lib/document/document.rb', line 88

def <<(meta)
  add_meta(meta, :call_initialization_callbacks => true)
end

#add_meta(meta, opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/document/document.rb', line 92

def add_meta(meta, opts = {})
  opts = opts.stringify_keys
  _module = nil

  # meta can be specified both as a meta document and as a module
  case meta
  when Document
    push meta
    _module = StrokeDB::Document.collect_meta_modules(@document.store, meta).first
  when Meta
    push meta.document(@document.store)
    _module = meta
  else
    raise ArgumentError, "Meta should be either document or meta module"
  end

  # register meta in the document
  @document[:meta] = self

  if _module
    @document.extend(_module)
    
    _module.send!(:setup_callbacks, @document) rescue nil
    
    if opts['call_initialization_callbacks'] 
      @document.send!(:execute_callbacks_for, _module, :on_initialization)
      @document.send!(:execute_callbacks_for, _module, :on_new_document) if @document.new?
    end
  end
end