Class: Undestroy::Config
- Inherits:
-
Object
- Object
- Undestroy::Config
- Defined in:
- lib/undestroy/config.rb
Constant Summary collapse
- OPTIONS =
[ :table_name, :abstract_class, :fields, :migrate, :source_class, :target_class, :internals ]
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(options = {}) ⇒ Config
constructor
A new instance of Config.
- #merge(object) ⇒ Object
- #primitive_fields(object) ⇒ Object
- #to_hash ⇒ Object
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 |
# File 'lib/undestroy/config.rb', line 8 def initialize(={}) self.migrate = true self.fields = { :deleted_at => proc { Time.now } } self.internals = { :archive => Undestroy::Archive, :transfer => Undestroy::Transfer, } .each do |key, value| self[key] = value end end |
Class Method Details
.config ⇒ Object
49 50 51 |
# File 'lib/undestroy/config.rb', line 49 def self.config @config ||= self.new end |
.configure {|config| ... } ⇒ Object
45 46 47 |
# File 'lib/undestroy/config.rb', line 45 def self.configure yield(config) if block_given? end |
Instance Method Details
#[](key) ⇒ Object
23 24 25 |
# File 'lib/undestroy/config.rb', line 23 def [](key) self.send(key) if OPTIONS.include?(key) end |
#[]=(key, value) ⇒ Object
27 28 29 |
# File 'lib/undestroy/config.rb', line 27 def []=(key, value) self.send("#{key}=", value) if OPTIONS.include?(key) end |
#merge(object) ⇒ Object
35 36 37 |
# File 'lib/undestroy/config.rb', line 35 def merge(object) self.class.new(self.to_hash.merge(object.to_hash)) end |
#primitive_fields(object) ⇒ Object
39 40 41 42 43 |
# File 'lib/undestroy/config.rb', line 39 def primitive_fields(object) self.fields.inject({}) do |hash, (key, val)| hash.merge(key => val.is_a?(Proc) ? val.call(object) : val) end end |
#to_hash ⇒ Object
31 32 33 |
# File 'lib/undestroy/config.rb', line 31 def to_hash OPTIONS.inject({}) { |hash, key| hash.merge(key => self[key]) } end |