Class: PathTo::DescribedRoutes::Application
- Inherits:
-
WithParams
- Object
- WithParams
- PathTo::DescribedRoutes::Application
- Defined in:
- lib/path-to/described_routes.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Base URI of the application.
-
#default_type ⇒ Object
readonly
A Class (or at least something with a #new method) from which child objects will be created.
-
#http_client ⇒ Object
readonly
An HTTParty or similar.
-
#resource_templates ⇒ Object
readonly
An Array of DescribedRoutes::Resource objects.
Attributes inherited from WithParams
Instance Method Summary collapse
-
#[](params = {}) ⇒ Object
Creates a copy of self with additional params.
-
#application ⇒ Object
Returns self.
-
#child_class_for(instance, method, params, template) ⇒ Object
Determines whether this application &/or its child objects should respond to the given method, and if so returns a class from which a new child instance (typically Path or a subclass thereof) will be created.
-
#initialize(options) ⇒ Application
constructor
A new instance of Application.
-
#method_missing(method, *args) ⇒ Object
Tries to respond to a missing method.
-
#resource_templates_by_name ⇒ Object
Returns a hash of all ResourceTemplates (the tree flattened) keyed by name.
Methods inherited from WithParams
Constructor Details
#initialize(options) ⇒ Application
Returns a new instance of Application.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/path-to/described_routes.rb', line 90 def initialize() super([:parent], [:service], [:params]) @base = [:base] @base.sub(/\/$/, '') if base @default_type = [:default_type] || TemplatedPath @http_client = [:http_client] || HTTPClient @resource_templates = [:resource_templates] unless @resource_templates if (json = [:json]) @resource_templates = ::DescribedRoutes::ResourceTemplate.parse_json(json) elsif (yaml = [:yaml]) @resource_templates = ::DescribedRoutes::ResourceTemplate.parse_yaml(yaml) elsif (xml = [:xml]) @resource_templates = ::DescribedRoutes::ResourceTemplate.parse_xml(xml) end end if parent @base ||= parent.base @default_type ||= parent.default_type @http_client ||= parent.http_client @resource_templates ||= parent.resource_templates end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
Tries to respond to a missing method. We can do so if
-
we have a resource template matching the method name
-
#child_class_for returns a class or other factory object capable of creating a new child instance
Otherwise we invoke super in the hope of avoiding any hard-to-debug behaviour!
132 133 134 135 136 137 138 139 140 |
# File 'lib/path-to/described_routes.rb', line 132 def method_missing(method, *args) resource_template = resource_templates_by_name[method.to_s] if resource_template && (child_class = child_class_for(self, method, params, resource_template)) params = args.inject(Hash.new){|h, arg| h.merge(arg)} child(child_class, method, params, resource_template) else super end end |
Instance Attribute Details
#base ⇒ Object (readonly)
Base URI of the application
88 89 90 |
# File 'lib/path-to/described_routes.rb', line 88 def base @base end |
#default_type ⇒ Object (readonly)
A Class (or at least something with a #new method) from which child objects will be created
82 83 84 |
# File 'lib/path-to/described_routes.rb', line 82 def default_type @default_type end |
#http_client ⇒ Object (readonly)
An HTTParty or similar
85 86 87 |
# File 'lib/path-to/described_routes.rb', line 85 def http_client @http_client end |
#resource_templates ⇒ Object (readonly)
An Array of DescribedRoutes::Resource objects
79 80 81 |
# File 'lib/path-to/described_routes.rb', line 79 def resource_templates @resource_templates end |
Instance Method Details
#[](params = {}) ⇒ Object
Creates a copy of self with additional params
120 121 122 |
# File 'lib/path-to/described_routes.rb', line 120 def [](params = {}) self.class.new(:parent => self, :params => params) end |
#application ⇒ Object
Returns self. See TemplatedPath#application.
167 168 169 |
# File 'lib/path-to/described_routes.rb', line 167 def application self end |
#child_class_for(instance, method, params, template) ⇒ Object
Determines whether this application &/or its child objects should respond to the given method, and if so returns a class from which a new child instance (typically Path or a subclass thereof) will be created. This implementation (easily overridden) returns #default_type if there is a URI template defined for the method.
Parameters:
- instance
-
This application or (presumably) one of its child objects
- method
-
The method invoked on the instance that has (presumably) been intercepted by instance#method_missing
- params
-
The instance’s params
153 154 155 |
# File 'lib/path-to/described_routes.rb', line 153 def child_class_for(instance, method, params, template) default_type end |
#resource_templates_by_name ⇒ Object
Returns a hash of all ResourceTemplates (the tree flattened) keyed by name
160 161 162 |
# File 'lib/path-to/described_routes.rb', line 160 def resource_templates_by_name @resource_templates_by_name ||= ::DescribedRoutes::ResourceTemplate.all_by_name(resource_templates) end |