Class: NoSE::Schema

Inherits:
Object show all
Defined in:
lib/nose/schema.rb

Overview

Simple DSL for constructing indexes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Schema

Returns a new instance of Schema.



8
9
10
11
# File 'lib/nose/schema.rb', line 8

def initialize(&block)
  @indexes = {}
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#indexesObject (readonly)

Returns the value of attribute indexes.



6
7
8
# File 'lib/nose/schema.rb', line 6

def indexes
  @indexes
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/nose/schema.rb', line 6

def model
  @model
end

Class Method Details

.load(name) ⇒ Object

Find the schema with the given name



14
15
16
17
18
# File 'lib/nose/schema.rb', line 14

def self.load(name)
  filename = File.expand_path "../../../schemas/#{name}.rb", __FILE__
  contents = File.read(filename)
  binding.eval contents, filename
end

Instance Method Details

#Index(key, &block) ⇒ void

This method returns an undefined value.

Wrap commands for defining index attributes



37
38
39
40
41
42
43
44
45
# File 'lib/nose/schema.rb', line 37

def Index(key, &block)
  # Apply the DSL
  dsl = IndexDSL.new(self)
  dsl.instance_eval(&block) if block_given?
  index = Index.new dsl.hash_fields, dsl.order_fields, dsl.extra,
                    QueryGraph::Graph.from_path(dsl.path_keys),
                    saved_key: key
  @indexes[index.key] = index
end

#Model(name) ⇒ void

This method returns an undefined value.

Set the model to be used by the schema



24
25
26
27
# File 'lib/nose/schema.rb', line 24

def Model(name)
  @model = Model.load name
  NoSE::DSL.mixin_fields @model.entities, IndexDSL
end

#SimpleIndex(entity) ⇒ void

This method returns an undefined value.

Add a simple index for an entity



31
32
33
# File 'lib/nose/schema.rb', line 31

def SimpleIndex(entity)
  @indexes[entity] = @model[entity].simple_index
end