Class: ActiveExport::Base
- Inherits:
-
Object
- Object
- ActiveExport::Base
- Defined in:
- lib/active_export/base.rb
Direct Known Subclasses
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.
-
#source ⇒ Object
Returns the value of attribute source.
-
#source_name ⇒ Object
Returns the value of attribute source_name.
Class Method Summary collapse
Instance Method Summary collapse
- #build_label_keys_and_eval_methods! ⇒ Object
-
#convert(value) ⇒ Object
Convert value for export string.
- #default_scope ⇒ Object
-
#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 |
# 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) ? [:label_keys] : nil @eval_methods = .has_key?(:eval_methods) ? [:eval_methods] : nil @label_prefix = .has_key?(:label_prefix) ? [:label_prefix] : nil 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 |
#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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_export/base.rb', line 32 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 |
.translate(key, scope = []) ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/active_export/base.rb', line 20 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
86 87 88 |
# File 'lib/active_export/base.rb', line 86 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
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_export/base.rb', line 50 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
78 79 80 |
# File 'lib/active_export/base.rb', line 78 def default_scope [self.source_name, self.namespace] end |
#key_name(key) ⇒ Object
82 83 84 |
# File 'lib/active_export/base.rb', line 82 def key_name(key) key.to_s.include?(".") ? key.to_s : "#{label_prefix}.#{key}" end |
#translate(key, scope = nil) ⇒ Object
97 98 99 |
# File 'lib/active_export/base.rb', line 97 def translate(key, scope = nil) self.class.translate(key, scope || default_scope) end |