module WrapIt
Registering helpers
To use your classes and helpers in templates you should register your library. First, in your framework initialization, you should register your library in WrapIt
. You can have separate module for it or WrapIt
will make anonymous one. After this, your module will have `register` and `unregister` methods to register your classes.
@example usual case - library in separate module
# initialization time module Library module Helpers; end end WrapIt.register_module Library::Helpers, prefix: 'lib_' # controller class MyController < ApplicationController helper Library::Helpers end # implementation module Library module Helpers class Button < WrapIt::Base ... end register :super_button, Button end end # in template: <%= lib_super_button 'text' %>
@example anonymous module
helpers = WrapIt.register_module prefix: 'lib_' class Button < WrapIt::Base ... end helpers.register :super_button, Button class MyController helper helpers end
@author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
Constants
- VERSION
Current gem version
Public Class Methods
framework()
click to toggle source
@private
# File lib/wrap_it/frameworks.rb, line 7 def self.framework return @framework unless @framework.nil? gems = Gem.loaded_specs.keys if gems.include?('rails') @framework = :rails elsif gems.include?('sinatra') @framework = :sinatra else @framework = :unknown end end
rails?()
click to toggle source
@private
# File lib/wrap_it/frameworks.rb, line 20 def self.rails? framework == :rails end
register_module(*args)
click to toggle source
Registers helpers module
@overload register_module
(mod = nil, opts = {})
@param mod [Module] module for register. Anonymous module will be created if ommited. @param opts [Hash] options @option opts [String] :prefix prefix for helper methods
@return [void]
# File lib/wrap_it/helpers.rb, line 62 def self.register_module(*args) options = args.extract_options! options.symbolize_keys! options = {prefix: ''}.merge(options) mod = args.shift mod.is_a?(Module) || mod = Module.new mod.instance_eval do define_singleton_method(:register, &WrapIt.register_block(options)) define_singleton_method(:unregister, &WrapIt.unregister_block(options)) end mod end
sinatra?()
click to toggle source
@private
# File lib/wrap_it/frameworks.rb, line 25 def self.sinatra? framework == :sinatra end
Private Class Methods
method_block(name, class_name)
click to toggle source
# File lib/wrap_it/helpers.rb, line 128 def self.method_block(name, class_name) # Runs in helpers module context proc do |*args, &block| opts = args.extract_options! opts[:helper_name] = name args << opts obj = Object.const_get(class_name).new(self, *args, &block) obj.render end end
register_block(options)
click to toggle source
@!method unregister([name, …]) removes list of helpers from template @param name [Symbol, String] name of helper to remove from tampate
# File lib/wrap_it/helpers.rb, line 87 def self.register_block(options) # Runs in helpers module class context proc do |*args| class_name = args.pop class_name.is_a?(String) || class_name.is_a?(Class) || fail( ArgumentError, "Last argument for #{name}.register_helper should be a class name" ) class_name.is_a?(Class) && class_name = class_name.name helpers = instance_methods args.each do |helper| !helper.is_a?(Symbol) && fail( ArgumentError, "First arguments for WrapIt.register" \ " should be Symbols with helper names" ) helpers.include?(helper) && fail( ArgumentError, "Helper #{helper} for WrapIt.register allready exists" ) define_method( "#{options[:prefix]}#{helper}", WrapIt.method_block(helper, class_name) ) end end end
unregister_block(options)
click to toggle source
# File lib/wrap_it/helpers.rb, line 114 def self.unregister_block(options) # Runs in helpers module class context proc do |*list| helpers = instance_methods list.each do |helper| helper.is_a?(String) && helper = helper.to_sym next unless helper.is_a?(Symbol) helper_name = "#{options[:prefix]}#{helper}" next unless helpers.include?(helper_name) remove_method helper_name end end end