Module: Keytar

Extended by:
ActiveSupport::Concern
Defined in:
lib/keytar.rb

Defined Under Namespace

Modules: ClassMethods Classes: Key

Constant Summary collapse

DEFAULTS =
{:delimiter  => ":",
:order    => [:shard, :prefix, :base, :name, :unique, :args, :suffix, :version, :v],
:pluralize_instances => true,
:key_case => :downcase,
:unique   => :id}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_key_class_method_on(base, options = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/keytar.rb', line 41

def self.define_key_class_method_on(base, options = {})
  (class << base;self ;end).instance_eval do
    define_method("#{options[:name]}_key") do |*args|
      build_key(options.merge(:args => args))
    end
  end
end

.define_key_instance_method_on(base, options) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/keytar.rb', line 49

def self.define_key_instance_method_on(base, options)
  base.class_eval do
    define_method("#{options[:name]}_key") do |*args|
      build_key(options.merge(:args => args))
    end
  end
end

Instance Method Details

#build_key(options = {}) ⇒ Object

build_key method for instances by default class is pluralized to create different key



89
90
91
92
93
94
95
# File 'lib/keytar.rb', line 89

def build_key(options = {})
  options.reverse_merge!(self.class.key_config)
  unique            = method(options[:unique].to_sym).call if respond_to?(options[:unique].to_sym)
  options[:base]    = options[:base].to_s.pluralize unless options[:pluralize_instances].blank?
  options[:unique]  = unique && unique == object_id ?  nil  : unique
  Key.build(options)
end