Sha256: 49270e96f77a8e7e4545eb2970117eb93c01cb1db0c50e9b8d84173aa1b4d560

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

module Alula
  class Plugin
    def self.register(name, klass); plugins[name.to_s] = klass; end
    def self.plugins; @@plugins ||= {}; end
    def plugins; self.class.plugins; end
    
    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
    
    def self.addons
      @@addons ||= Hash.new {|hash, key| hash[key] = []}
    end
    
    def self.addon(type, content_or_block)
      addons[type] << content_or_block
    end
    
    def self.prepend_addon(type, content_or_block)
      addons[type].unshift content_or_block
    end
    
    def self.script(type, content_or_block)
      script = <<-EOS
      <script type="#{self.cookieconsent? ? "text/plain" : "text/javascript"}" #{self.cookieconsent? ? "style=\"cc-onconsent-analytics\"" : ""}>
      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
      
      addons[type] << scpt
    end
    
    def self.needs_cookieconsent
      @@cookieconsent = true
    end
    
    def self.cookieconsent?
      @@cookieconsent == true
    end
    
    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
    
    def self.script_load_mode
      @@script_load_mode ||= :async
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alula-0.4.18 lib/alula/plugin.rb
alula-0.4.17 lib/alula/plugin.rb