lib/keytar/key_builder.rb in keytar-0.1.1 vs lib/keytar/key_builder.rb in keytar-0.9.0
- old
+ new
@@ -10,36 +10,45 @@
:key_prefix => nil,
:key_suffix => nil,
:key_pluralize_instances => true,
:key_case => :downcase,
:key_plural => nil,
- :key_unique => "id"
+ :key_unique => "id",
+ :key_cache_methods => true
}
def self.included(klass)
# setup method missing on class
- klass.class_eval %{
+ klass.class_eval do
extend KeyBuilder::Ext
# if method_missing doesn't already exist, aliasing and calling it will create an infinite loop
@@key_builder_jump_to_superclass = true
if klass.respond_to?("method_missing")
@@key_builder_jump_to_superclass = false
alias :key_builder_alias_method_missing :method_missing
end
def self.method_missing(method_name, *args, &blk)
if method_name.to_s =~ /.*key$/
+ ## Performance: define method so we can skip method_missing next time
+ if key_cache_methods
+ (class << self;self ;end).instance_eval do
+ define_method(method_name) do |*args|
+ build_key(:base => self.to_s.downcase, :name => method_name, :args => args)
+ end
+ end
+ end
self.build_key(:base => self.to_s.downcase, :name => method_name, :args => args)
else
if @@key_builder_jump_to_superclass
super
else
key_builder_alias_method_missing(method_name, *args, &blk)
end
end
end
- }
+ end
end
# class methods to be extended
module Ext
# creates class level getter and setter methods for the defaults for config
@@ -51,10 +60,15 @@
return @@#{key}
end
}
end
+ def keyfig(options = {})
+ options.keys.each do |key|
+ eval("@@#{key} = options[key]") if key.to_s =~ /^key_.*/
+ end
+ end
# Call KeyBuilder.build_key or Foo.build_key with options
# :base => self.to_s.downcase, :name => method_name, :args => args
def build_key(options = {})
key_hash = build_key_hash(options)
@@ -100,9 +114,17 @@
def method_missing(method_name, *args, &blk)
if method_name.to_s =~ /.*key$/
+ ## Performance: define method so we can skip method_missing next time
+ if self.class.key_cache_methods
+ self.class.instance_eval do
+ define_method(method_name) do |*args|
+ build_key(method_name, *args)
+ end
+ end
+ end
build_key(method_name, *args)
else
original_method_missing(method_name, *args, &blk)
end
end
\ No newline at end of file