Class: RailsOnPg::Views::ViewDefinition
- Inherits:
-
Object
- Object
- RailsOnPg::Views::ViewDefinition
- Defined in:
- lib/rails_on_pg/views.rb
Overview
View definition, see create_view dsl
Instance Method Summary collapse
- #conditions(cond) ⇒ Object
- #from(*tables) ⇒ Object
-
#initialize(name, &block) ⇒ ViewDefinition
constructor
A new instance of ViewDefinition.
- #join(value) ⇒ Object
- #select(*columns) ⇒ Object
- #to_sql ⇒ Object
Constructor Details
#initialize(name, &block) ⇒ ViewDefinition
Returns a new instance of ViewDefinition.
86 87 88 89 90 |
# File 'lib/rails_on_pg/views.rb', line 86 def initialize name, &block @joins = [] @name = name instance_eval &block end |
Instance Method Details
#conditions(cond) ⇒ Object
101 102 103 104 105 |
# File 'lib/rails_on_pg/views.rb', line 101 def conditions cond @where = cond.collect{ |attrib, value| "#{attrib} = #{value}"}.join(" AND ") if cond.is_a?(Hash) @where = cond if cond.is_a?(String) end |
#from(*tables) ⇒ Object
95 96 97 |
# File 'lib/rails_on_pg/views.rb', line 95 def from *tables @from = tables.join(',') end |
#join(value) ⇒ Object
98 99 100 |
# File 'lib/rails_on_pg/views.rb', line 98 def join value @joins << value end |
#select(*columns) ⇒ Object
92 93 94 |
# File 'lib/rails_on_pg/views.rb', line 92 def select *columns @select = columns.join(',') end |
#to_sql ⇒ Object
107 108 109 110 |
# File 'lib/rails_on_pg/views.rb', line 107 def to_sql @where ||= '1=1' "CREATE VIEW #{@name} AS SELECT #{@select} FROM #{@from} #{@joins.join(' ')} WHERE #{@where};" end |