Module: Ixtlan::Remote::ModelHelpers

Included in:
Resource, Rest
Defined in:
lib/ixtlan/remote/model_helpers.rb

Instance Method Summary collapse

Instance Method Details

#to_model(data) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ixtlan/remote/model_helpers.rb', line 34

def to_model( data )
  case data
  when Hash
    if data.size == 1
      case data.values.first
      when Array
        data.keys.first
      when Hash
        data.keys.first
      end
    end
  when Array
    if data.empty?
      #TODO
    else
      to_model_class(data[0].class)
    end
  when String
    data
  when Symbol
    data
  when Class
    to_model_class(data)
  else
    to_model_class(data.class)
  end
end

#to_model_class(obj) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/ixtlan/remote/model_helpers.rb', line 62

def to_model_class(obj)
  if obj.respond_to?(:model_name) 
    obj.model_name.constantize
  elsif obj.is_a? Class
    obj
  else
    obj.class
  end
end

#to_model_singular_underscore(data) ⇒ Object



30
31
32
# File 'lib/ixtlan/remote/model_helpers.rb', line 30

def to_model_singular_underscore( data )
  to_model_underscore( data ).sub( /s$/, '' )
end

#to_model_underscore(data) ⇒ Object



25
26
27
28
# File 'lib/ixtlan/remote/model_helpers.rb', line 25

def to_model_underscore( data )
  m = to_model( data )
  m.to_s.underscore if m
end