Memoize readers for plugins to avoid nil checks

This commit is contained in:
Robin Ward 2015-02-04 12:59:18 -05:00
parent f9695882b1
commit 530b20d339
1 changed files with 29 additions and 34 deletions

View File

@ -5,9 +5,17 @@ require_dependency 'plugin/auth_provider'
class Plugin::Instance
attr_reader :auth_providers, :assets, :styles, :color_schemes
attr_accessor :path, :metadata
# Memoized array readers
[:assets, :auth_providers, :color_schemes, :initializers, :javascripts, :styles].each do |att|
class_eval %Q{
def #{att}
@#{att} ||= []
end
}
end
def self.find_all(parent_path)
[].tap { |plugins|
# also follows symlinks - http://stackoverflow.com/q/357754
@ -22,8 +30,6 @@ class Plugin::Instance
def initialize(metadata=nil, path=nil)
@metadata = metadata
@path = path
@assets = []
@color_schemes = []
if @path
# Automatically include all ES6 JS and hbs files
@ -86,8 +92,7 @@ class Plugin::Instance
end
def after_initialize(&block)
@after_initialize ||= []
@after_initialize << block
initializers << block
end
def notify_after_initialize
@ -95,12 +100,10 @@ class Plugin::Instance
ColorScheme.create_from_base(name: c[:name], colors: c[:colors]) unless ColorScheme.where(name: c[:name]).exists?
end
if @after_initialize
@after_initialize.each do |callback|
initializers.each do |callback|
callback.call
end
end
end
def listen_for(event_name)
return unless self.respond_to?(event_name)
@ -108,13 +111,11 @@ class Plugin::Instance
end
def register_css(style)
@styles ||= []
@styles << style
styles << style
end
def register_javascript(js)
@javascripts ||= []
@javascripts << js
javascripts << js
end
def register_custom_html(hash)
@ -132,13 +133,9 @@ class Plugin::Instance
end
def automatic_assets
css = ""
js = ""
css = styles.join("\n")
js = javascripts.join("\n")
css = @styles.join("\n") if @styles
js = @javascripts.join("\n") if @javascripts
unless auth_providers.blank?
auth_providers.each do |auth|
overrides = ""
overrides = ", titleOverride: '#{auth.title}'" if auth.title
@ -156,7 +153,6 @@ class Plugin::Instance
css << ".btn-social.#{auth.name}{ background: #{auth.background_color}; }\n"
end
end
end
# Generate an IIFE for the JS
js = "(function(){#{js}})();" if js.present?
@ -201,12 +197,11 @@ class Plugin::Instance
def auth_provider(opts)
@auth_providers ||= []
provider = Plugin::AuthProvider.new
[:glyph, :background_color, :title, :message, :frame_width, :frame_height, :authenticator].each do |sym|
provider.send "#{sym}=", opts.delete(sym)
end
@auth_providers << provider
auth_providers << provider
end