Class: Admincredible::Client
- Inherits:
-
Object
- Object
- Admincredible::Client
- Includes:
- Connection, JoomlaRequest, Request
- Defined in:
- lib/admincredible/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#initialize {|config| ... } ⇒ Client
constructor
A new instance of Client.
-
#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.
Methods included from JoomlaRequest
#jdelete, #jget, #jpost, #jput
Methods included from Request
Methods included from Connection
#build_connection, #connection, #rebuild_connection
Constructor Details
#initialize {|config| ... } ⇒ Client
Returns a new instance of Client.
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 = args.last.is_a?(Hash) ? args.pop : {} @resource_cache[method] ||= {} if ![:reload] && (cached = @resource_cache[method][.hash]) cached else resource_class = Admincredible::Resources.const_get(method.singularize.modulize) @resource_cache[method][.hash] = resource_class.new(self, ) end end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
21 22 23 |
# File 'lib/admincredible/client.rb', line 21 def config @config end |