class Parcels::Fragments::CssFragment

Attributes

css_string[R]
file[R]
line[R]
options[R]
source[R]

Public Class Methods

new(css_string, source, file, line, options) click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 13
def initialize(css_string, source, file, line, options)
  options.assert_valid_keys(:engines, :wrap, :prefix)

  @css_string = css_string
  @source = source
  @file = file
  @line = line
  @options = options
end
to_css(parcels_environment, context, fragments) click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 5
def to_css(parcels_environment, context, fragments)
  fragments = Array(fragments)
  fragments.map { |f| f.to_css(parcels_environment, context) }.join("\n")
end

Public Instance Methods

to_css(parcels_environment, context) click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 31
    def to_css(parcels_environment, context)
      scss = css_string

      if wrapped? && (wrapper_css_class = source.try(:_parcels_widget_outer_element_class))
        scss = %{.#{wrapper_css_class} {
  #{scss}
}}
      end

      if options[:prefix]
        if options[:prefix].kind_of?(String)
          scss = "#{options[:prefix]}\n\n#{scss}"
        else
          raise "You supplied a css_prefix (or a :prefix option) that wasn't a String, but, rather: #{options[:prefix].inspect}"
        end
      end

      asset_attributes = ::Sprockets::AssetAttributes.new(parcels_environment.sprockets_environment, synthetic_filename)
      processors = asset_attributes.processors
      out = process_with_processors(processors, context, scss)

      header_comment + out
    end
to_s() click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 27
def to_s
  "<#{self.class.name.demodulize}: from '#{file}', line #{line}, options #{options.inspect}>"
end
wrapping_css_class_required?() click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 23
def wrapping_css_class_required?
  wrapped?
end

Private Instance Methods

engines_as_extensions() click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 80
def engines_as_extensions
  @engines_as_extensions ||= begin
    engines = options[:engines] || [ ]
    out = Array(engines).flatten.map do |component|
      component = component.to_s
      component = ".#{component}" unless component =~ /^\./
      component
    end.join(".")
    ".#{out}".gsub(/\.\.+/, '.')
  end
end
header_comment() click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 58
def header_comment
  out = "/* From '#{file}'"
  out << ":#{line}" if line
  out << " */\n"
  out
end
process_with_processors(processors, context, data) click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 92
def process_with_processors(processors, context, data)
  result = data

  processors.each do |processor|
    template = processor.new(file, 1, { :load_paths => context.environment.paths, :filename => file }) { result }
    result = template.render(context, { :filename => file })
  end

  result
end
synthetic_filename() click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 69
def synthetic_filename
  @synthetic_filename ||= begin
    synthetic_name = File.basename(file)
    synthetic_name = $1 if synthetic_name =~ /^([^\.]+)\./i
    synthetic_name << ".css.scss"
    synthetic_name << engines_as_extensions

    File.join(File.dirname(file), synthetic_name)
  end
end
wrapped?() click to toggle source
# File lib/parcels/fragments/css_fragment.rb, line 65
def wrapped?
  options.fetch(:wrap, true)
end