Class: DataShift::MapperUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/datashift/mapping/mapper_utils.rb

Class Method Summary collapse

Class Method Details

.class_from_string(str) ⇒ Object

Similar to const_get_from_string except this version returns nil if no such class found Support modules e.g “Spree::Property”



46
47
48
49
50
51
52
# File 'lib/datashift/mapping/mapper_utils.rb', line 46

def self.class_from_string( str )

  MapperUtils.const_get_from_string(str.to_s) # Kernel.const_get(model)
rescue StandardError
  nil

end

.class_from_string_or_raise(klass) ⇒ Object

Raises:

  • (NoSuchClassError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/datashift/mapping/mapper_utils.rb', line 16

def self.class_from_string_or_raise( klass )

  ruby_klass = begin
    # support modules e.g "Spree::Property")
    MapperUtils.class_from_string(klass) # Kernel.const_get(model)
  rescue NameError => e
    logger.error( e.message )
    raise Thor::Error, "ERROR: No such Class [#{klass}] found - check valid model supplied"
  end

  raise NoSuchClassError, "ERROR: No such Model [#{klass}] found - check valid model supplied" unless ruby_klass

  ruby_klass
end

.const_get_from_string(str) ⇒ Object

Helper to deal with string versions of modules/namespaced classes Find and return the base class from a string.

e.g “Spree::Property” returns the Spree::Property class Raises exception if no such class found



36
37
38
39
40
# File 'lib/datashift/mapping/mapper_utils.rb', line 36

def self.const_get_from_string(str)
  str.to_s.split('::').inject(Object) do |mod, class_name|
    mod.const_get(class_name)
  end
end

.ensure_class(klass) ⇒ Object



12
13
14
# File 'lib/datashift/mapping/mapper_utils.rb', line 12

def self.ensure_class( klass )
  klass.is_a?(String) ? MapperUtils.class_from_string_or_raise( klass ) : klass
end

.table_to_arclass(table, mod) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/datashift/mapping/mapper_utils.rb', line 54

def self.table_to_arclass(table, mod)

  find_table = mod.nil? ? table.classify : "#{mod}::#{table.classify}"

  begin
    DataShift::MapperUtils.class_from_string(find_table)
  rescue LoadError
  rescue StandardError
    nil
  end
end