Class: PathTo::DescribedRoutes::Application

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

Instance Attribute Summary collapse

Attributes inherited from WithParams

#params, #parent, #service

Instance Method Summary collapse

Methods inherited from WithParams

#child, #respond_to?

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(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
  
  @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
  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!



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

#baseObject (readonly)

Base URI of the application



88
89
90
# File 'lib/path-to/described_routes.rb', line 88

def base
  @base
end

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

An HTTParty or similar



85
86
87
# File 'lib/path-to/described_routes.rb', line 85

def http_client
  @http_client
end

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

#applicationObject

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_nameObject

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