Class: DataShift::ModelMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/datashift/model_mapper.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”



38
39
40
41
42
43
44
# File 'lib/datashift/model_mapper.rb', line 38

def self.class_from_string( str )
  begin
    ModelMapper::const_get_from_string(str.to_s)  #Kernel.const_get(model)
  rescue
    return nil
  end
end

.class_from_string_or_raise(klass) ⇒ Object

Raises:

  • (NoSuchClassError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/datashift/model_mapper.rb', line 6

def self.class_from_string_or_raise( klass )

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

  raise NoSuchClassError.new("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



27
28
29
30
31
# File 'lib/datashift/model_mapper.rb', line 27

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