Module: Pottery::InstanceMethods

Defined in:
lib/pottery.rb

Instance Method Summary collapse

Instance Method Details

#saveObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pottery.rb', line 47

def save
  if respond_to?('id_name') && !id_name.nil? && !(id_name.to_s.strip.size == 0)
    existing = Soup[id_name]
    if existing && !(existing.is_a?(Array) && existing.empty?)
      if existing.is_a? Array
        last = existing.pop
        puts 'deleting duplicates for id: ' + id_name
        existing.each {|x| x.destroy}
        existing = last
      end
      snip = Pottery::PotterySnip.new existing.attributes.merge({:__id => existing.id})
    else
      snip = Pottery::PotterySnip.new
    end
    morph_attributes.each_pair do |symbol, value|
      symbol = convert_if_name_or_id_name(symbol)
      snip.set_value(symbol.to_s, value)
    end
    snip.save
    self
  else
    raise 'unique id_name must be defined'
  end
end