Permalink
Browse files

Support literals intstead of procs for job values

  • Loading branch information...
1 parent 5c83759 commit 01be3209ff7b9bc443e8943251f52f9b782517f8 @jeremyruppel jeremyruppel committed Apr 12, 2012
Showing with 28 additions and 9 deletions.
  1. +13 −9 lib/beso/job.rb
  2. +15 −0 spec/beso/job_spec.rb
View
@@ -6,16 +6,16 @@ def initialize( event, options )
@props = { }
end
- def identity( &block )
- @identity = block
+ def identity( value=nil, &block )
+ @identity = value || block
end
- def timestamp( &block )
- @timestamp = block
+ def timestamp( value=nil, &block )
+ @timestamp = value || block
end
- def prop( name, &block )
- @props[ name.to_sym ] = block
+ def prop( name, value=nil, &block )
+ @props[ name.to_sym ] = value || block
end
def to_csv( options={} )
@@ -43,14 +43,14 @@ def custom_headers
def required_columns( model )
[ ].tap do |row|
- row << @identity.call( model )
- row << @timestamp.call( model ).to_i
+ row << block_or_value( @identity, model )
+ row << block_or_value( @timestamp, model ).to_i
row << event_title
end
end
def custom_columns( model )
- @props.values.map { |block| block.call( model ) }
+ @props.values.map { |value| block_or_value( value, model ) }
end
def event_title
@@ -60,5 +60,9 @@ def event_title
def model_class
@table.to_s.classify.constantize
end
+
+ def block_or_value( value, model )
+ value.respond_to?( :call ) ? value.call( model ) : value
+ end
end
end
View
@@ -59,6 +59,21 @@
) }
end
+ context 'with literal properties defined' do
+ before do
+ subject.identity 22
+ subject.timestamp 1234
+ subject.prop :foo, 'bar'
+ end
+
+ its( :to_csv ){ should eq( <<-EOS
+Identity,Timestamp,Event,Prop:Foo
+22,1234,Message Sent,bar
+22,1234,Message Sent,bar
+ EOS
+ ) }
+ end
+
context 'with custom options' do
before do
subject.identity { |user| user.id }

0 comments on commit 01be320

Please sign in to comment.