Class: PgGnostic::ViewDefinition
- Inherits:
-
Object
- Object
- PgGnostic::ViewDefinition
- Defined in:
- lib/pg_gnostic/view_definition.rb
Class Attribute Summary collapse
-
.views ⇒ Object
readonly
Returns the value of attribute views.
Class Method Summary collapse
- .clear_declarations ⇒ Object
- .create_view(name, opts = {}) ⇒ Object
- .create_view_sql(vname, sql) ⇒ Object
- .delete(name) ⇒ Object
- .delete_all ⇒ Object
- .execute(view, stack) ⇒ Object
- .execute_sql(view) ⇒ Object
- .load_declarations(path) ⇒ Object
- .named_fields(*args) ⇒ Object
- .predifined_fields ⇒ Object
- .reset_created_flag ⇒ Object
- .to_arr(val) ⇒ Object
- .update ⇒ Object
Class Attribute Details
.views ⇒ Object (readonly)
Returns the value of attribute views.
49 50 51 |
# File 'lib/pg_gnostic/view_definition.rb', line 49 def views @views end |
Class Method Details
.clear_declarations ⇒ Object
66 67 68 |
# File 'lib/pg_gnostic/view_definition.rb', line 66 def clear_declarations @views={} end |
.create_view(name, opts = {}) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/pg_gnostic/view_definition.rb', line 70 def create_view(name,opts={}) raise "View with name #{name} already registered" if views.key? name opts[:depends_on] = to_arr opts[:depends_on] if opts.key? :depends_on views[name] = opts.merge(:name=>name,:sql=>opts[:sql]) end |
.create_view_sql(vname, sql) ⇒ Object
94 95 96 97 98 |
# File 'lib/pg_gnostic/view_definition.rb', line 94 def create_view_sql(vname,sql) <<-SQL CREATE VIEW #{vname} AS #{sql}; SQL end |
.delete(name) ⇒ Object
110 111 112 113 |
# File 'lib/pg_gnostic/view_definition.rb', line 110 def delete(name) puts " * Drop view #{name}" ActiveRecord::Base.connection.execute "DROP VIEW IF EXISTS #{name} CASCADE;" end |
.delete_all ⇒ Object
104 105 106 107 108 |
# File 'lib/pg_gnostic/view_definition.rb', line 104 def delete_all views.each do |name,view| delete name end end |
.execute(view, stack) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/pg_gnostic/view_definition.rb', line 123 def execute(view,stack) if stack.include?(view) stack<< view raise "ERROR: Recursion in views dependencies \n #{stack.to_yaml}" end return if view[:created] if view.key? :depends_on view[:depends_on].each do |dep| if views.key?(dep) stack<< view execute views[dep], stack else raise "ERROR: Could not find dependency #{dep}" end end end execute_sql(view) view[:created] = true end |
.execute_sql(view) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/pg_gnostic/view_definition.rb', line 144 def execute_sql(view) name = view[:name] ActiveRecord::Base.connection.reconnect! template = ERB.new view[:sql] t = Tables.new t.instance_eval do @sql = template.result(binding) end sql = create_view_sql(view[:name],t.sql) puts " * Create view #{name}" ActiveRecord::Base.transaction do ActiveRecord::Base.connection().execute sql; end rescue Exception=>e puts "ERROR: While creating #{name} #{e}" raise e end |
.load_declarations(path) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/pg_gnostic/view_definition.rb', line 54 def load_declarations(path) Dir["#{path}/*.rb"].each do |f| load f end Dir["#{path}/*.sql"].each do |f| lines = [IO.readlines(f, '')].flatten sql = lines.join view_name = File.basename(f,".sql").to_sym create_view(view_name,:sql=>sql) end end |
.named_fields(*args) ⇒ Object
81 82 83 84 |
# File 'lib/pg_gnostic/view_definition.rb', line 81 def named_fields(*args) key = args.shift predifined_fields[key]=args.to_a end |
.predifined_fields ⇒ Object
77 78 79 |
# File 'lib/pg_gnostic/view_definition.rb', line 77 def predifined_fields @predifined_fields ||={} end |
.reset_created_flag ⇒ Object
100 101 102 |
# File 'lib/pg_gnostic/view_definition.rb', line 100 def reset_created_flag views.values.each {|v| v[:created] = false} end |
.to_arr(val) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/pg_gnostic/view_definition.rb', line 86 def to_arr(val) if val && !val.is_a?(Array) [ val ] else val end end |
.update ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/pg_gnostic/view_definition.rb', line 115 def update delete_all reset_created_flag views.values.each do |view| execute(view,[]) end end |