Module: Persistence::Port::Controller

Included in:
Persistence
Defined in:
lib/persistence/port/controller.rb

Overview

Controller methods for Persistence singleton.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(instance) ⇒ Object

Initializes singleton when extended.



14
15
16
17
18
19
20
21
# File 'lib/persistence/port/controller.rb', line 14

def self.extended( instance )
  
  instance.module_eval do
    @ports = { }
    @pending_buckets = { }
  end
  
end

Instance Method Details

#create_pending_buckets(port) ⇒ Object

Creates pending buckets when port is enabled.

Parameters:

  • Port to create pending buckets with.

Returns:

  • self



226
227
228
229
230
231
232
233
234
235
# File 'lib/persistence/port/controller.rb', line 226

def create_pending_buckets( port )
  
  @pending_buckets.delete_if do |this_class, this_bucket|
    this_bucket.initialize_for_port( port )
    true
  end

  return self
  
end

#current_portPersistence::Port?

Get current port

Returns:

  • Current port.



145
146
147
# File 'lib/persistence/port/controller.rb', line 145

def current_port
  return @current_port
end

#disable_port(port_name) ⇒ Object

Disable port.

Parameters:

  • Port name to disable.

Returns:

  • self



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/persistence/port/controller.rb', line 120

def disable_port( port_name )
  
  if port_instance = port( port_name )
  
    if current_port == port_instance
      set_current_port( nil )
    end
  
    port_instance.disable

  end
  
  return self

end

#enable_port(port_name, adapter_instance = nil) ⇒ Persistence::Port

Enable a port. If no port is already enabled, port will be set as current port.

Parameters:

  • Name of port to create or enable.

  • (defaults to: nil)

    Adapter instance to use to create port. If not provided attempt will be made to enable existing port by name.

Returns:

  • Port instance.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/persistence/port/controller.rb', line 68

def enable_port( port_name, adapter_instance = nil )

  port_instance = nil

  if adapter_instance

    port_instance = ::Persistence::Port.new( port_name, adapter_instance )
    
    prior_port_by_name = @ports[ port_name.to_sym ]
    
    @ports[ port_name.to_sym ] = port_instance

    port_instance.enable
    
    if prior_port_by_name
      buckets_around_from_prior_port = prior_port_by_name.buckets
      buckets_around_from_prior_port.each do |this_bucket_name, this_bucket|
        port_instance.initialize_persistence_bucket_from_instance( this_bucket )
      end
    end
    
  else

    unless port_instance = port( port_name )
      raise 'Port must first be enabled with adapter instance before it can be re-enabled by name.'
    end

    port_instance.enable

  end

  unless current_port
    set_current_port( port_instance )
  end
  
  create_pending_buckets( port_instance )

  return port_instance

end

#pending_bucket(klass, bucket_name) ⇒ Persistence::Port::Bucket

Create pending bucket to be enabled with port is enabled.

Parameters:

  • Class bucket is being created for.

  • Name to use for bucket.

Returns:



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/persistence/port/controller.rb', line 251

def pending_bucket( klass, bucket_name )

  bucket_instance = nil

  unless bucket_instance = @pending_buckets[ klass ] and bucket_instance.name == bucket_name
    @pending_buckets[ klass ] = bucket_instance = ::Persistence::Port::Bucket.new( nil, bucket_name )
  end
  
  return bucket_instance
  
end

#pending_bucketsHash{Symbol,String=>Persistence::Port::Bucket}

Tracks pending persistence buckets created before a port is enabled.

Returns:



48
49
50
51
52
# File 'lib/persistence/port/controller.rb', line 48

def pending_buckets

  return @pending_buckets

end

#port(port_name) ⇒ Persistence::Port

Get port for name.

Parameters:

  • Port name.

Returns:

  • Port instance.



176
177
178
# File 'lib/persistence/port/controller.rb', line 176

def port( port_name )
  return @ports[ port_name.to_sym ]
end

#port_for_name_or_port(persistence_port_or_name, ensure_exists = false) ⇒ Persistence::Port

Get port for name.

Parameters:

  • Port name or instance.

  • (defaults to: false)

    Whether exception should be thrown is port does not exist.

Returns:

  • Port instance.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/persistence/port/controller.rb', line 192

def port_for_name_or_port( persistence_port_or_name, ensure_exists = false )
  
  port_instance = nil
  
  case persistence_port_or_name
    when ::Symbol, ::String
      port_instance = port( persistence_port_or_name )
    else
      port_instance = persistence_port_or_name
  end
  
  unless port_instance
    if ensure_exists
      raise ::ArgumentError, 'No port found by name ' << persistence_port_or_name.to_s
    end
  end
  
  return port_instance
  
end

#portsHash{Symbol,String=>Persistence::Port}

Tracks persistence ports.

Returns:

  • Hash of ports.



31
32
33
34
35
# File 'lib/persistence/port/controller.rb', line 31

def ports
  
  return @ports

end

#set_current_port(persistence_port_or_name) ⇒ Object

Set current port

Parameters:

  • Port instance or name to set current port to.

Returns:

  • self



160
161
162
163
# File 'lib/persistence/port/controller.rb', line 160

def set_current_port( persistence_port_or_name )
  @current_port = port_for_name_or_port( persistence_port_or_name )
  return self
end