Class: ResourcesController::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/resources_controller/specification.rb

Overview

This class holds all the info that is required to find a resource, or determine a name prefix, based on a route segment or segment pair (e.g. /blog or /users/3).

You don't need to instantiate this class directly - it is created by ResourcesController::ClassMethods#nested_in, ResourcesController#map_resource (and ResourcesController::InstanceMethods#load_wildcard)

This is primarily a container class. A summary of its behaviour:

  1. setting defaults for its own variables on initialize, and

  2. finding an enclosing resource, given a controller object

Direct Known Subclasses

SingletonSpecification

Defined Under Namespace

Classes: NoClassFoundError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_name, options = {}, &block) ⇒ Specification

Example Usage

Specifcation.new <name>, <options hash>, <&block>

name should always be singular.

Options:

  • :singleton: (default false) set this to true if the resource is a Singleton

  • :find: (default null) set this to a symbol or Proc to specify how to find the resource. Use this if the resource is found in an unconventional way

Options for unconvential use (otherwise these are all inferred from the name)

  • :source: a plural string or symbol (e.g. :users). This is used to find the class or association name

  • :class: a Class. This is the class of the resource (if it can't be inferred from name or :source)

  • :key: (e.g. :user_id) used to find the resource id in params

  • :name_prefix: (e.g. 'user_') (set this to false if you want to specify that there is none)

  • :segment: (e.g. 'users') the segment name in the route that is matched

Passing a block is the same as passing :find => Proc


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/resources_controller/specification.rb', line 48

def initialize(spec_name, options = {}, &block)
  options.assert_valid_keys(:class, :source, :key, :find, :name_prefix, :segment, :as)
  @name        = spec_name.to_s
  @find        = block || options.delete(:find)
  @segment     = (options[:segment] && options[:segment].to_s) || name.pluralize
  @source      = (options[:source] && options[:source].to_s) || name.pluralize
  @name_prefix = options[:name_prefix] || (options[:name_prefix] == false ? '' : "#{name}_")
  @klass       = options[:class] || infer_class
  @key         = (options[:key] && options[:key].to_s) || name.foreign_key
  @as          = options[:as]
end

Instance Attribute Details

#asObject

Returns the value of attribute as


19
20
21
# File 'lib/resources_controller/specification.rb', line 19

def as
  @as
end

#findObject (readonly)

Returns the value of attribute find


18
19
20
# File 'lib/resources_controller/specification.rb', line 18

def find
  @find
end

#keyObject (readonly)

Returns the value of attribute key


18
19
20
# File 'lib/resources_controller/specification.rb', line 18

def key
  @key
end

#klassObject (readonly)

Returns the value of attribute klass


18
19
20
# File 'lib/resources_controller/specification.rb', line 18

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name


18
19
20
# File 'lib/resources_controller/specification.rb', line 18

def name
  @name
end

#name_prefixObject (readonly)

Returns the value of attribute name_prefix


18
19
20
# File 'lib/resources_controller/specification.rb', line 18

def name_prefix
  @name_prefix
end

#segmentObject (readonly)

Returns the value of attribute segment


18
19
20
# File 'lib/resources_controller/specification.rb', line 18

def segment
  @segment
end

#sourceObject (readonly)

Returns the value of attribute source


18
19
20
# File 'lib/resources_controller/specification.rb', line 18

def source
  @source
end

Class Method Details

.new(name, options = {}, &block) ⇒ Object

factory for Specification and SingletonSpecification

you can call Specification.new 'name', :singleton => true


24
25
26
# File 'lib/resources_controller/specification.rb', line 24

def self.new(name, options = {}, &block)
  options.delete(:singleton) ? SingletonSpecification.new(name, options, &block) : super(name, options, &block)
end

Instance Method Details

#find_custom(controller) ⇒ Object

finds the resource using the custom :find Proc or symbol


71
72
73
74
# File 'lib/resources_controller/specification.rb', line 71

def find_custom(controller)
  raise "This specification has no custom :find attribute" unless find
  find.is_a?(Proc) ? controller.instance_exec(&find) : controller.send(find)
end

#find_from(controller) ⇒ Object

given a controller object, returns the resource according to this specification


66
67
68
# File 'lib/resources_controller/specification.rb', line 66

def find_from(controller)
  find ? find_custom(controller) : find_resource(controller)
end

#find_resource(controller) ⇒ Object

finds the resource on a controller using enclosing resources or resource class


77
78
79
# File 'lib/resources_controller/specification.rb', line 77

def find_resource(controller)
  (controller.enclosing_resource ? controller.enclosing_resource.send(source) : klass).find controller.params[key]
end

#singleton?Boolean

returns false


61
62
63
# File 'lib/resources_controller/specification.rb', line 61

def singleton?
  false
end