Class: PathTo::DescribedRoutes::Application

Inherits:
WithParams
  • Object
show all
Defined in:
lib/path-to/described_routes.rb

Overview

DescribedRoutes implementation of PathTo::Application.

Instance Attribute Summary collapse

Attributes inherited from WithParams

#params, #parent, #service

Instance Method Summary collapse

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(options)
  super(options[:parent], options[:service], options[:params])

  @base = options[:base]
  @base.sub(/\/$/, '') if base
  @default_type = options[:default_type] || TemplatedPath
  @http_client = options[:http_client] || HTTPClient
  @http_options = options[:http_options]
  
  @resource_templates = options[:resource_templates]
  unless @resource_templates
    if (json = options[:json])
      @resource_templates = ::DescribedRoutes::ResourceTemplate.parse_json(json)
    elsif (yaml = options[:yaml])
      @resource_templates = ::DescribedRoutes::ResourceTemplate.parse_yaml(yaml)
    elsif (xml = options[: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.http_options
  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

  1. we have a resource template matching the method name

  2. #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

#baseObject (readonly)

Base URI of the application



141
142
143
# File 'lib/path-to/described_routes.rb', line 141

def base
  @base
end

#default_typeObject (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_clientObject (readonly)

An HTTParty or similar



138
139
140
# File 'lib/path-to/described_routes.rb', line 138

def http_client
  @http_client
end

#http_optionsObject (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
  @http_options
end

#resource_templatesObject (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

#applicationObject

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_nameObject

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