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

View File

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