Class: Beso::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/beso/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, options) ⇒ Job

Returns a new instance of Job.



3
4
5
6
7
8
9
10
11
# File 'lib/beso/job.rb', line 3

def initialize( event, options )
  @event = event.to_sym
  @title = options.delete( :event ) || @event.to_s.titleize
  @table = options.delete( :table )
  @since = options.delete( :since )
  @scope = options.delete( :scope ) || lambda { self }
  @props = { }
  @extra = options
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



12
13
14
# File 'lib/beso/job.rb', line 12

def event
  @event
end

Instance Method Details

#first_timestampObject



49
50
51
# File 'lib/beso/job.rb', line 49

def first_timestamp
  model_class.minimum @timestamp
end

#identity(value = nil, &block) ⇒ Object



14
15
16
# File 'lib/beso/job.rb', line 14

def identity( value=nil, &block )
  @identity = value || block
end

#last_timestampObject



53
54
55
# File 'lib/beso/job.rb', line 53

def last_timestamp
  model_class.maximum @timestamp
end

#prop(name, value = nil, &block) ⇒ Object



23
24
25
26
# File 'lib/beso/job.rb', line 23

def prop( name, value=nil, &block )
  raise TooManyPropertiesError if @props.length == 10
  @props[ name.to_sym ] = value || block || name.to_sym
end

#timestamp(value) ⇒ Object



18
19
20
21
# File 'lib/beso/job.rb', line 18

def timestamp( value )
  raise InvalidTimestampError unless value.is_a?(Symbol)
  @timestamp = value
end

#to_csv(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/beso/job.rb', line 28

def to_csv( options={} )
  raise MissingIdentityError  if @identity.nil?
  raise MissingTimestampError if @timestamp.nil?

  @since ||= options.delete( :since ) || first_timestamp

  condition = "#{@table}.#{@timestamp} >= ?"

  relation = model_class.instance_exec( &@scope ).where( condition, @since )

  return nil if relation.empty?

  Beso::CSV.generate( @extra.merge( options ) ) do |csv|
    csv << ( required_headers + custom_headers )

    relation.each do |model|
      csv << ( required_columns( model ) + custom_columns( model ) )
    end
  end
end