Skip to content

Commit

Permalink
makes the configuration object a lot simpler and more flexible by all…
Browse files Browse the repository at this point in the history
…owing custom settings on init
  • Loading branch information
markrebec committed May 20, 2015
1 parent 45a8130 commit ceaca94
Showing 1 changed file with 60 additions and 34 deletions.
94 changes: 60 additions & 34 deletions lib/transactor/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,67 +1,93 @@
# TODO make this thing a little more dynamic, allow passing defaults when initializing
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/hash/keys'

module Transactor
class Configuration
DEFAULT_CONFIGURATION_OPTIONS = {}

attr_reader *DEFAULT_CONFIGURATION_OPTIONS.keys
def configure(argh={}, &block)
save_state! do
configure_with_args argh
configure_with_block &block
end
self
end

DEFAULT_CONFIGURATION_OPTIONS.keys.each do |key|
define_method "#{key.to_s}=" do |val|
@changed[key] = [send(key), val]
instance_variable_set "@#{key.to_s}", val
def configure_with_args(argh)
save_state! do
argh.symbolize_keys.each { |key,val| configure_option(key, val) }
end
end

def configure_with_block(&block)
save_state! do
instance_eval &block if block_given?
end
end

def configure_option(key, val)
save_state! do
@changed[key] = [@options[key], val]
@options[key] = val
end
end

def changed
@changed = {}
to_hash.each { |key,val| @changed[key] = [@saved_state[key], val] if @saved_state[key] != val }
@options.each { |key,val| @changed[key] = [@saved_state[key], val] if @saved_state[key] != val }
@changed
end

def changed?(key)
changed.has_key?(key)
changed.key?(key)
end

def configure(args={}, &block)
save_state
configure_with_args args
configure_with_block &block
self
end
def save_state!(&block)
if save_state?
@saved_state = @options.dup
@changed = {}

def configure_with_args(args)
args.select { |k,v| DEFAULT_CONFIGURATION_OPTIONS.keys.include?(k) }.each do |key,val|
instance_variable_set "@#{key.to_s}", val
if block_given?
disable_state_saves!
begin
yield
ensure
enable_state_saves!
end
end
else
yield if block_given?
end
end

def configure_with_block(&block)
self.instance_eval(&block) if block_given?
def enable_state_saves!
@save_state = true
end

def save_state
@saved_state = clone.to_hash
@changed = {}
def disable_state_saves!
@save_state = false
end

def save_state?
@save_state
end

def to_hash
h = {}
DEFAULT_CONFIGURATION_OPTIONS.keys.each do |key|
h[key] = instance_variable_get "@#{key.to_s}"
def method_missing(meth, *args, &block)
if meth.to_s.match(/=\Z/)
opt = meth.to_s.gsub(/=/,'').to_sym
return configure_option(opt, args.first) if @options.key?(opt)
else
return @options[meth] if @options.key?(meth)
end
h

super
end
alias_method :to_h, :to_hash

protected

def initialize
DEFAULT_CONFIGURATION_OPTIONS.each do |key,val|
instance_variable_set "@#{key.to_s}", val
end
save_state
def initialize(*args, &block)
@options = {}
enable_state_saves!
configure args.extract_options!, &block
end
end
end

0 comments on commit ceaca94

Please sign in to comment.