Class: ResourcesController::Specification
- Inherits:
-
Object
- Object
- ResourcesController::Specification
- 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:
-
setting defaults for its own variables on initialize, and
-
finding an enclosing resource, given a controller object
Direct Known Subclasses
Defined Under Namespace
Classes: NoClassFoundError
Instance Attribute Summary collapse
-
#as ⇒ Object
Returns the value of attribute as.
-
#find ⇒ Object
readonly
Returns the value of attribute find.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#name_prefix ⇒ Object
readonly
Returns the value of attribute name_prefix.
-
#segment ⇒ Object
readonly
Returns the value of attribute segment.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
-
.new(name, options = {}, &block) ⇒ Object
factory for Specification and SingletonSpecification.
Instance Method Summary collapse
-
#find_custom(controller) ⇒ Object
finds the resource using the custom :find Proc or symbol.
-
#find_from(controller) ⇒ Object
given a controller object, returns the resource according to this specification.
-
#find_resource(controller) ⇒ Object
finds the resource on a controller using enclosing resources or resource class.
-
#initialize(spec_name, options = {}, &block) ⇒ Specification
constructor
Example Usage.
-
#singleton? ⇒ Boolean
returns false.
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, = {}, &block) .assert_valid_keys(:class, :source, :key, :find, :name_prefix, :segment, :as) @name = spec_name.to_s @find = block || .delete(:find) @segment = ([:segment] && [:segment].to_s) || name.pluralize @source = ([:source] && [:source].to_s) || name.pluralize @name_prefix = [:name_prefix] || ([:name_prefix] == false ? '' : "#{name}_") @klass = [:class] || infer_class @key = ([:key] && [:key].to_s) || name.foreign_key @as = [:as] end |
Instance Attribute Details
#as ⇒ Object
Returns the value of attribute as.
19 20 21 |
# File 'lib/resources_controller/specification.rb', line 19 def as @as end |
#find ⇒ Object (readonly)
Returns the value of attribute find.
18 19 20 |
# File 'lib/resources_controller/specification.rb', line 18 def find @find end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
18 19 20 |
# File 'lib/resources_controller/specification.rb', line 18 def key @key end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
18 19 20 |
# File 'lib/resources_controller/specification.rb', line 18 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
18 19 20 |
# File 'lib/resources_controller/specification.rb', line 18 def name @name end |
#name_prefix ⇒ Object (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 |
#segment ⇒ Object (readonly)
Returns the value of attribute segment.
18 19 20 |
# File 'lib/resources_controller/specification.rb', line 18 def segment @segment end |
#source ⇒ Object (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, = {}, &block) .delete(:singleton) ? SingletonSpecification.new(name, , &block) : super(name, , &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 |