Extracted default value initializers.

This commit is contained in:
Dan Neumann 2013-02-08 01:56:12 -06:00
parent 54260fd766
commit 0cf68e2714
2 changed files with 41 additions and 12 deletions

View file

@ -9,10 +9,21 @@ class DiscoursePluginRegistry
attr_accessor :stylesheets attr_accessor :stylesheets
end end
def register_js(filename, options={}) # Default accessor values
self.class.javascripts ||= Set.new #
self.class.server_side_javascripts ||= Set.new def self.stylesheets
@stylesheets ||= Set.new
end
def self.javascripts
@javascripts ||= Set.new
end
def self.server_side_javascripts
@server_side_javascripts ||= Set.new
end
def register_js(filename, options={})
# If we have a server side option, add that too. # If we have a server side option, add that too.
self.class.server_side_javascripts << options[:server_side] if options[:server_side].present? self.class.server_side_javascripts << options[:server_side] if options[:server_side].present?
@ -20,12 +31,11 @@ class DiscoursePluginRegistry
end end
def register_css(filename) def register_css(filename)
self.class.stylesheets ||= Set.new
self.class.stylesheets << filename self.class.stylesheets << filename
end end
def stylesheets def stylesheets
self.class.stylesheets || Set.new self.class.stylesheets
end end
def register_archetype(name, options={}) def register_archetype(name, options={})
@ -33,17 +43,17 @@ class DiscoursePluginRegistry
end end
def server_side_javascripts def server_side_javascripts
self.class.javascripts || Set.new self.class.javascripts
end end
def javascripts def javascripts
self.class.javascripts || Set.new self.class.javascripts
end end
def self.clear def self.clear
self.stylesheets = Set.new self.stylesheets = nil
self.server_side_javascripts = Set.new self.server_side_javascripts = nil
self.javascripts = Set.new self.javascripts = nil
end end
def self.setup(plugin_class) def self.setup(plugin_class)
@ -52,6 +62,4 @@ class DiscoursePluginRegistry
plugin.setup plugin.setup
end end
end end

View file

@ -4,6 +4,27 @@ require 'discourse_plugin_registry'
describe DiscoursePluginRegistry do describe DiscoursePluginRegistry do
let(:registry) { DiscoursePluginRegistry.new } let(:registry) { DiscoursePluginRegistry.new }
context '#stylesheets' do
it 'defaults to an empty Set' do
DiscoursePluginRegistry.stylesheets = nil
DiscoursePluginRegistry.stylesheets.should == Set.new
end
end
context '#javascripts' do
it 'defaults to an empty Set' do
DiscoursePluginRegistry.javascripts = nil
DiscoursePluginRegistry.javascripts.should == Set.new
end
end
context '#server_side_javascripts' do
it 'defaults to an empty Set' do
DiscoursePluginRegistry.server_side_javascripts = nil
DiscoursePluginRegistry.server_side_javascripts.should == Set.new
end
end
context '.register_css' do context '.register_css' do
before do before do