Class: Undestroy::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/undestroy/config.rb

Defined Under Namespace

Classes: Field

Constant Summary collapse

OPTIONS =
[
  :table_name, :abstract_class, :fields, :migrate, :indexes, :prefix,
  :source_class, :target_class, :internals, :model_paths
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/undestroy/config.rb', line 8

def initialize(options={})
  self.indexes = false
  self.migrate = true
  self.prefix = "archive_"
  self.fields = {}
  self.model_paths = []
  self.internals = {
    :archive => Undestroy::Archive,
    :transfer => Undestroy::Transfer,
    :restore => Undestroy::Restore
  }

  add_field :deleted_at, :datetime do |instance|
    Time.now
  end

  # Default for Rails apps
  self.model_paths << Rails.root.join('app', 'models') if defined?(Rails)

  options.each do |key, value|
    self[key] = value.duplicable? ? value.dup : value
  end

  self.class.catalog << self
end

Class Method Details

.catalogObject



68
69
70
# File 'lib/undestroy/config.rb', line 68

def self.catalog
  @@catalog ||= []
end

.configObject



64
65
66
# File 'lib/undestroy/config.rb', line 64

def self.config
  @config ||= self.new
end

.configure {|config| ... } ⇒ Object

Yields:



60
61
62
# File 'lib/undestroy/config.rb', line 60

def self.configure
  yield(config) if block_given?
end

.reset_catalogObject



72
73
74
# File 'lib/undestroy/config.rb', line 72

def self.reset_catalog
  @@catalog = []
end

Instance Method Details

#[](key) ⇒ Object



34
35
36
# File 'lib/undestroy/config.rb', line 34

def [](key)
  self.send(key) if OPTIONS.include?(key)
end

#[]=(key, value) ⇒ Object



38
39
40
# File 'lib/undestroy/config.rb', line 38

def []=(key, value)
  self.send("#{key}=", value) if OPTIONS.include?(key)
end

#add_field(name, *args, &block) ⇒ Object



56
57
58
# File 'lib/undestroy/config.rb', line 56

def add_field(name, *args, &block)
  self.fields[name.to_sym] = Field.new(name, *args, &block)
end

#merge(object) ⇒ Object



46
47
48
# File 'lib/undestroy/config.rb', line 46

def merge(object)
  self.class.new(self.to_hash.merge(object.to_hash))
end

#primitive_fields(object) ⇒ Object



50
51
52
53
54
# File 'lib/undestroy/config.rb', line 50

def primitive_fields(object)
  self.fields.inject({}) do |hash, (key, field)|
    hash.merge(key => field.value(object))
  end
end

#to_hashObject



42
43
44
# File 'lib/undestroy/config.rb', line 42

def to_hash
  OPTIONS.inject({}) { |hash, key| hash.merge(key => self[key]) }
end