Class: PathTo::DescribedRoutes::Application
- Inherits:
-
WithParams
- Object
- WithParams
- PathTo::DescribedRoutes::Application
- Defined in:
- lib/path-to/described_routes.rb
Overview
DescribedRoutes implementation of PathTo::Application.
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.
-
#http_options ⇒ Object
readonly
Hash of options to be included in HTTP method calls.
-
#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
#child, #complete_params_hash!, #extract_params, #respond_to?
Constructor Details
#initialize(options) ⇒ Application
Returns a new instance of Application.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/path-to/described_routes.rb', line 146 def initialize() super([:parent], [:service], [:params]) @base = [:base] @base.sub(/\/$/, '') if base @default_type = [:default_type] || TemplatedPath @http_client = [:http_client] || HTTPClient @http_options = [:http_options] @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 @http_options ||= parent. 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!
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/path-to/described_routes.rb', line 190 def method_missing(method, *args) child_resource_template = resource_templates_by_name[method.to_s] if child_resource_template && (child_class = child_class_for(self, method, params, child_resource_template)) positional_params, params_hash = extract_params(args, params) complete_params_hash!(params_hash, child_resource_template.positional_params(nil), positional_params) child(child_class, method, params_hash, child_resource_template) else super end end |
Instance Attribute Details
#base ⇒ Object (readonly)
Base URI of the application
141 142 143 |
# File 'lib/path-to/described_routes.rb', line 141 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
135 136 137 |
# File 'lib/path-to/described_routes.rb', line 135 def default_type @default_type end |
#http_client ⇒ Object (readonly)
An HTTParty or similar
138 139 140 |
# File 'lib/path-to/described_routes.rb', line 138 def http_client @http_client end |
#http_options ⇒ Object (readonly)
Hash of options to be included in HTTP method calls
144 145 146 |
# File 'lib/path-to/described_routes.rb', line 144 def @http_options end |
#resource_templates ⇒ Object (readonly)
An Array of DescribedRoutes::Resource objects
132 133 134 |
# File 'lib/path-to/described_routes.rb', line 132 def resource_templates @resource_templates end |
Instance Method Details
#[](params) ⇒ Object
Creates a copy of self with additional params
178 179 180 |
# File 'lib/path-to/described_routes.rb', line 178 def [](params) self.class.new(:parent => self, :params => params) end |
#application ⇒ Object
Returns self. See TemplatedPath#application.
226 227 228 |
# File 'lib/path-to/described_routes.rb', line 226 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
212 213 214 |
# File 'lib/path-to/described_routes.rb', line 212 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
219 220 221 |
# File 'lib/path-to/described_routes.rb', line 219 def resource_templates_by_name @resource_templates_by_name ||= ::DescribedRoutes::ResourceTemplate.all_by_name(resource_templates) end |