Class: Admincredible::Client

Inherits:
Object
  • Object
show all
Includes:
Connection, JoomlaRequest, Request
Defined in:
lib/admincredible/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JoomlaRequest

#jdelete, #jget, #jpost, #jput

Methods included from Request

#delete, #get, #post, #put

Methods included from Connection

#build_connection, #connection, #rebuild_connection

Constructor Details

#initialize {|config| ... } ⇒ Client

Returns a new instance of Client.

Yields:

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/admincredible/client.rb', line 23

def initialize
  raise ArgumentError, 'block not given' unless block_given?

  @config = Admincredible::Configuration.new
  yield config

  @resource_cache = {}

  if config.logger.nil? || config.logger == true
    require 'logger'
    config.logger       = Logger.new($stderr)
    config.logger.level = Logger::WARN
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

A user tries a missing method on our client, we assume it’s a resource class and look it up in Admincredible::Resources. For example, calling client.extensions.all, becomes Admincredible::Resources::Extension.new().all

Instances are cached, so this can be said to be sorta implementing that fancy schmancy singleton pattern all those cool developers are using.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/admincredible/client.rb', line 46

def method_missing(method, *args, &block)
  method  = method.to_s
  options = args.last.is_a?(Hash) ? args.pop : {}

  @resource_cache[method] ||= {}

  if !options[:reload] && (cached = @resource_cache[method][options.hash])
    cached
  else
    resource_class = Admincredible::Resources.const_get(method.singularize.modulize)
    @resource_cache[method][options.hash] = resource_class.new(self, options)
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/admincredible/client.rb', line 21

def config
  @config
end