Class: Staticz::CSSBundle

Inherits:
Object
  • Object
show all
Defined in:
lib/manifest/css_bundle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, location) ⇒ CSSBundle

Returns a new instance of CSSBundle.



7
8
9
10
11
# File 'lib/manifest/css_bundle.rb', line 7

def initialize(name, location)
  @name = name
  @location = location
  @elements = []
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



5
6
7
# File 'lib/manifest/css_bundle.rb', line 5

def elements
  @elements
end

#locationObject (readonly)

Returns the value of attribute location.



5
6
7
# File 'lib/manifest/css_bundle.rb', line 5

def location
  @location
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/manifest/css_bundle.rb', line 5

def name
  @name
end

Instance Method Details

#build(listener_class: nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/manifest/css_bundle.rb', line 35

def build(listener_class: nil)
  listener = listener_class&.new(self)

  File.write "build/#{name}_bundle.css", render

  listener&.finish
end


66
67
68
69
70
71
72
73
74
# File 'lib/manifest/css_bundle.rb', line 66

def create_link_function
  link_path = "/#{name}_bundle.css"

  Object.send(:define_method, path_method_name) do
    link_path
  end

  Manifest.instance.functions << path_method_name
end

#generate_location_path(name) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/manifest/css_bundle.rb', line 27

def generate_location_path(name)
  if location.empty?
    name
  else
    "#{location}/#{name}"
  end
end

#pathObject



89
90
91
# File 'lib/manifest/css_bundle.rb', line 89

def path
  "src/#{name}"
end

#path_method_nameObject



62
63
64
# File 'lib/manifest/css_bundle.rb', line 62

def path_method_name
  "#{name.to_s.gsub(/[.\/-]/, "_")}_css_bundle_path"
end


82
83
84
85
86
87
# File 'lib/manifest/css_bundle.rb', line 82

def print(indentation)
  puts "#{" " * (indentation * 3)}└─ CSSBundle: #{name} -> #{path_method_name}"
  elements.each do |e|
    e.print(indentation + 1, :no_path)
  end
end

#renderObject



43
44
45
# File 'lib/manifest/css_bundle.rb', line 43

def render
  render_elements elements
end

#render_elements(elements) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/manifest/css_bundle.rb', line 47

def render_elements(elements)
  elements
    .map do |element|
      if element.is_a? Staticz::Sub
        render_elements element.elements
      else
        [
          "/* #{element.name} */",
          element.render
        ].join("\n\n")
      end
    end
    .join("\n\n")
end

#sass(name, &block) ⇒ Object



19
20
21
# File 'lib/manifest/css_bundle.rb', line 19

def sass(name, &block)
  elements.push(Staticz::Compilable::Scss.new(generate_location_path(name), type: :sass))
end

#scss(name, &block) ⇒ Object



23
24
25
# File 'lib/manifest/css_bundle.rb', line 23

def scss(name, &block)
  elements.push(Staticz::Compilable::Scss.new(generate_location_path(name), type: :scss))
end

#sub(name, &block) ⇒ Object



13
14
15
16
17
# File 'lib/manifest/css_bundle.rb', line 13

def sub(name, &block)
  s = Staticz::Sub.new(generate_location_path(name))
  elements.push(s)
  s.instance_eval(&block)
end

#valid?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/manifest/css_bundle.rb', line 76

def valid?
  elements.map do |e|
    e.valid?
  end
end