Class: PgTrigger::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_trigger/plan.rb

Overview

This class represents the actions needed to go from the existing triggers to the expected triggers defined in the models.

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlan

Returns a new instance of Plan.



41
42
43
44
45
46
# File 'lib/pg_trigger/plan.rb', line 41

def initialize
  @name = nil
  @type = nil
  @table = nil
  @actions = Hash.new { |h, k| h[k] = [] }.tap(&:compare_by_identity)
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



39
40
41
# File 'lib/pg_trigger/plan.rb', line 39

def table
  @table
end

#typeObject (readonly)

Returns the value of attribute type.



39
40
41
# File 'lib/pg_trigger/plan.rb', line 39

def type
  @type
end

Instance Method Details

#add_trigger(t) ⇒ Object



70
71
72
73
74
75
# File 'lib/pg_trigger/plan.rb', line 70

def add_trigger(t)
  set_type :create
  set_table t.table

  @actions[:to_add] << t
end

#drop_trigger(t) ⇒ Object



77
78
79
80
81
82
# File 'lib/pg_trigger/plan.rb', line 77

def drop_trigger(t)
  set_type :drop
  set_table t.table

  @actions[:to_remove] << t
end

#empty?Boolean

Returns:

  • (Boolean)


52
# File 'lib/pg_trigger/plan.rb', line 52

def empty? = @actions.empty?

#nameObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pg_trigger/plan.rb', line 54

def name
  @name ||= begin
    action = case type
    when :create then "create"
    when :drop then "drop"
    when :update then "update"
    else
      "change"
    end

    table_name = table == "multiple" ? "multiple_tables" : table

    "#{action}_triggers_on_#{table_name}"
  end
end

#new_triggersObject



48
# File 'lib/pg_trigger/plan.rb', line 48

def new_triggers = @actions[:to_add]

#removed_triggersObject



50
# File 'lib/pg_trigger/plan.rb', line 50

def removed_triggers = @actions[:to_remove]

#update_trigger(old_t, new_t) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/pg_trigger/plan.rb', line 84

def update_trigger(old_t, new_t)
  set_type :update
  set_table new_t.table

  @actions[:to_remove] << old_t
  @actions[:to_add] << new_t
end