Class: Undestroyable::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/undestroyable/configuration.rb

Overview

Specifies which strategy to use for this configuration.

Possible options include:

[:startegy]
  [:none]
    Deletes record, as with usual destroy method. This option may be used as a cancelation
    of undestoyable state of record, for STI model as an example.
  [:column]
    Updates <tt>:deleted_at</tt> column of the record. This is default strategy,
    in cafe if undestroyable is specified for model.
  [:table]
    Move record into separate table.
  [:database]
    Move table into separate database.
  [:dump]
    Marshal dump all deleted records into common table table.
[:table_name]
  Keep alternative to table name. By default it will equivalent to one used by Model backend.
  It is ignored by <tt>:none</tt> and <tt>::column</tt> strategies.
[:table_prefix]
  Keep table prefix. If it is set not nil, it will be ignored for table creation. Default is nil.
  It is ignored by <tt>:none</tt> and <tt>::column</tt> strategies.
[:table_suffix]
  Keep table suffix. If it is set not nil, it will be ignored for table creation. Default is nil.
  It is ignored by <tt>:none</tt> and <tt>::column</tt> strategies.
[:full_table_name]
  Keep full table name. If this field is not specified it will be generated from 
  table_name + table_prefix + table_suffix.
  It is ignored by <tt>:none</tt> and <tt>::column</tt> strategies.
[:connection]
  Keep credetial information for remote database connection.
  *NB!* Compulsary for :database strategy, ignored for rest strategies.

EXAMPLES

 class ConceptCat < ActiveRecord::Base
   undstroyable do
     startegy :table
     table_name :metalic
     table_suffix :scrap
   end
 end

 class ConceptCat < ActiveRecord::Base
   undstroyable do
     startegy :database
     connection {}
     table_name :scrap
   end
 end

class ConceptCat < ActiveRecord::Base
  undstroyable do
    startegy :dump
    table_name :scrap
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ Configuration

Returns a new instance of Configuration.



68
69
70
71
72
73
# File 'lib/undestroyable/configuration.rb', line 68

def initialize(opts = {},&block)
  copy_system = opts[:copy_system].nil? ? true : opts.delete(:copy_system)
  @configuration ||= (copy_system ? Undestroyable.config.configuration.dup : {})
  @configuration[:strategy] ||= :column
  prepare &block if block_given?
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



66
67
68
# File 'lib/undestroyable/configuration.rb', line 66

def configuration
  @configuration
end

Instance Method Details

#[](option_name) ⇒ Object

Allows to access configuration values via provided keys. Example:

c = Undestroyable.config
c[:strategy] # => :column


94
95
96
97
98
# File 'lib/undestroyable/configuration.rb', line 94

def [](option_name)
  # :full_table_name will be generated on first request
  generate_full_name if option_name == :full_table_name && !configuration[:full_table_name]
  configuration[option_name]
end

#connection(alternative_connection) ⇒ Object



122
123
124
# File 'lib/undestroyable/configuration.rb', line 122

def connection(alternative_connection)
  @configuration[:connection] = alternative_connection
end

#connection_settingsObject



126
127
128
# File 'lib/undestroyable/configuration.rb', line 126

def connection_settings
  @configuration[:connection]
end

#full_table_name(alternative_full_table_name) ⇒ Object



118
119
120
# File 'lib/undestroyable/configuration.rb', line 118

def full_table_name(alternative_full_table_name)
  @configuration[:full_table_name] = alternative_full_table_name
end

#prepare(&block) ⇒ Object Also known as: setup



75
76
77
78
# File 'lib/undestroyable/configuration.rb', line 75

def prepare &block
  instance_eval &block
  raise(::Undestroyable::ConnectionIsCompulsaryError.new "Connection information is compulsary") if database? && connection_settings.blank?
end

#strategy(shrtkey) ⇒ Object

Raises:



100
101
102
103
104
# File 'lib/undestroyable/configuration.rb', line 100

def strategy(shrtkey)
  raise WrongStrategyError.new(%Q{Such strategy "#{shrtkey}" does not exists.}) unless ::Undestroyable::STRATEGIES.include? shrtkey.to_sym
  table_name('dump') if shrtkey.to_sym == :dump && !@configuration[:table_name]
  @configuration[:strategy] = shrtkey.to_sym
end

#table_name(alternative_table_name) ⇒ Object



106
107
108
# File 'lib/undestroyable/configuration.rb', line 106

def table_name(alternative_table_name)
  @configuration[:table_name] = alternative_table_name
end

#table_prefix(alternative_table_prefix) ⇒ Object



110
111
112
# File 'lib/undestroyable/configuration.rb', line 110

def table_prefix(alternative_table_prefix)
  @configuration[:table_prefix] = alternative_table_prefix
end

#table_suffix(alternative_table_suffix) ⇒ Object



114
115
116
# File 'lib/undestroyable/configuration.rb', line 114

def table_suffix(alternative_table_suffix)
  @configuration[:table_suffix] = alternative_table_suffix
end