Module: StrokeDB::Meta

Defined in:
lib/strokedb/document/meta.rb,
lib/strokedb/document/util.rb

Overview

Meta is basically a type. Imagine the following document:

some_apple:

weight: 3oz
color: green
price: $3

Each apple is a fruit and a product in this case (because it has price).

we can express it by assigning metas to document like this:

some_apple:

meta: [Fruit, Product]
weight: 3oz
color: green
price: $3

In document slots metas store references to metadocument.

Document class will be extended by modules Fruit and Product.

Defined Under Namespace

Modules: Util

Constant Summary collapse

CALLBACKS =
%w(on_initialization 
on_load 
before_save 
after_save 
when_slot_not_found 
on_new_document 
on_validation 
after_validation 
on_set_slot)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.document(store = nil) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/strokedb/document/meta.rb', line 74

def document(store=nil)
  raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store
  unless meta_doc = store.find(uuid)
    meta_doc = Document.create!(store, :name => Meta.name.demodulize, :uuid => uuid, :nsurl => StrokeDB.nsurl)
  end
  meta_doc
end

.make_uuid(nsurl, name) ⇒ Object



37
38
39
# File 'lib/strokedb/document/meta.rb', line 37

def make_uuid(nsurl, name)
  ::StrokeDB::Util.sha1_uuid("meta:#{nsurl}##{name}")
end

.make_uuid_from_fullname(full_name) ⇒ Object



33
34
35
# File 'lib/strokedb/document/meta.rb', line 33

def make_uuid_from_fullname(full_name)
  ::StrokeDB::Util.sha1_uuid(full_name)
end

.new(*args, &block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/strokedb/document/meta.rb', line 41

def new(*args, &block)
  mod = Module.new
  args = args.unshift(nil) if args.empty? || args.first.is_a?(Hash)
  args << {} unless args.last.is_a?(Hash)
  mod.module_eval do
    @args = args
    @meta_initialization_procs = []
    @metas = [self]
    extend Meta
    extend Associations
    extend Validations
    extend Coercions
    extend Virtualizations
    extend Util
  end
  mod.module_eval(&block) if block_given?
  mod.module_eval do
    initialize_associations
    initialize_validations
    initialize_coercions
    initialize_virtualizations
  end
  if name = args.last.stringify_keys['name']
    META_CACHE[make_uuid(args.last.stringify_keys['nsurl'],args.last.stringify_keys['name'])] = mod 
    mod.instance_eval %{
      def name 
        '#{name}'
      end
    }
  end
  mod
end

.resolve_uuid_name(nsurl, name) ⇒ Object



29
30
31
# File 'lib/strokedb/document/meta.rb', line 29

def resolve_uuid_name(nsurl,name)
  "meta:#{nsurl}##{name}"
end

Instance Method Details

#+(meta) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/strokedb/document/meta.rb', line 100

def +(meta)
  if is_a?(Module) && meta.is_a?(Module)
    new_meta = Module.new
    instance_variables.each do |iv|
      new_meta.instance_variable_set(iv, instance_variable_get(iv) ? instance_variable_get(iv).clone : nil)
    end
    new_meta.instance_variable_set(:@metas, @metas.clone)
    new_meta.instance_variable_get(:@metas) << meta
    new_meta.module_eval do
      extend Meta
    end
    new_meta_name = new_meta.instance_variable_get(:@metas).map{|m| m.name.demodulize}.join('__')
    mod = self.name.modulize.constantize rescue Object
    mod.send(:remove_const, new_meta_name) rescue nil
    mod.const_set(new_meta_name, new_meta)
    new_meta
  elsif is_a?(Document) && meta.is_a?(Document)
    (Document.new(store, self.to_raw.except('uuid','version','previous_version'), true) +
    Document.new(store, meta.to_raw.except('uuid','version','previous_version'), true)).extend(Meta).make_immutable!
  else
    raise "Can't + #{self.class} and #{meta.class}"
  end
end

#create!(*args, &block) ⇒ Object



164
165
166
# File 'lib/strokedb/document/meta.rb', line 164

def create!(*args, &block)
  new(*args, &block).save!
end

#document(store = nil) ⇒ Object



248
249
250
251
252
253
254
# File 'lib/strokedb/document/meta.rb', line 248

def document(store=nil)
  metadocs = @metas.map do |m|
    @args = m.instance_variable_get(:@args)
    make_document(store)
  end
  metadocs.size > 1 ? metadocs.inject { |a, b| a + b }.make_immutable! : metadocs.first
end

#extended(obj) ⇒ Object



258
259
260
# File 'lib/strokedb/document/meta.rb', line 258

def extended(obj)
  setup_callbacks(obj) if obj.is_a?(Document)
end

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

Finds all documents matching given parameters. The simplest form of find call is without any parameters. This returns all documents belonging to the meta as an array.

User = Meta.new
all_users = User.find

Another form is to find a document by its UUID:

specific_user = User.find("1e3d02cc-0769-4bd8-9113-e033b246b013")

If the UUID is not found, nil is returned.

Most prominent search uses slot values as criteria:

short_fat_joes = User.find(:name => "joe", :weight => 110, :height => 167)

All matching documents are returned as an array.

In all described cases the default store is used. You may also specify another store as the first argument:

all_my_users = User.find(my_store)
all_my_joes  = User.find(my_store, :name => "joe")
oh_my        = User.find(my_store, "1e3d02cc-0769-4bd8-9113-e033b246b013")


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/strokedb/document/meta.rb', line 195

def find(*args, &block)
  if args.empty? || !args.first.respond_to?(:search)
    raise NoDefaultStoreError unless StrokeDB.default_store
    
    args = args.unshift(StrokeDB.default_store) 
  end

  unless args.size == 1 || args.size == 2
    raise ArgumentError, "Invalid arguments for find"
  end

  store = args[0]
  opt = { :meta => @metas.map {|m| m.document(store)} }

  case args[1]
  when String
    raise ArgumentError, "Invalid UUID" unless args[1].match(UUID_RE)
    store.find(args[1], &block)
  when Hash
    store.search opt.merge(args[1])
  when nil
    store.search opt
  else
    raise ArgumentError, "Invalid search criteria for find"
  end
end

#find_or_create(*args, &block) ⇒ Object

Similar to find, but creates a document with an appropriate slot values if document was not found.

If found, returned is only the first result.



233
234
235
236
# File 'lib/strokedb/document/meta.rb', line 233

def find_or_create(*args, &block)
  result = find(*args)
  result.empty? ? create!(*args, &block) : result.first
end

#implements(another_meta) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/strokedb/document/meta.rb', line 91

def implements(another_meta)
  values = @args.find{|a| a.is_a?(Hash) }
  values.merge!(another_meta.document.to_raw.delete_if {|k,v| ['name','uuid','version','previous_version','meta'].member?(k) })
  values[:implements_metas] ||= []
  values[:implements_metas] << another_meta.document
  include(another_meta)
  self
end

#inspectObject Also known as: to_s



238
239
240
241
242
243
244
# File 'lib/strokedb/document/meta.rb', line 238

def inspect
  if is_a?(Module)
    name
  else
    pretty_print
  end
end

#named(*args, &block) ⇒ Object

Raises:

  • (ArgumentError)


124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/strokedb/document/meta.rb', line 124

def named(*args,&block)
  args.unshift StrokeDB.default_store unless args.first.is_a?(StrokeDB::Store)
  args << {} unless args.last.is_a?(Hash)
  raise ArgumentError, "you should specify name" unless args[1].is_a?(String)
  name = args[1]
  uuid = ::StrokeDB::Util.sha1_uuid("#{document.uuid}:#{name}")
  unless doc = find(args[0],uuid,&block)
    doc = create!(args[0],args.last.reverse_merge(:uuid => uuid),&block)
  else
    doc.update_slots!(args.last)
  end
  doc
end

#new(*args, &block) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/strokedb/document/meta.rb', line 156

def new(*args, &block)
  args = args.clone
  args << {} unless args.last.is_a?(Hash)
  args.last[:meta] = @metas
  doc = Document.new(*args, &block)
  doc
end