Class: ActiveExport::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_export/base.rb

Direct Known Subclasses

Csv, Xml, Yaml

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_name, namespace, options = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
# File 'lib/active_export/base.rb', line 10

def initialize(source_name, namespace, options = {})
  @source_name = source_name.to_sym
  @namespace = namespace.to_sym
  @config = ::ActiveExport.configuration
  @label_keys = options.has_key?(:label_keys) ? options.delete(:label_keys) : nil
  @eval_methods = options.has_key?(:eval_methods) ? options.delete(:eval_methods) : nil
  @label_prefix = options.has_key?(:label_prefix) ? options.delete(:label_prefix) : nil
  @options = options
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/active_export/base.rb', line 9

def config
  @config
end

#eval_methodsObject

Returns the value of attribute eval_methods.



8
9
10
# File 'lib/active_export/base.rb', line 8

def eval_methods
  @eval_methods
end

#label_keysObject

Returns the value of attribute label_keys.



8
9
10
# File 'lib/active_export/base.rb', line 8

def label_keys
  @label_keys
end

#label_prefixObject

Returns the value of attribute label_prefix.



8
9
10
# File 'lib/active_export/base.rb', line 8

def label_prefix
  @label_prefix
end

#namespaceObject

Returns the value of attribute namespace.



8
9
10
# File 'lib/active_export/base.rb', line 8

def namespace
  @namespace
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/active_export/base.rb', line 8

def options
  @options
end

#sourceObject

Returns the value of attribute source.



8
9
10
# File 'lib/active_export/base.rb', line 8

def source
  @source
end

#source_nameObject

Returns the value of attribute source_name.



8
9
10
# File 'lib/active_export/base.rb', line 8

def source_name
  @source_name
end

Class Method Details

.build_label_keys_and_eval_methods(methods) ⇒ Array

Parameters:

  • methods (Array)

Returns:

  • (Array)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/active_export/base.rb', line 57

def build_label_keys_and_eval_methods(methods)
  label_keys = []
  eval_methods = []
  methods.each do |f|
    if f.is_a?(Hash)
      label_keys << f.keys.first
      eval_methods << f.values.first
    else
      label_keys << f
      eval_methods << f
    end
  end
  return label_keys, eval_methods
end

.export(data, source, namespace, options = {}) ⇒ String

Generate Csv String

Parameters:

  • data (Array, ActiveRecord::Relation)
  • source (Symbol, String)
  • namespace (Symobl, String)
  • options (Hash) (defaults to: {})

    ({})

Options Hash (options):

  • :eval_methods (Array)
  • :label_keys (Array)
  • :label_prefix (String)
  • :csv_options (Hash) — default: see http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html

Returns:

  • (String)

    Csv String



31
32
33
# File 'lib/active_export/base.rb', line 31

def export(data, source, namespace, options = {})
  new(source, namespace, options).export(data)
end

.export_file(data, source, namespace, filename, options = {}) ⇒ Object

Generate Csv File

Parameters:

  • data (Array, ActiveRecord::Relation)
  • source (Symbol, String)

    YAML File alias name (see ActiveExport::Configuration#sources)

  • namespace (Symbol, String)

    YAML File namespace

  • filename (String, Filepath)

    Csv File name

  • options (Hash) (defaults to: {})

    ({}) (see .export)



41
42
43
# File 'lib/active_export/base.rb', line 41

def export_file(data, source, namespace, filename, options = {})
  new(source, namespace, options).export_file(data, filename)
end

.translate(key, scope = []) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/active_export/base.rb', line 45

def translate(key, scope = [])
  defaults = [
    :"active_export.#{scope.join('.')}.#{key.to_s.gsub('.', '_')}",
    :"activerecord.attributes.#{key}",
    :"activemodel.attributes.#{key}",
    key.to_s.gsub('.', '_').humanize
  ]
  I18n.translate(defaults.shift, default: defaults)
end

Instance Method Details

#build_label_keys_and_eval_methods!Object



124
125
126
# File 'lib/active_export/base.rb', line 124

def build_label_keys_and_eval_methods!
  @label_keys, @eval_methods = self.class.build_label_keys_and_eval_methods(source[:methods])
end

#convert(value) ⇒ Object

TODO:

refactor me

Convert value for export string



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_export/base.rb', line 88

def convert(value)
  if value.nil?
    translate(:nil, config.default_value_label_scope)
  elsif value == ''
    translate(:blank, config.default_value_label_scope)
  elsif value == true
    translate(:true, config.default_value_label_scope)
  elsif value == false
    translate(:false, config.default_value_label_scope)
  else
    value.to_s
  end
end

#default_scopeObject



116
117
118
# File 'lib/active_export/base.rb', line 116

def default_scope
  [self.source_name, self.namespace]
end

#export_data(data, exporter) ⇒ Object

Export data to exporter

Parameters:

  • data (Array, ActiveRecord::Relation)

    Export target data

  • Object (Object)

    needs to support ‘<<’ accessor



76
77
78
79
80
81
82
83
84
# File 'lib/active_export/base.rb', line 76

def export_data(data, exporter)
  if data.respond_to?(:find_in_batches) && data.respond_to?(:orders) && data.orders.blank? && data.respond_to?(:taken) && data.taken.blank?
    data.find_in_batches(find_in_batches_options) do |group|
      group.each{|f| exporter << generate_value(f) }
    end
  else
    data.each{|f| exporter << generate_value(f) }
  end
end

#find_in_batches_optionsObject (protected)



154
155
156
# File 'lib/active_export/base.rb', line 154

def find_in_batches_options
  self.config.default_find_in_batches_options.merge( self.options[:find_in_batches_options] || {} )
end

#generate_headerObject (protected)



141
142
143
144
145
# File 'lib/active_export/base.rb', line 141

def generate_header
  self.label_keys.inject([]) {|result, key|
    result << translate(key_name(key))
  }
end

#generate_value(row) ⇒ Object (protected)



147
148
149
150
151
152
# File 'lib/active_export/base.rb', line 147

def generate_value(row)
  self.eval_methods.inject([]){|result, f|
    v = row.send(:eval, f) rescue nil
    result << convert(v)
  }
end

#key_name(key) ⇒ Object



120
121
122
# File 'lib/active_export/base.rb', line 120

def key_name(key)
  key.to_s.include?(".") ? key.to_s : "#{label_prefix}.#{key}"
end

#translate(key, scope = nil) ⇒ Object



135
136
137
# File 'lib/active_export/base.rb', line 135

def translate(key, scope = nil)
  self.class.translate(key, scope || default_scope)
end