Class: SexyPgConstraints::Constrainer

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/sexy_pg_constraints/constrainers/constrainer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

execute, make_title

Constructor Details

#initialize(table, columns = []) ⇒ Constrainer

Returns a new instance of Constrainer.



7
8
9
10
# File 'lib/sexy_pg_constraints/constrainers/constrainer.rb', line 7

def initialize(table, columns = [])
  @table = table.to_s
  @columns = columns
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(column, constraints) ⇒ Object



12
13
14
# File 'lib/sexy_pg_constraints/constrainers/constrainer.rb', line 12

def method_missing(column, constraints)
  self.class.add_constraints(@table, column.to_s, constraints)
end

Class Method Details

.add_constraints(table, column, constraints) ⇒ Object



26
27
28
29
30
31
# File 'lib/sexy_pg_constraints/constrainers/constrainer.rb', line 26

def add_constraints(table, column, constraints)
  constraints.each_pair do |type, options|
    execute "alter table #{table} add constraint #{make_title(table, column, type)} " \
      + SexyPgConstraints::Constraints.send(type, column, options) + ';'
  end
end

Instance Method Details

#[](*columns) ⇒ Object



16
17
18
19
# File 'lib/sexy_pg_constraints/constrainers/constrainer.rb', line 16

def [](*columns)
  @columns = columns.map{|c| c.to_s}
  self
end

#all(constraints) ⇒ Object



21
22
23
# File 'lib/sexy_pg_constraints/constrainers/constrainer.rb', line 21

def all(constraints)
  self.class.add_constraints(@table, @columns, constraints)
end