Sha256: 258b9e11c518413139aa08e076a736d52e9e7bf8a929d8d1098b8a681b2d023b

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

module Lockdown
  module Helper
    #
    # If str_sym is a Symbol (:users), give me back "Users"
    # If str_sym is a String ("Users"), give me back :users
    #
    # Was :to_title_sym for String and :to_title_str for Symbol
    #
    def convert_reference_name(str_sym)
      if str_sym.is_a?(Symbol)
        titleize(str_sym)
      else
        underscore(str_sym).tr(' ','_').to_sym
      end
    end

		def string_name(str_sym)
			str_sym.is_a?(Symbol) ? convert_reference_name(str_sym) : str_sym
		end

		def symbol_name(str_sym)
			str_sym.is_a?(String) ? convert_reference_name(str_sym) : str_sym
		end

    def camelize(str)
      str.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
    end

    def random_string(len = 10)
      chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
      Array.new(len){||chars[rand(chars.size)]}.join
    end

		def administrator_group_string
			string_name(:administrators)
    end

		def administrator_group_symbol
			:administrators
    end

    private

    def titleize(str)
      humanize(underscore(str)).gsub(/\b([a-z])/) { $1.capitalize }
    end
    
    def humanize(str)
      str.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
    end

    def underscore(str)
      str.to_s.gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").downcase
    end

    if Lockdown.rails_app?
      def kontroller_class_name(str)
        "#{str}Controller"
      end
    else
      def kontroller_class_name(str)
        str
      end
    end
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lockdown-0.3.10 lib/lockdown/helper.rb
lockdown-0.3.13 lib/lockdown/helper.rb
lockdown-0.3.11 lib/lockdown/helper.rb
lockdown-0.3.12 lib/lockdown/helper.rb
lockdown-0.3.14 lib/lockdown/helper.rb
lockdown-0.3.8 lib/lockdown/helper.rb
lockdown-0.3.9 lib/lockdown/helper.rb
lockdown-0.3.15 lib/lockdown/helper.rb