Class: Alula::Plugin

Inherits:
Object
  • Object
show all
Defined in:
lib/alula/plugin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.addon(type, content_or_block) ⇒ Object



26
27
28
# File 'lib/alula/plugin.rb', line 26

def self.addon(type, content_or_block)
  addons[type] << content_or_block
end

.addonsObject



22
23
24
# File 'lib/alula/plugin.rb', line 22

def self.addons
  @@addons ||= Hash.new {|hash, key| hash[key] = []}
end

.cookieconsent?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/alula/plugin.rb', line 60

def self.cookieconsent?(name = nil)
  if name.nil?
    @@cookieconsent.kind_of?(Hash)
  else
    @@cookieconsent.kind_of?(Hash) and @@cookieconsent.key?(name)
  end
end

.cookieconsent_typesObject



73
74
75
76
77
78
79
80
# File 'lib/alula/plugin.rb', line 73

def self.cookieconsent_types
  @@cookieconsented ||= {}
  if @@cookieconsented.kind_of?(Hash)
    @@cookieconsented.keys
  else
    []
  end
end

.cookieconsented(name) ⇒ Object



68
69
70
71
# File 'lib/alula/plugin.rb', line 68

def self.cookieconsented(name)
  @@cookieconsented ||= {}
  @@cookieconsented[name] = true
end

.load(name, options) ⇒ Object



15
16
17
18
19
20
# File 'lib/alula/plugin.rb', line 15

def self.load(name, options)
  if plugins[name] and !(!!options == options and !options)
    plugin = plugins[name]
    return plugin if plugin.install(options || Hashie::Mash.new)
  end
end

.needs_cookieconsent(type = 'analytics') ⇒ Object



53
54
55
56
57
58
# File 'lib/alula/plugin.rb', line 53

def self.needs_cookieconsent(type = 'analytics')
  name = caller_name
  
  @@cookieconsent ||= {}
  @@cookieconsent[name] = type
end

.pluginsObject



7
8
9
# File 'lib/alula/plugin.rb', line 7

def self.plugins
  @@plugins ||= {}
end

.prepend_addon(type, content_or_block) ⇒ Object



30
31
32
# File 'lib/alula/plugin.rb', line 30

def self.prepend_addon(type, content_or_block)
  addons[type].unshift content_or_block
end

.register(name, klass) ⇒ Object



3
4
5
# File 'lib/alula/plugin.rb', line 3

def self.register(name, klass)
  plugins[name.to_s] = klass
end

.script(placement, content_or_block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/alula/plugin.rb', line 34

def self.script(placement, content_or_block)
  name = caller_name
  
  if self.cookieconsent?(name)
    self.cookieconsented(name)
  end
  
  script = <<-EOS
  <script type="#{self.cookieconsent?(name) ? "text/plain" : "text/javascript"}" #{self.cookieconsent?(name) ? "class=\"cc-onconsent-#{@@cookieconsent[name].to_s}\"" : ""}>
  EOS
  if content_or_block.kind_of?(Proc)
    scpt = ->(context) { script + content_or_block.call(context) + "</script>" }
  else
    scpt = script + content_or_block + "</script>"
  end

  self.addon(placement, scpt)
end

.script_load_modeObject



91
92
93
# File 'lib/alula/plugin.rb', line 91

def self.script_load_mode
  @@script_load_mode ||= :async
end

.script_load_mode=(mode) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/alula/plugin.rb', line 82

def self.script_load_mode=(mode)
  @@script_load_mode = case mode
  when :sync
    :sync
  when :defer
    self.script_load_mode == :sync ? :sync : :defer
  end
end

Instance Method Details

#pluginsObject



11
12
13
# File 'lib/alula/plugin.rb', line 11

def plugins
  self.class.plugins
end