Class: ActiveExport::Base
- Inherits:
-
Object
- Object
- ActiveExport::Base
- Defined in:
- lib/active_export/base.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#eval_methods ⇒ Object
Returns the value of attribute eval_methods.
-
#label_keys ⇒ Object
Returns the value of attribute label_keys.
-
#label_prefix ⇒ Object
Returns the value of attribute label_prefix.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#options ⇒ Object
Returns the value of attribute options.
-
#source ⇒ Object
Returns the value of attribute source.
-
#source_name ⇒ Object
Returns the value of attribute source_name.
Class Method Summary collapse
- .build_label_keys_and_eval_methods(methods) ⇒ Array
-
.export(data, source, namespace, options = {}) ⇒ String
Generate Csv String.
-
.export_file(data, source, namespace, filename, options = {}) ⇒ Object
Generate Csv File.
- .translate(key, scope = []) ⇒ Object
Instance Method Summary collapse
- #build_label_keys_and_eval_methods! ⇒ Object
-
#convert(value) ⇒ Object
Convert value for export string.
- #default_scope ⇒ Object
-
#export_data(data, exporter) ⇒ Object
Export data to exporter.
- #find_in_batches_options ⇒ Object protected
- #generate_header ⇒ Object protected
- #generate_value(row) ⇒ Object protected
-
#initialize(source_name, namespace, options = {}) ⇒ Base
constructor
A new instance of Base.
- #key_name(key) ⇒ Object
- #translate(key, scope = nil) ⇒ Object
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, = {}) @source_name = source_name.to_sym @namespace = namespace.to_sym @config = ::ActiveExport.configuration @label_keys = .has_key?(:label_keys) ? .delete(:label_keys) : nil @eval_methods = .has_key?(:eval_methods) ? .delete(:eval_methods) : nil @label_prefix = .has_key?(:label_prefix) ? .delete(:label_prefix) : nil @options = end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/active_export/base.rb', line 9 def config @config end |
#eval_methods ⇒ Object
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_keys ⇒ Object
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_prefix ⇒ Object
Returns the value of attribute label_prefix.
8 9 10 |
# File 'lib/active_export/base.rb', line 8 def label_prefix @label_prefix end |
#namespace ⇒ Object
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/active_export/base.rb', line 8 def namespace @namespace end |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/active_export/base.rb', line 8 def @options end |
#source ⇒ Object
Returns the value of attribute source.
8 9 10 |
# File 'lib/active_export/base.rb', line 8 def source @source end |
#source_name ⇒ Object
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
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
31 32 33 |
# File 'lib/active_export/base.rb', line 31 def export(data, source, namespace, = {}) new(source, namespace, ).export(data) end |
.export_file(data, source, namespace, filename, options = {}) ⇒ Object
Generate Csv File
41 42 43 |
# File 'lib/active_export/base.rb', line 41 def export_file(data, source, namespace, filename, = {}) new(source, namespace, ).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
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_scope ⇒ Object
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
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() do |group| group.each{|f| exporter << generate_value(f) } end else data.each{|f| exporter << generate_value(f) } end end |
#find_in_batches_options ⇒ Object (protected)
154 155 156 |
# File 'lib/active_export/base.rb', line 154 def self.config..merge( self.[:find_in_batches_options] || {} ) end |
#generate_header ⇒ Object (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 |