Module: Card::Model::SaveHelper

Included in:
Card::Migration
Defined in:
lib/card/model/save_helper.rb

Instance Method Summary collapse

Instance Method Details

#create_card(args) ⇒ Object



4
5
6
7
# File 'lib/card/model/save_helper.rb', line 4

def create_card args
  resolve_name_conflict args
  Card.create! args
end

#create_card!(args) ⇒ Object

if card with same name exists move it out of the way



10
11
12
# File 'lib/card/model/save_helper.rb', line 10

def create_card! args
  create_card args.reverse_merge(rename_if_conflict: :old)
end

#create_or_update(name_or_args, args = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/card/model/save_helper.rb', line 23

def create_or_update name_or_args, args={}
  if name_or_args.is_a?(Hash)
    name = name_or_args.delete :name
    args = name_or_args
  else
    name = name_or_args
  end
  if Card[name]
    update_card name, args
  else
    create_card args.merge(name: name)
  end
end

#create_or_update!(name_or_args, args = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/card/model/save_helper.rb', line 37

def create_or_update! name_or_args, args=nil
  if name_or_args.is_a?(Hash)
    name = name_or_args.delete(:name)
    args = name_or_args
  else
    name = name_or_args
    args ||= {}
  end
  create_or_update name, args.reverse_merge(rename_if_conflict: :new)
end

#ensure_card(args) ⇒ Object

create if it doesn't exist



55
56
57
58
# File 'lib/card/model/save_helper.rb', line 55

def ensure_card args
  return if Card[args[:name]]
  Card.create! args
end

#resolve_name_conflict(args) ⇒ Object



48
49
50
51
52
# File 'lib/card/model/save_helper.rb', line 48

def resolve_name_conflict args
  rename = args.delete :rename_if_conflict
  return unless rename
  args[:name] = Card.uniquify_name args[:name], rename
end

#update_card(name, args) ⇒ Object



14
15
16
17
# File 'lib/card/model/save_helper.rb', line 14

def update_card name, args
  resolve_name_conflict args
  Card[name].update_attributes! args
end

#update_card!(args) ⇒ Object



19
20
21
# File 'lib/card/model/save_helper.rb', line 19

def update_card! args
  update_card args.reverse_merge(rename_if_conflict: :new)
end