Class: Viewmatic::Schema

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

Overview

Represents a schema of views to be built.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Schema

Initialize a new schema. If you pass a block it will be eval’d inside the instance.



38
39
40
41
42
# File 'lib/viewmatic/schema.rb', line 38

def initialize(&block)
  @views = {}
  @conn_proc = -> { ActiveRecord::Base.connection }
  instance_exec(&block) if block
end

Class Attribute Details

.pathsArray<String> (readonly)

Returns Array of globs matching view definition files. default: [‘db/views.rb’, ‘db/views/*.rb’].

Returns:

  • (Array<String>)

    Array of globs matching view definition files. default: [‘db/views.rb’, ‘db/views/*.rb’]



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

def paths
  @paths
end

Instance Attribute Details

#viewsArray<Viewmatic::View] all defined views in this schema (readonly)

Returns Array<Viewmatic::View] all defined views in this schema.

Returns:

  • (Array<Viewmatic::View] all defined views in this schema)

    Array<Viewmatic::View] all defined views in this schema



33
34
35
# File 'lib/viewmatic/schema.rb', line 33

def views
  @views
end

Class Method Details

.define(&block) ⇒ Viewmatic::Schema

Define a new schema. If you pass a block, it will be eval’d inside the Schema instance.

Returns:



26
27
28
29
30
# File 'lib/viewmatic/schema.rb', line 26

def self.define(&block)
  schema = new(&block)
  Viewmatic.schemas << schema
  schema
end

Instance Method Details

#connection(&block) ⇒ Object

Override the default connection to use.



58
59
60
61
# File 'lib/viewmatic/schema.rb', line 58

def connection(&block)
  @conn_proc = block if block
  @conn_proc
end

#drop!Object

Drop all views defined in this schema.



76
77
78
79
80
81
# File 'lib/viewmatic/schema.rb', line 76

def drop!
  conn = @conn_proc.call
  views.each do |_name, view|
    conn.execute SchemaStatements.drop_view view
  end
end

#load!Object

Create all views defined in this schema.



66
67
68
69
70
71
# File 'lib/viewmatic/schema.rb', line 66

def load!
  conn = @conn_proc.call
  views.each do |_name, view|
    conn.execute SchemaStatements.create_view view
  end
end

#view(name) {|view| ... } ⇒ Object

Define a new view.

Parameters:

  • name (Symbol)

    what to call the view

Yields:



49
50
51
52
53
# File 'lib/viewmatic/schema.rb', line 49

def view(name)
  view = views[name] = View.new(name)
  yield view if block_given?
  view
end