Module: Tanuki::ControllerBehavior::ClassMethods
- Included in:
- Tanuki::ControllerBehavior
- Defined in:
- lib/tanuki/behavior/controller_behavior.rb
Overview
Tanuki::ControllerBehavior mixed-in class methods.
Class Method Summary collapse
-
.extended(mod) ⇒ Object
Prepares the extended module.
Instance Method Summary collapse
-
#arg_defs ⇒ Object
Returns own or superclass argument definitions.
-
#escape(s, chrs) ⇒ Object
Escapes characters
chrsand encodes a given stringsfor use in links. -
#extract_args(md) ⇒ Object
Extracts arguments, initializing default values beforehand.
-
#grow_link(ctrl, route_part, arg_defs) ⇒ Object
Builds link from controller
ctrlto a given route. -
#has_arg(name, obj, *args) ⇒ Object
Defines an argument with a
name, derived from typeobjwith additionalargs.
Class Method Details
.extended(mod) ⇒ Object
Prepares the extended module.
262 263 264 |
# File 'lib/tanuki/behavior/controller_behavior.rb', line 262 def self.extended(mod) mod.instance_variable_set(:@_arg_defs, {}) end |
Instance Method Details
#arg_defs ⇒ Object
Returns own or superclass argument definitions.
229 230 231 |
# File 'lib/tanuki/behavior/controller_behavior.rb', line 229 def arg_defs @_arg_defs ||= superclass.arg_defs.dup end |
#escape(s, chrs) ⇒ Object
Escapes characters chrs and encodes a given string s for use in links.
234 235 236 |
# File 'lib/tanuki/behavior/controller_behavior.rb', line 234 def escape(s, chrs) s ? Rack::Utils.escape(s.to_s.gsub(/[\$#{chrs}]/, '$\0')) : nil end |
#extract_args(md) ⇒ Object
Extracts arguments, initializing default values beforehand. Searches md hash for default value overrides.
239 240 241 242 243 244 245 |
# File 'lib/tanuki/behavior/controller_behavior.rb', line 239 def extract_args(md) res = [] arg_defs.each_pair do |name, arg| res[arg[:index]] = md[name] end res end |
#grow_link(ctrl, route_part, arg_defs) ⇒ Object
Builds link from controller ctrl to a given route.
248 249 250 251 252 253 |
# File 'lib/tanuki/behavior/controller_behavior.rb', line 248 def grow_link(ctrl, route_part, arg_defs) own_link = escape(route_part[:route], '\/:') << route_part[:args].map do |k, v| arg_defs[k][:arg].default == v ? '' : ":#{escape(k, '\/:-')}-#{escape(v, '\/:')}" end.join "#{ctrl.link == '/' ? '' : ctrl.link}/#{own_link}" end |
#has_arg(name, obj, *args) ⇒ Object
Defines an argument with a name, derived from type obj with additional args.
256 257 258 259 |
# File 'lib/tanuki/behavior/controller_behavior.rb', line 256 def has_arg(name, obj, *args) # TODO Ensure thread safety arg_defs[name] = {:arg => Argument.to_argument(obj, *args), :index => @_arg_defs.size} end |