Class: Cullender::Mapping
- Inherits:
-
Object
- Object
- Cullender::Mapping
- Defined in:
- lib/cullender/mapping.rb
Overview
Responsible for handling cullender mappings and routes configuration. Each resource configured by cullender_for in routes is actually creating a mapping object. You can refer to cullender_for in routes for usage options.
The required value in cullender_for is actually not used internally, but it’s inflected to find all other values.
map.cullender_for :events
mapping = Cullender.mappings[:event]
mapping.name #=> :event
# is the scope used in controllers and warden, given in the route as :singular.
mapping.as #=> "events"
# how the mapping should be search in the path, given in the route as :as.
mapping.to #=> Event
# is the class to be loaded from routes, given in the route as :class_name.
mapping.modules #=> [:authenticatable]
# is the modules included in the class
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
:nodoc:.
-
#controllers ⇒ Object
readonly
:nodoc:.
-
#failure_app ⇒ Object
readonly
:nodoc:.
-
#format ⇒ Object
readonly
:nodoc:.
-
#path ⇒ Object
readonly
:nodoc:.
-
#path_names ⇒ Object
readonly
:nodoc:.
-
#scoped_path ⇒ Object
readonly
:nodoc:.
-
#sign_out_via ⇒ Object
readonly
:nodoc:.
-
#singular ⇒ Object
(also: #name)
readonly
:nodoc:.
-
#used_helpers ⇒ Object
readonly
:nodoc:.
-
#used_routes ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
- .find_by_path!(path, path_type = :fullpath) ⇒ Object
-
.find_scope!(duck) ⇒ Object
Receives an object and find a scope for it.
Instance Method Summary collapse
- #fullpath ⇒ Object
-
#initialize(name, options) ⇒ Mapping
constructor
:nodoc:.
- #routes ⇒ Object
-
#to ⇒ Object
Gives the class the mapping points to.
Constructor Details
#initialize(name, options) ⇒ Mapping
:nodoc:
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cullender/mapping.rb', line 50 def initialize(name, ) #:nodoc: @scoped_path = [:as] ? "#{[:as]}/#{name}" : name.to_s @singular = ([:singular] || @scoped_path.tr('/', '_').singularize).to_sym @class_name = ([:class_name] || name.to_s.classify).to_s @klass = Cullender.ref(@class_name) @path = ([:path] || name).to_s @path_prefix = [:path_prefix] @format = [:format] # default_failure_app(options) default_controllers() default_path_names() default_used_route() default_used_helpers() end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def class_name @class_name end |
#controllers ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def controllers @controllers end |
#failure_app ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def failure_app @failure_app end |
#format ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def format @format end |
#path ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def path @path end |
#path_names ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def path_names @path_names end |
#scoped_path ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def scoped_path @scoped_path end |
#sign_out_via ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def sign_out_via @sign_out_via end |
#singular ⇒ Object (readonly) Also known as: name
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def singular @singular end |
#used_helpers ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def used_helpers @used_helpers end |
#used_routes ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/cullender/mapping.rb', line 25 def used_routes @used_routes end |
Class Method Details
.find_by_path!(path, path_type = :fullpath) ⇒ Object
45 46 47 48 |
# File 'lib/cullender/mapping.rb', line 45 def self.find_by_path!(path, path_type=:fullpath) Cullender.mappings.each_value { |m| return m if path.include?(m.send(path_type)) } raise "Could not find a valid mapping for path #{path.inspect}" end |
.find_scope!(duck) ⇒ Object
Receives an object and find a scope for it. If a scope cannot be found, raises an error. If a symbol is given, it’s considered to be the scope.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/cullender/mapping.rb', line 32 def self.find_scope!(duck) case duck when String, Symbol return duck when Class Cullender.mappings.each_value { |m| return m.name if duck <= m.to } else Cullender.mappings.each_value { |m| return m.name if duck.is_a?(m.to) } end raise "Could not find a valid mapping for #{duck.inspect}" end |
Instance Method Details
#fullpath ⇒ Object
78 79 80 |
# File 'lib/cullender/mapping.rb', line 78 def fullpath "/#{@path_prefix}/#{@path}".squeeze("/") end |
#routes ⇒ Object
74 75 76 |
# File 'lib/cullender/mapping.rb', line 74 def routes @routes ||= ["rule"]#.compact.uniq end |
#to ⇒ Object
Gives the class the mapping points to.
70 71 72 |
# File 'lib/cullender/mapping.rb', line 70 def to @klass.get end |