Module: Six::Import::Model::ClassMethods

Defined in:
lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb

Instance Method Summary collapse

Instance Method Details

#blabla(h) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 64

def blabla(h)
  l = []
  h.each_pair do |key, value|
    l << self.from_hash(value.merge({:type => key.camelize.singularize}))
    # Officially??
    # l << Object.const_get(key.camelize.singularize).from_hash(value.merge({:type => key.camelize.singularize}))
  end
  l
end

#from_hash(hash) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 99

def from_hash( hash )
  h = hash.dup
  h.each do |key, value|
    case value
      when Array
        h[key].map! { |e|
          e.is_a?(Hash) ? Object.const_get(key.camelize.singularize).from_hash(e) : e
        }
      when Hash
        h[key] = Object.const_get(key.camelize).from_hash value
    end
  end
  n = nil
  previous = nil
  if h["id"]
    id = h["id"]
    h.delete "id"
    r = nil
    begin
      r = self.override_class.find(id)
    rescue
    end
    if r
      previous = r.inspect
      r.attributes = h
      n = r
    else
      n = self.new h
      n.id = id
    end
  else
    n = self.new h
  end
  n.type = h[:type] if h[:type] if n.respond_to?(:type)
  #logger.info "#{n.class} #{n.id} #{n.changed?}: #{"#{n.inspect} - #{previous}" if n.changed?}"
  n.save if n.changed?# || n.new?
  n
end

#from_json(json) ⇒ Object



138
139
140
141
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 138

def from_json( json )
  hash = ActiveSupport::JSON.decode json
  self.from_hash hash
end

#from_xml(xml) ⇒ Object

The xml has a surrounding class tag (e.g. ship-to), but the hash has no counterpart (e.g. ‘ship_to’ => {} )



145
146
147
148
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 145

def from_xml( xml )
  hash = Hash.from_xml xml
  self.from_hash hash[self.to_s.underscore]
end

#impObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 74

def imp
  Six::Network::Panel.setlogger(logger)
  begin
    Six::Network::Panel.("", "")
  rescue ScriptError
  end

  path = "/#{self.to_s.pluralize.downcase}/exp/#{SYNC_VERSION}.json"
  logger.debug "Path: #{path}"
  res = Six::Network::Panel.get(path)
  h = ActiveSupport::JSON.decode res.body
  k = []
  return k unless h

  case h
    when Array
      h.each do |b|
        k += self.blabla(b)
      end
    when Hash
      k += self.blabla h
  end
  k
end

#override_classObject



60
61
62
# File 'lib/six-updater-web/vendor/plugins/six-import/lib/six/import.rb', line 60

def override_class
  defined?(self::OVERRIDE_MODEL) ? self::OVERRIDE_MODEL : self
end