Permalink
Browse files

Have classes inherit from base

Also includes a new, sexy `register` method for registering
attributes. Attributes can be optional.
  • Loading branch information...
1 parent 8b7c225 commit 48202e65468f0647265aeb50017030b3a69efc04 Sebastian von Conrad committed Jan 9, 2012
Showing with 64 additions and 26 deletions.
  1. +1 −0 lib/in_bed_with.rb
  2. +2 −13 lib/in_bed_with/analytics.rb
  3. +37 −0 lib/in_bed_with/base.rb
  4. +2 −13 lib/in_bed_with/gauges.rb
  5. +22 −0 spec/in_bed_with/base_spec.rb
View
@@ -4,6 +4,7 @@
require 'active_support/core_ext'
require 'active_support/inflector'
+require 'in_bed_with/base'
require 'in_bed_with/analytics'
require 'in_bed_with/gauges'
@@ -1,20 +1,9 @@
module InBedWith
- class Analytics
- attr_accessor :property_id
-
- def initialize(options={})
- self.property_id = options[:property_id] || InBedWith.analytics_property_id
- end
+ class Analytics < Base
+ register :property_id
def code
- raise ArgumentError, property_id_missing unless property_id
-
"<script type=\"text/javascript\">var _gaq = _gaq || []; _gaq.push(['_setAccount', '#{property_id}']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();</script>"
end
-
- private
- def property_id_missing
- 'Please set a Google Analytics Property ID through config.analytics_property_id or by supplying :property_id when calling in_bed_with.analytics.'
- end
end
end
@@ -0,0 +1,37 @@
+module InBedWith
+ class Base
+ class << self
+ attr_reader :attributes
+
+ def register(attribute, options={})
+ InBedWith.mattr_accessor module_attr(attribute)
+ attr_accessor attribute
+
+ @attributes ||= {}
+ @attributes.merge!({ attribute.to_sym => options })
+ end
+
+ def module_attr(attribute)
+ "#{name.demodulize.underscore}_#{attribute}"
+ end
+ end
+
+ def initialize(attributes={})
+ attributes.symbolize_keys!
+
+ self.class.attributes.each do |attribute, options|
+ value = attributes[attribute] || InBedWith.send(self.class.module_attr(attribute))
+
+ raise ArgumentError, attribute_missing(attribute) unless value || options[:optional]
+
+ self.send "#{attribute}=", value
+ end
+ end
+
+ private
+ def attribute_missing(attribute)
+ full_name = attribute.to_s.camelize
+ "Please set a #{self.class.name.demodulize} #{full_name} through config.#{self.class.module_attr(attribute)} or by supplying :#{attribute} when calling in_bed_with.#{self.class.name.demodulize.underscore}."
+ end
+ end
+end
@@ -1,20 +1,9 @@
module InBedWith
- class Gauges
- attr_accessor :site_id
-
- def initialize(options={})
- self.site_id = options[:site_id] || InBedWith.gauges_site_id
- end
+ class Gauges < Base
+ register :site_id
def code
- raise ArgumentError, site_id_missing unless site_id
-
"<script type=\"text/javascript\">var _gauges = _gauges || []; (function() { var t = document.createElement('script'); t.type = 'text/javascript'; t.async = true; t.id = 'gauges-tracker'; t.setAttribute('data-site-id', '#{site_id}'); t.src = '//secure.gaug.es/track.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(t, s); })();</script>"
end
-
- private
- def site_id_missing
- 'Please set a Gauges Site ID through config.gauges_site_id or by supplying :site_id when calling in_bed_with.gauges.'
- end
end
end
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+module InBedWith
+ class Test < Base
+ register :foo
+ register :bar, :optional => true
+
+ def code
+ "foo_with_value_#{foo}"
+ end
+ end
+end
+
+describe InBedWith::Base do
+ it 'raises an exception if a required attribute is missing' do
+ lambda { InBedWith::Test.new }.should raise_exception ArgumentError, /config\.test_foo/
+ end
+
+ it 'does not raise an exception if an optional attribute is missing' do
+ InBedWith::Test.new(:foo => 'gazonk').code.should == 'foo_with_value_gazonk'
+ end
+end

0 comments on commit 48202e6

Please sign in to comment.