Skip to content

Commit

Permalink
uses canfig for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
markrebec committed May 21, 2015
1 parent acb161d commit c627aa9
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 94 deletions.
1 change: 1 addition & 0 deletions lib/transactor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/string/output_safety'
require 'canfig'
require 'transactor/version'
require 'transactor/configuration'
require 'transactor/errors'
Expand Down
95 changes: 1 addition & 94 deletions lib/transactor/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,97 +1,4 @@
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/hash/keys'

module Transactor
class Configuration

def configure(argh={}, &block)
@options ||= {}
save_state! do
configure_with_args argh
configure_with_block &block
end
self
end

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)
raise ArgumentError, "#{key} is not an allowed configuration option" unless @allowed.include?(key)

save_state! do
@changed[key] = [@options[key], val]
@options[key] = val
end
end

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

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

def save_state!(&block)
if save_state?
@saved_state = @options.dup
@changed = {}

if block_given?
disable_state_saves!
begin
yield
ensure
enable_state_saves!
end
end
else
yield if block_given?
end
end

def enable_state_saves!
@save_state = true
end

def disable_state_saves!
@save_state = false
end

def save_state?
@save_state
end

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 @allowed.include?(opt)
else
return @options[meth] if @allowed.include?(meth)
end

super
end

protected

def initialize(*args, &block)
options = args.extract_options!
@allowed = options.symbolize_keys.keys
enable_state_saves!
configure options, &block
end
class Configuration < ::Canfig::Configuration
end
end
1 change: 1 addition & 0 deletions transactor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Gem::Specification.new do |s|
s.homepage = "http://github.com/markrebec/transactor"

s.add_dependency "activesupport"
s.add_dependency "canfig"

s.add_development_dependency "rake"
s.add_development_dependency "rspec"
Expand Down

0 comments on commit c627aa9

Please sign in to comment.