Sha256: 52a874d34d8499c222221b6d9f1d4341fb922cb1a5e80850478b22c62c840eef

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'comma'

module Beso
  class Job
    def initialize( name, options )
      @name  = name.to_sym
      @table = options.delete :table
      @props = { }
    end

    def prop( sym, *args, &block )
      # TODO this is weak, find a better way
      # to do what you mean to do
      args[ 0 ] = "Prop:#{args[ 0 ] || sym.to_s.titleize}"

      @props[ sym.to_sym ] = [ args, block ]
    end

    def to_csv
      model_class.class_exec( event_title ) do |event|
        define_method( :event_title ) { event }
      end

      model_class.instance_exec( @name, @props ) do |name, props|
        comma name do
          id          'Identity'
          created_at  'Timestamp' do |m|
            m.to_i
          end
          event_title 'Event'

          props.each do |sym, (args, block)|
            self.send sym, *args, &block
          end
        end
      end

      model_class.all.to_comma @name
    end

    protected

    def event_title
      @name.to_s.titleize
    end

    def model_class
      @table.to_s.classify.constantize
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
beso-0.0.1 lib/beso/job.rb