Module: ViewModel

Defined in:
lib/view_model.rb,
lib/adapters/abstract_adapter.rb,
lib/adapters/postgresql_adapter.rb

Defined Under Namespace

Classes: AbstractAdapter, PostgreSQLAdapter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



9
10
11
# File 'lib/view_model.rb', line 9

def self.included(model)   
  model.extend self 
end

Instance Method Details

#check_dependenciesObject

checks for dependent views



34
35
36
37
38
39
40
41
# File 'lib/view_model.rb', line 34

def check_dependencies
  return if @view_dependencies == nil
  @view_dependencies.each do |dep|
    if dep.view_definition_set? == false
      raise "Cannot migrate: #{dep} does not have a view_definition set."
    end
  end 
end

#create_dependencies!Object

create all dependent views



52
53
54
55
56
57
# File 'lib/view_model.rb', line 52

def create_dependencies!
  return if @view_dependencies == nil
  @view_dependencies.each do |dep|
    dep.create_view!
  end 
end

#create_view!Object

Create the model’s view according to it’s definition.



80
81
82
83
84
85
# File 'lib/view_model.rb', line 80

def create_view!
  puts "creating view #{table_name}"
  sql = "CREATE OR REPLACE VIEW #{table_name} AS "
  sql << @view_definition
  execute(sql)
end

#drop_dependencies!Object

drops all dependent views



44
45
46
47
48
49
# File 'lib/view_model.rb', line 44

def drop_dependencies!
  return if @view_dependencies == nil
  @view_dependencies.each do |dep|
    dep.drop_view!
  end 
end

#drop_view!Object

Drops a view if exists. note: “if exists” works only on Postgresql 8.2 or later. Older versions are not supported.



89
90
91
92
# File 'lib/view_model.rb', line 89

def drop_view!
  puts "dropping view #{table_name}"
  execute("DROP VIEW if exists #{table_name}")
end

#execute(sql) ⇒ Object

shortcut for connection.execute(sql)



19
20
21
# File 'lib/view_model.rb', line 19

def execute(sql)
  connection.execute(sql)
end

#get_adapterObject

gets the adapter needed which has a custom method to get its dependencies



24
25
26
27
28
29
30
31
# File 'lib/view_model.rb', line 24

def get_adapter
  adapter = connection.class.to_s.demodulize
  begin
   return ("ViewModel::"+adapter).constantize
  rescue
    raise "ViewModel: can't find adapter for ConnectionAdapter #{adapter}"
  end
end

#get_dependenciesObject

checks Postgresql relations for any view that uses current view.



64
65
66
# File 'lib/view_model.rb', line 64

def get_dependencies
  @view_dependencies = get_adapter.get_dependencies(connection, table_name)
end

#migrate!Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/view_model.rb', line 68

def migrate!
  raise "Please provide a view_definition inside the model" if !view_definition_set?
  get_dependencies
  check_dependencies
  drop_dependencies!
  drop_view!
  create_view!
  create_dependencies!
  true
end

#view_definition(sql) ⇒ Object

sets the view_definition in the model



14
15
16
# File 'lib/view_model.rb', line 14

def view_definition(sql)
  @view_definition = sql
end

#view_definition_set?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/view_model.rb', line 59

def view_definition_set?
  @view_definition != nil
end