Class: DataShift::Transformation::Factory
- Inherits:
-
Object
- Object
- DataShift::Transformation::Factory
- Includes:
- Logging
- Defined in:
- lib/datashift/transformation/factory.rb
Constant Summary collapse
- TRANSFORMERS_HASH_INSTANCE_NAMES =
[:default, :override, :substitution, :prefix, :postfix].freeze
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#overrides ⇒ Object
readonly
Returns the value of attribute overrides.
-
#postfixes ⇒ Object
readonly
Returns the value of attribute postfixes.
-
#prefixes ⇒ Object
readonly
Returns the value of attribute prefixes.
-
#substitutions ⇒ Object
readonly
Returns the value of attribute substitutions.
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
-
#configure_from(load_object_class, yaml_file, locale_key = nil) ⇒ Object
Default values and over rides per class can be provided in YAML config file.
- #configure_from_yaml(load_object_class, yaml) ⇒ Object
- #hash_key(key) ⇒ Object
-
#initialize ⇒ Factory
constructor
A new instance of Factory.
- #set_substitution(method_binding, rule, replacement) ⇒ Object
- #set_substitution_on(key, operator, rule, replacement) ⇒ Object
Methods included from Logging
#logdir, #logdir=, #logger, #verbose
Constructor Details
#initialize ⇒ Factory
Returns a new instance of Factory.
81 82 83 |
# File 'lib/datashift/transformation/factory.rb', line 81 def initialize clear end |
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
78 79 80 |
# File 'lib/datashift/transformation/factory.rb', line 78 def defaults @defaults end |
#overrides ⇒ Object (readonly)
Returns the value of attribute overrides.
78 79 80 |
# File 'lib/datashift/transformation/factory.rb', line 78 def overrides @overrides end |
#postfixes ⇒ Object (readonly)
Returns the value of attribute postfixes.
79 80 81 |
# File 'lib/datashift/transformation/factory.rb', line 79 def postfixes @postfixes end |
#prefixes ⇒ Object (readonly)
Returns the value of attribute prefixes.
79 80 81 |
# File 'lib/datashift/transformation/factory.rb', line 79 def prefixes @prefixes end |
#substitutions ⇒ Object (readonly)
Returns the value of attribute substitutions.
78 79 80 |
# File 'lib/datashift/transformation/factory.rb', line 78 def substitutions @substitutions end |
Class Method Details
.instance(locale = :en) ⇒ Object
70 71 72 |
# File 'lib/datashift/transformation/factory.rb', line 70 def self.instance(locale = :en) @__instance__[locale] ||= new end |
.reset(locale = :en) ⇒ Object
74 75 76 |
# File 'lib/datashift/transformation/factory.rb', line 74 def self.reset(locale = :en) @__instance__[locale] = new end |
Instance Method Details
#clear ⇒ Object
85 86 87 88 89 |
# File 'lib/datashift/transformation/factory.rb', line 85 def clear TRANSFORMERS_HASH_INSTANCE_NAMES.each do|h| instance_variable_set("@#{h.to_s.pluralize}", ActiveSupport::HashWithIndifferentAccess.new({})) end end |
#configure_from(load_object_class, yaml_file, locale_key = nil) ⇒ Object
Default values and over rides per class can be provided in YAML config file.
The locale_key for situations where the Class one level down, for example as per our own Template for Datashift Import/Export Configuration which has format :
key:
klass:
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/datashift/transformation/factory.rb', line 99 def configure_from(load_object_class, yaml_file, locale_key = nil) data = YAML.load( ERB.new( IO.read(yaml_file) ).result ) class_name = load_object_class.name data = data[locale_key] if(locale_key) configure_from_yaml(load_object_class, data[class_name]) if(data[class_name]) end |
#configure_from_yaml(load_object_class, yaml) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/datashift/transformation/factory.rb', line 110 def configure_from_yaml(load_object_class, yaml) setter_method_map = { defaults: :set_default_on, overrides: :set_override_on, substitutions: :set_substitution_on_list, prefixes: :set_prefix_on, postfixes: :set_postfix_on } setter_method_map.each do |key, call| settings = yaml[key.to_s] settings.each do |operator, value| logger.info("Configuring Transform [#{key}] for [#{operator.inspect}] to [#{value}]") send( call, load_object_class, operator, value) end if(settings && settings.is_a?(Hash)) end end |
#hash_key(key) ⇒ Object
131 132 133 134 135 |
# File 'lib/datashift/transformation/factory.rb', line 131 def hash_key(key) return key if(key.is_a? String) return key.class_name if(key.is_a? MethodBinding) key.name # Class name end |
#set_substitution(method_binding, rule, replacement) ⇒ Object
197 198 199 |
# File 'lib/datashift/transformation/factory.rb', line 197 def set_substitution( method_binding, rule, replacement ) set_substitution_on( method_binding, method_binding.operator, rule, replacement) end |
#set_substitution_on(key, operator, rule, replacement) ⇒ Object
201 202 203 |
# File 'lib/datashift/transformation/factory.rb', line 201 def set_substitution_on(key, operator, rule, replacement ) substitutions_for(key)[operator] = Struct::Substitution.new(rule, replacement) end |