Class: Kingsman::Mapping
- Inherits:
-
Object
- Object
- Kingsman::Mapping
- Defined in:
- lib/kingsman/mapping.rb
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#controllers ⇒ Object
readonly
Returns the value of attribute controllers.
-
#failure_app ⇒ Object
readonly
Returns the value of attribute failure_app.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#path_names ⇒ Object
readonly
Returns the value of attribute path_names.
-
#router_name ⇒ Object
readonly
Returns the value of attribute router_name.
-
#scoped_path ⇒ Object
readonly
Returns the value of attribute scoped_path.
-
#sign_out_via ⇒ Object
readonly
Returns the value of attribute sign_out_via.
-
#singular ⇒ Object
readonly
Returns the value of attribute singular.
-
#used_helpers ⇒ Object
readonly
Returns the value of attribute used_helpers.
-
#used_routes ⇒ Object
readonly
Returns the value of attribute used_routes.
Class Method Summary collapse
-
.add_module(m) ⇒ Object
Create magic predicates for verifying what module is activated by this map.
- .find_by_path!(path, path_type = :fullpath) ⇒ Object
-
.find_scope!(obj) ⇒ Object
Receives an object and find a scope for it.
Instance Method Summary collapse
- #authenticatable? ⇒ Boolean
- #fullpath ⇒ Object
-
#initialize(name, options) ⇒ Mapping
constructor
A new instance of Mapping.
-
#modules ⇒ Object
Return modules for the mapping.
- #no_input_strategies ⇒ Object
- #routes ⇒ Object
- #strategies ⇒ Object
- #to ⇒ Object
Constructor Details
#initialize(name, options) ⇒ Mapping
Returns a new instance of Mapping.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/kingsman/mapping.rb', line 29 def initialize(name, ) @scoped_path = [:as] ? "#{[:as]}/#{name}" : name.to_s @name = name.to_s.singularize.to_sym @class_name = ([:class_name] || name.to_s.classify).to_s @path = ([:path] || name).to_s @path_prefix = [:path_prefix] @sign_out_via = [:sign_out_via] || Kingsman.sign_out_via @format = [:format] # default_failure_app(options) default_controllers() default_path_names() default_used_route() # default_used_helpers(options) end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def class_name @class_name end |
#controllers ⇒ Object (readonly)
Returns the value of attribute controllers.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def controllers @controllers end |
#failure_app ⇒ Object (readonly)
Returns the value of attribute failure_app.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def failure_app @failure_app end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def format @format end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/kingsman/mapping.rb', line 6 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def path @path end |
#path_names ⇒ Object (readonly)
Returns the value of attribute path_names.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def path_names @path_names end |
#router_name ⇒ Object (readonly)
Returns the value of attribute router_name.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def router_name @router_name end |
#scoped_path ⇒ Object (readonly)
Returns the value of attribute scoped_path.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def scoped_path @scoped_path end |
#sign_out_via ⇒ Object (readonly)
Returns the value of attribute sign_out_via.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def sign_out_via @sign_out_via end |
#singular ⇒ Object (readonly)
Returns the value of attribute singular.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def singular @singular end |
#used_helpers ⇒ Object (readonly)
Returns the value of attribute used_helpers.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def used_helpers @used_helpers end |
#used_routes ⇒ Object (readonly)
Returns the value of attribute used_routes.
3 4 5 |
# File 'lib/kingsman/mapping.rb', line 3 def used_routes @used_routes end |
Class Method Details
.add_module(m) ⇒ Object
Create magic predicates for verifying what module is activated by this map. Example:
def confirmable?
self.modules.include?(:confirmable)
end
83 84 85 86 87 88 89 |
# File 'lib/kingsman/mapping.rb', line 83 def self.add_module(m) class_eval <<-METHOD, __FILE__, __LINE__ + 1 def #{m}? self.modules.include?(:#{m}) end METHOD end |
.find_by_path!(path, path_type = :fullpath) ⇒ Object
24 25 26 27 |
# File 'lib/kingsman/mapping.rb', line 24 def self.find_by_path!(path, path_type = :fullpath) Kingsman.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!(obj) ⇒ 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.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/kingsman/mapping.rb', line 10 def self.find_scope!(obj) obj = obj.kingsman_scope if obj.respond_to?(:kingsman_scope) case obj when String, Symbol return obj.to_sym when Class Kingsman.mappings.each_value { |m| return m.name if obj <= m.to } else Kingsman.mappings.each_value { |m| return m.name if obj.is_a?(m.to) } end raise "Could not find a valid mapping for #{obj.inspect}" end |
Instance Method Details
#authenticatable? ⇒ Boolean
68 69 70 |
# File 'lib/kingsman/mapping.rb', line 68 def authenticatable? @authenticatable ||= self.modules.any? { |m| m.to_s =~ /authenticatable/ } end |
#fullpath ⇒ Object
72 73 74 |
# File 'lib/kingsman/mapping.rb', line 72 def fullpath "/#{@path_prefix}/#{@path}".squeeze("/") end |
#modules ⇒ Object
Return modules for the mapping.
48 49 50 |
# File 'lib/kingsman/mapping.rb', line 48 def modules @modules ||= to.respond_to?(:kingsman_modules) ? to.kingsman_modules : [] end |
#no_input_strategies ⇒ Object
60 61 62 |
# File 'lib/kingsman/mapping.rb', line 60 def no_input_strategies self.strategies & Kingsman::NO_INPUT end |
#routes ⇒ Object
64 65 66 |
# File 'lib/kingsman/mapping.rb', line 64 def routes @routes ||= ROUTES.values_at(*self.modules).compact.uniq end |
#strategies ⇒ Object
56 57 58 |
# File 'lib/kingsman/mapping.rb', line 56 def strategies @strategies ||= STRATEGIES.values_at(*self.modules).compact.uniq.reverse end |
#to ⇒ Object
52 53 54 |
# File 'lib/kingsman/mapping.rb', line 52 def to @class_name.constantize end |