Sha256: 0004c97382ca56d6260b0da481cefb46a499765a28190a26970bbbe24c1b410b

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Lipsiadmin
  module Ext
    # Ext configuration used by components
    #
    #   Generates: { name: 'name', handler: function(){ alert('Hello World') } }
    #
    class Configuration < Hash

      def initialize(hash)#:nodoc:
        hash.each { |k,v| self[k] = v }
      end

      # Returns the configuration as a string.
      # Optionally you can specify the indentation spaces.
      def to_s(indent=1)
        return if self.empty?
        i = (" "*indent)
        s = self.size > 0 ? "\n" : "  "
        r = "{#{s}" + self.collect { |k,v| "#{i*2}#{k}: #{s(v)}" if k != :var }.join(",#{s}") + "#{s}#{i if i.size > 1}}"
        return r =~ /\{\s*\}/ ? "" : r
      end

      private
        def javascript_object_for(object)
          case object
            when Configuration
              object.to_s(2)
            when Array
              "[" + object.collect { |o| s(o) }.join(",") + "]"
            else
              object.respond_to?(:to_json) ? object.to_json : object.inspect
          end
        end
        alias_method :s, :javascript_object_for
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lipsiadmin-5.1.9 lib/view/helpers/ext/configuration.rb
lipsiadmin-5.1.8 lib/view/helpers/ext/configuration.rb
lipsiadmin-5.1.7 lib/view/helpers/ext/configuration.rb