Sha256: 1bd49b4b14dd62173856ee9f246a5bc3fb0a98765d7099eba580a6e6a34a3c1b

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module ExpressTemplates
  module Components
    # wrap locals and helpers for evaluation during render
    class Wrapper

      attr_accessor :name, :args

      def initialize(name, *args)
        @name = name
        @args = args
      end

      def compile
        # insure nils do not blow up view
        %Q("\#\{#{_compile}\}")
      end

      def to_template
        _compile
      end

      def children
        []
      end

      private
        def _compile
          if @args.empty?
            return name
          else
            args_string = args.slice(0..-2).map(&:inspect).join(', ')
            last_arg = ''
            if args.last.is_a?(Hash) # expand a hash
              unless args.last.empty?
                # This approach has limitations - will only work on structures of
                # immediate values
                last_arg = args.last.inspect.match(/^\{(.*)\}$/)[1]
                last_arg.gsub!(/:(\w+)=>/, '\1: ') # use ruby 2 hash syntax
              else
                last_arg = "{}" # empty hash
              end
            else
              last_arg = args.last.inspect
            end
            args_string << (args_string.empty? ? last_arg : ", #{last_arg}")
            return "#{name}(#{args_string})"
          end
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
express_templates-0.2.0 lib/express_templates/components/wrapper.rb