Class: HashAuth::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hash-auth/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Client

The hash passed in to a new client will initialize this Client object with getters and setters for each key in the hash. The default value for each getter will be the associated value passed in the hash



6
7
8
9
10
11
# File 'lib/hash-auth/client.rb', line 6

def initialize(hash)
  hash.each do |key,value|
    add_instance_getters_and_setters key
    send "#{key}=", value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/hash-auth/client.rb', line 26

def method_missing(method, *args, &block)
  # Check config for default value
  default = "default_#{method}"
  if HashAuth.configuration.respond_to? default
    HashAuth.configuration.send default
  end
end

Instance Method Details

#add_instance_getters_and_setters(var) ⇒ Object

Add instance specific getters and setters for the name passed in, so as to allow different Client objects to have different properties that are accessible by . notation



16
17
18
19
20
21
22
23
24
# File 'lib/hash-auth/client.rb', line 16

def add_instance_getters_and_setters(var)
  singleton = (class << self; self end)
  singleton.send :define_method, var.to_sym do
    instance_variable_get "@#{var}"
  end
  singleton.send :define_method, "#{var}=".to_sym do |val|
    instance_variable_set "@#{var}", val
  end
end