Module: Condenser::Pipeline

Included in:
Condenser
Defined in:
lib/condenser/pipeline.rb

Constant Summary collapse

CONFIG_VARS =
%w(mime_types extensions templates preprocessors transformers postprocessors minifiers exporters writers).map(&:to_sym)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/condenser/pipeline.rb', line 6

def self.prepended(base)
  base.extend(self)
  CONFIG_VARS.each do |var|
    base.instance_variable_set("@#{var}", {})
    base.send(:attr_reader, var)
    self.send(:attr_reader, var)
  end
end

Instance Method Details

#clear_pipelineObject



149
150
151
# File 'lib/condenser/pipeline.rb', line 149

def clear_pipeline
  (CONFIG_VARS - [:mime_types, :extensions]).each { |v| self.instance_variable_set("@#{v}", {}) }
end

#initialize(*args, **kws, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/condenser/pipeline.rb', line 15

def initialize(*args, **kws, &block)
  CONFIG_VARS.each do |var_name|
    original_var = self.class.instance_variable_get("@#{var_name}")
    new_var = original_var.dup
    
    if original_var.is_a?(Hash)
      original_var.each do |k, v|
        new_var[k] = v.dup if v.is_a?(Hash) || v.is_a?(Array)
      end
    end

    instance_variable_set("@#{var_name}", new_var)
  end
  super
end

#minifier_for(mime_type) ⇒ Object



117
118
119
# File 'lib/condenser/pipeline.rb', line 117

def minifier_for(mime_type)
  @minifiers[mime_type]
end

#pipline_digestObject



62
63
64
# File 'lib/condenser/pipeline.rb', line 62

def pipline_digest
  @pipline_digest ||= Digest::MD5.hexdigest(pipline_hash)
end

#pipline_hashObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/condenser/pipeline.rb', line 51

def pipline_hash
  JSON.generate(pipline_to_json([
    @templates,
    @preprocessors,
    @transformers,
    @postprocessors,
    @minifiers,
    @exporters
  ]))
end

#pipline_to_json(values) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/condenser/pipeline.rb', line 31

def pipline_to_json(values)
  if values.is_a?(Hash)
    values.transform_values do |value|
      pipline_to_json(value)
    end
  elsif values.is_a?(Array)
    values.map do |value|
      pipline_to_json(value)
    end
  elsif values.is_a?(Class) || values.is_a?(Module)
    values.name
  elsif values.is_a?(String)
    @base ? values.delete_prefix(@base) : values
  elsif values.nil? || values == true || values == false || values.is_a?(Symbol) || values.is_a?(Integer) || values.is_a?(Float)
    values
  else
    { values.class.name => pipline_to_json(values.options) }
  end
end

#register_exporter(mime_type, engine) ⇒ Object



100
101
102
103
# File 'lib/condenser/pipeline.rb', line 100

def register_exporter(mime_type, engine)
  @exporters[mime_type] ||= []
  @exporters[mime_type] << engine
end

#register_mime_type(mime_type, extensions: nil, extension: nil, charset: :default) ⇒ Object



66
67
68
69
70
71
# File 'lib/condenser/pipeline.rb', line 66

def register_mime_type(mime_type, extensions: nil, extension: nil, charset: :default)
  extensions = Array(extensions || extension)

  @mime_types[mime_type] = { extensions: extensions, charset: charset }
  extensions.each { |ext| @extensions[ext] = mime_type }
end

#register_minifier(mime_type, engine) ⇒ Object



113
114
115
# File 'lib/condenser/pipeline.rb', line 113

def register_minifier(mime_type, engine)
  @minifiers[mime_type] = engine
end

#register_postprocessor(mime_type, engine) ⇒ Object



95
96
97
98
# File 'lib/condenser/pipeline.rb', line 95

def register_postprocessor(mime_type, engine)
  @postprocessors[mime_type] ||= []
  @postprocessors[mime_type] << engine
end

#register_preprocessor(mime_type, engine) ⇒ Object



77
78
79
80
# File 'lib/condenser/pipeline.rb', line 77

def register_preprocessor(mime_type, engine)
  @preprocessors[mime_type] ||= []
  @preprocessors[mime_type] << engine
end

#register_template(mime_type, engine) ⇒ Object



73
74
75
# File 'lib/condenser/pipeline.rb', line 73

def register_template(mime_type, engine)
  @templates[mime_type] = engine
end

#register_transformer(from_mime_type, to_mime_type, engine) ⇒ Object



90
91
92
93
# File 'lib/condenser/pipeline.rb', line 90

def register_transformer(from_mime_type, to_mime_type, engine)
  @transformers[from_mime_type] ||= {}
  @transformers[from_mime_type][to_mime_type] = engine
end

#register_writer(engine) ⇒ Object



125
126
127
128
129
130
# File 'lib/condenser/pipeline.rb', line 125

def register_writer(engine)
  Array(engine.mime_types).each do |mime_type|
    @writers[mime_type] ||= []
    @writers[mime_type] << engine
  end
end

#unregister_exporter(mime_type, engine = nil) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/condenser/pipeline.rb', line 105

def unregister_exporter(mime_type, engine=nil)
  if engine.nil?
    @exporters[mime_type].clear
  else
    @exporters[mime_type]&.reject! { |e| e == engine || e.is_a?(engine) }
  end
end

#unregister_minifier(mime_type) ⇒ Object



121
122
123
# File 'lib/condenser/pipeline.rb', line 121

def unregister_minifier(mime_type)
  @minifiers[mime_type] = nil
end

#unregister_preprocessor(mime_type, engine = nil) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/condenser/pipeline.rb', line 82

def unregister_preprocessor(mime_type, engine=nil)
  if engine.nil?
    @preprocessors[mime_type].clear
  else
    @preprocessors[mime_type]&.reject! { |e| e == engine || e.is_a?(engine) }
  end
end

#unregister_writer(engine, for_mime_types: []) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/condenser/pipeline.rb', line 132

def unregister_writer(engine, for_mime_types: [])
  mime_types = @writers.keys if mime_types.nil?
  Array(for_mime_types || @writers.keys).each do |mime_type|
    @writers[mime_type].select! do |writer|
      if engine.nil? || engine == writer[0] || writer[0].is_a?(engine)
        if added_mime_types.nil? || added_mime_types == writer[1]
          false
        else
          true
        end
      else
        true
      end
    end
  end
end