Sha256: 8a29ca0fded170a4fbc4db0c9a7683607f55cbc33bffcb58adf3285d26a9ed0e

Contents?: true

Size: 1.71 KB

Versions: 39

Compression:

Stored size: 1.71 KB

Contents

require 'hamlit/attribute'
require 'hamlit/concerns/string_interpolation'

# This module compiles :runtime sexp. It is a special version of
# old-style attribute which is built on runtime.
module Hamlit
  module Compilers
    module RuntimeAttribute
      include Concerns::StringInterpolation

      # This is used for compiling only runtime attribute in Compilers::Attribute.
      def on_runtime(str)
        compile_runtime_attribute(str)
      end

      # Given html attrs, merge classes and ids to :dynamic_attribute.
      def merge_runtime_attributes(attrs)
        merge_targets = filter_attrs(attrs, 'id', 'class')
        dynamic_attr  = attrs.find { |exp, *args| exp == :runtime }

        attrs -= merge_targets
        attrs.delete(dynamic_attr)

        base = decompile_temple_attrs(merge_targets)
        [compile_runtime_attribute(dynamic_attr.last, base), *attrs]
      end

      private

      def compile_runtime_attribute(str, base = nil)
        str = str.gsub(/(\A\{|\}\Z)/, '')
        quote = options[:attr_quote].inspect
        code = "::Hamlit::Attribute.build(#{[quote, base, str].compact.join(', ')})"
        [:dynamic, code]
      end

      def has_runtime_attribute?(attrs)
        attrs.any? do |exp, *args|
          exp == :runtime
        end
      end

      # Given attrs in temple ast, return an attribute as hash literal.
      def decompile_temple_attrs(attrs)
        literal = '{'
        attrs.each do |html, attr, name, (type, value)|
          case type
          when :static
            literal += ":#{name}=>#{string_literal(value)},"
          when :dynamic
            literal += ":#{name}=>#{value},"
          end
        end
        literal.gsub(/,\Z/, '') + '}'
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
hamlit-1.7.2 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.7.1 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.7.0 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.6.7 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.6.6 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.6.5 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.6.4 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.6.3 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.6.2 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.6.1 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.6.0 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.9 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.8 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.7 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.6 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.5 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.4 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.3 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.2 lib/hamlit/compilers/runtime_attribute.rb
hamlit-1.5.1 lib/hamlit/compilers/runtime_attribute.rb