Class: CommaHeaven::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/comma-heaven/export.rb

Defined Under Namespace

Modules: Implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, current_scope, options = {}) ⇒ Export

Returns a new instance of Export.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/comma-heaven/export.rb', line 34

def initialize(klass, current_scope, options = {})
  self.klass = klass
  self.current_scope = current_scope
  self.options = options || {}
  
  self.options.symbolize_keys! if self.options.respond_to?(:symbolize_keys!)
  
  self.export = self.options[:export] || {}
  
  self.export.symbolize_keys! if self.export.respond_to?(:symbolize_keys!)
  
  self.limit = self.options[:limit]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



63
64
65
66
67
68
69
70
71
72
# File 'lib/comma-heaven/export.rb', line 63

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

Instance Attribute Details

#byObject

Returns the value of attribute by.



31
32
33
# File 'lib/comma-heaven/export.rb', line 31

def by
  @by
end

#col_sepObject

Returns the value of attribute col_sep.



31
32
33
# File 'lib/comma-heaven/export.rb', line 31

def col_sep
  @col_sep
end

#current_scopeObject

Returns the value of attribute current_scope.



31
32
33
# File 'lib/comma-heaven/export.rb', line 31

def current_scope
  @current_scope
end

#exportObject

Returns the value of attribute export.



31
32
33
# File 'lib/comma-heaven/export.rb', line 31

def export
  @export
end

#formatObject

Returns the value of attribute format.



31
32
33
# File 'lib/comma-heaven/export.rb', line 31

def format
  @format
end

#klassObject

Returns the value of attribute klass.



31
32
33
# File 'lib/comma-heaven/export.rb', line 31

def klass
  @klass
end

#limitObject

Returns the value of attribute limit.



31
32
33
# File 'lib/comma-heaven/export.rb', line 31

def limit
  @limit
end

#optionsObject

Returns the value of attribute options.



31
32
33
# File 'lib/comma-heaven/export.rb', line 31

def options
  @options
end

Instance Method Details

#save(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/comma-heaven/export.rb', line 48

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

  tch_options = tch_options.symbolize_keys if tch_options.respond_to?(:symbolize_keys!)
  csv_options = csv_options.symbolize_keys if csv_options.respond_to?(:symbolize_keys!)
  
  current_scope
    .to_comma_heaven(tch_options)
    .to_csv(csv_options)
end