Sha256: b1b25c5e9a6f25882e54bdd8240e208e0810288414e2f6b649cc46463174df58

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'ostruct'

module CommaHeaven
  class Export
    module Implementation
      def export(options = {})
        Export.new(self, scope(:find), options)
      end
    end
    
    attr_accessor :klass, :current_scope, :options, :export, :limit, :by, :col_sep, :format
    undef :id if respond_to?(:id)
  
    def initialize(klass, current_scope, options = {})
      self.klass = klass
      self.current_scope = current_scope
      self.options = options || {}
      
      self.options.symbolize_keys!
      
      self.export = self.options[:export] || {}
      
      self.export.symbolize_keys!
      
      self.limit = self.options[:limit]
    end
    
    def save(options = {})
      all_options = self.options.merge(options)
      
      csv_options = all_options.slice(*FasterCSV::DEFAULT_OPTIONS.keys)
      tch_options = all_options.except(*FasterCSV::DEFAULT_OPTIONS.keys) # TCH means To Comma Heaven
      
      ids = klass.scoped(current_scope).find(:all, :select => 'id').map(&:id)
      klass.scoped(:conditions => ['id IN (?)', ids]).to_comma_heaven(tch_options.symbolize_keys).to_csv(csv_options.symbolize_keys)
    end

    private
      def method_missing(name, *args, &block)
        case 
        when column_name?(name)
          return OpenStruct.new(export[name].values.first) rescue OpenStruct.new({})
        when association_name?(name)
          return self.class.new(klass.reflect_on_association(name).klass, {}, export[name].values.first) rescue self.class.new(klass.reflect_on_association(name).klass, {}, {})
        else
          return super
        end
      end
      
      def column_name?(value)
        klass.column_names.include?(value.to_s)
      end
      
      def association_name?(value)
        klass.reflect_on_all_associations.map(&:name).include?(value.to_sym)
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comma-heaven-0.7.1 lib/comma-heaven/export.rb