Module: Keytar::ClassMethods

Defined in:
lib/keytar.rb

Overview

class methods to be extended

Instance Method Summary collapse

Instance Method Details

#build_key(options = {}) ⇒ Object

Call Keytar.build_key or Foo.build_key with options :base => self.to_s.downcase, :name => method_name, :args => args



81
82
83
84
85
# File 'lib/keytar.rb', line 81

def build_key(options = {})
  input = options.reverse_merge(key_config)
  input.delete(:unique) # class methods don't have a unique key
  Key.build(input)
end

#define_keys(*args) ⇒ Object Also known as: define_key

sets up configuration options for individual keys alows us to define the keys without calling method missing



61
62
63
64
65
66
67
68
69
# File 'lib/keytar.rb', line 61

def define_keys(*args)
  # coherce args into meaningful things
  names = []; options = {}; args.each {|arg| arg.is_a?(Hash) ? options = arg : names << arg}
  names.each do |name|
    name = name.to_s.gsub(/(^key$|_key$)/, '')
    Keytar.define_key_class_method_on(self, options.merge(:name => name))
    Keytar.define_key_instance_method_on(self, options.merge(:name => name))
  end
end

#key_config(options = {}) ⇒ Object

a way to define class level configurations for keytar using a hash



74
75
76
77
# File 'lib/keytar.rb', line 74

def key_config(options = {})
    options[:base]  = self
    @key_config     ||= options.reverse_merge(Keytar::DEFAULTS)
end