Module: Card::Model::SaveHelper

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

Overview

API to create and update cards. It is intended as a helper for "external" scripts (seeding, testing, migrating, etc) and not for internal application code. The general pattern is: All methods use the ActiveRecord !-methods that throw exceptions if somethings fails. All !-methods in this module rename existing cards to resolve name conflicts)

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/card/model/save_helper.rb', line 161

def method_missing method, *args
  method_name, cardtype_card = extract_cardtype_from_method_name method
  return super unless method_name

  args = standardize_args(*args)
  send "#{method_name}_card", args.merge(type_id: cardtype_card.id)
end

Instance Method Details

#add_coderule_item(name, prefix, type_id, to) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/card/model/save_helper.rb', line 142

def add_coderule_item name, prefix, type_id, to
  codename = "#{prefix}_#{name.tr(' ', '_').underscore}"
  name = "#{prefix}: #{name}"

  ensure_card name, type_id: type_id,
                    codename: codename
  Card[to].add_item! name
end

#add_script(name, opts = {}) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/card/model/save_helper.rb', line 133

def add_script name, opts={}
  name.sub!(/^script\:?\s?/, "") # in case name is given with prefix
  # remove it so that we don't double it

  add_coderule_item name, "script",
                    opts[:type_id] || Card::CoffeeScriptID,
                    opts[:to] || "*all+*script"
end

#add_style(name, opts = {}) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/card/model/save_helper.rb', line 124

def add_style name, opts={}
  name.sub!(/^style\:?\s?/, "") # in case name is given with prefix
  # remove it so that we don't double it

  add_coderule_item name, "style",
                    opts[:type_id] || Card::ScssID,
                    opts[:to] || "*all+*style"
end

#create_card(name_or_args, content_or_args = nil) ⇒ Object Also known as: create



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

def create_card name_or_args, content_or_args=nil
  Card.create! create_args(name_or_args, content_or_args)
end

#create_card!(name_or_args, content_or_args = nil) ⇒ Object Also known as: create!

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



109
110
111
112
# File 'lib/card/model/save_helper.rb', line 109

def create_card! name_or_args, content_or_args=nil
  args = standardize_args name_or_args, content_or_args
  create_card args.reverse_merge(rename_if_conflict: :old)
end

#create_or_update_card(name_or_args, content_or_args = nil) ⇒ Object Also known as: create_or_update



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/card/model/save_helper.rb', line 28

def create_or_update_card name_or_args, content_or_args=nil
  name = name_from_args name_or_args

  if Card[name]
    args = standardize_update_args name_or_args, content_or_args
    update_card(name, args)
  else
    args = standardize_args name_or_args, content_or_args
    create_card(args)
  end
end

#create_or_update_card!(name_or_args, content_or_args = nil) ⇒ Object Also known as: create_or_update!



119
120
121
122
# File 'lib/card/model/save_helper.rb', line 119

def create_or_update_card! name_or_args, content_or_args=nil
  args = standardize_args name_or_args, content_or_args
  create_or_update args.reverse_merge(rename_if_conflict: :new)
end

#delete_card(name) ⇒ Object Also known as: delete



40
41
42
43
44
# File 'lib/card/model/save_helper.rb', line 40

def delete_card name
  return unless Card.exist?(name)

  Card[name].delete!
end

#delete_code_card(name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/card/model/save_helper.rb', line 46

def delete_code_card name
  if name.is_a? Symbol
    return unless Card::Codename.exist? name
  end
  return unless Card.exist?(name)

  card = Card[name]
  card.update! codename: nil
  card.delete!
end

#ensure_card(name_or_args, content_or_args = nil) ⇒ Object Also known as: ensure

create if card doesn't exist updates existing card only if given attributes are different except the name

Examples:

if a card with name "under_score" exists

ensure_card "Under Score"                 # => no change
ensure_card "Under Score", type: :pointer # => changes the type to pointer
                                          #    but not the name


64
65
66
67
# File 'lib/card/model/save_helper.rb', line 64

def ensure_card name_or_args, content_or_args=nil
  name, args = standardize_ensure_args name_or_args, content_or_args
  ensure_card_simplified name, args
end

#ensure_card!(name_or_args, content_or_args = nil) ⇒ Object Also known as: ensure!

create if card doesn't exist updates existing card only if given attributes are different including the name For example if a card with name "under_score" exists then ensure_card "Under Score" renames it to "Under Score"



82
83
84
85
# File 'lib/card/model/save_helper.rb', line 82

def ensure_card! name_or_args, content_or_args=nil
  name, args = standardize_ensure_args name_or_args, content_or_args
  ensure_card_simplified name, add_name(name, args)
end

#ensure_code_card(name_or_args, content_or_args = nil) ⇒ Object

like ensure_card but derives codename from name if no codename is given. The derived codename is all lower case with underscores; "*" and ":" are removed



71
72
73
74
75
# File 'lib/card/model/save_helper.rb', line 71

def ensure_code_card name_or_args, content_or_args=nil
  name, args = standardize_ensure_args name_or_args, content_or_args
  args[:codename] = name.downcase.tr(" ", "_").tr(":*", "") unless args[:codename]
  ensure_card_simplified name, args
end

#ensure_trait(name, codename, args = {}) ⇒ Object

Creates or updates a trait card with codename and right rules. Content for rules that are pointer cards by default is converted to pointer format.

Examples:

ensure_trait "*a_or_b", :a_or_b,
             default: { type_id: Card::PointerID },
             options: ["A", "B"],
             input: "radio"


95
96
97
98
99
100
# File 'lib/card/model/save_helper.rb', line 95

def ensure_trait name, codename, args={}
  ensure_card name, codename: codename
  args.each do |setting, value|
    ensure_trait_rule name, setting, value
  end
end

#ensure_trait_rule(trait, setting, value) ⇒ Object



102
103
104
105
106
# File 'lib/card/model/save_helper.rb', line 102

def ensure_trait_rule trait, setting, value
  validate_setting setting
  card_args = normalize_trait_rule_args setting, value
  ensure_card [trait, :right, setting], card_args
end

#extract_cardtype_from_method_name(method) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/card/model/save_helper.rb', line 173

def extract_cardtype_from_method_name method
  return unless method =~ /^(?<method_name>create|ensure)_(?<type>.+?)(?:_card)?$/

  type = Regexp.last_match[:type]
  cardtype_card = Card::Codename[type.to_sym] ? Card[type.to_sym] : Card[type]
  return unless cardtype_card&.type_id == Card::CardtypeID ||
                cardtype_card&.id == Card::SetID

  [Regexp.last_match[:method_name], cardtype_card]
end

#respond_to_missing?(method, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/card/model/save_helper.rb', line 169

def respond_to_missing? method, _include_private=false
  extract_cardtype_from_method_name(method) || super
end

#update_card(name, content_or_args) ⇒ Object Also known as: update



22
23
24
25
26
# File 'lib/card/model/save_helper.rb', line 22

def update_card name, content_or_args
  args = standardize_update_args name, content_or_args
  resolve_name_conflict args
  Card[name]&.update! args
end

#update_card!(name, content_or_args) ⇒ Object Also known as: update!



114
115
116
117
# File 'lib/card/model/save_helper.rb', line 114

def update_card! name, content_or_args
  args = standardize_update_args name, content_or_args
  update_card name, args.reverse_merge(rename_if_conflict: :new)
end

#with_user(user_name) ⇒ Object



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

def with_user user_name
  Card::Auth.with current_id: Card.fetch_id(user_name) do
    yield
  end
end