Module: Card::Fetch::Results

Included in:
Card::Fetch
Defined in:
lib/card/fetch/results.rb

Overview

polishing fetch results

Instance Method Summary collapse

Instance Method Details

#assign_name(requested) ⇒ Object



126
127
128
129
130
131
# File 'lib/card/fetch/results.rb', line 126

def assign_name requested
  return if opts[:local_only]
  return unless requested&.to_s != card.name

  card.name = requested.to_s
end

#assignable_content(default_content) ⇒ Object



85
86
87
# File 'lib/card/fetch/results.rb', line 85

def assignable_content default_content
  new_opts[:content] || (@card.db_content.blank? && default_content)
end

#fancy_renew?Boolean

contains subcards, etc, that quick_renew can’t handle

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/card/fetch/results.rb', line 79

def fancy_renew?
  test_opts = new_opts.slice :supercard, :name, :type_id, :type, :type_code,
                             :content, :default_content
  new_opts.keys.size > test_opts.keys.size
end

#finalize_result_cardObject



11
12
13
14
# File 'lib/card/fetch/results.rb', line 11

def finalize_result_card
  card.include_set_modules unless skip_modules?
  card
end

#name_change?Boolean

Returns:

  • (Boolean)


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

def name_change?
  return false unless (new_name = new_opts[:name]&.to_name)
  return false if new_name.relative? && mark.name? && mark.absolute?

  new_name.to_s != @card.name.to_s
end

#new_optsObject



122
123
124
# File 'lib/card/fetch/results.rb', line 122

def new_opts
  @new_opts ||= opts[:new]
end

#new_result_cardObject



16
17
18
19
20
21
22
23
# File 'lib/card/fetch/results.rb', line 16

def new_result_card
  with_new_card do
    finalize_result_card
    # must include_set_modules before checking `card.known?`,
    # in case, eg, set modules override #virtual?
    card if new_opts || card.known?
  end
end

#newish_optsObject



44
45
46
47
48
49
50
51
# File 'lib/card/fetch/results.rb', line 44

def newish_opts
  hash = new_opts.clone.reverse_merge name: mark
  if (content = assignable_content(hash.delete(:default_content)))
    hash[:content] = content
  end
  hash[:type_lookup] = :force if @force_type_lookup
  hash
end

#quick_contentObject



89
90
91
92
93
# File 'lib/card/fetch/results.rb', line 89

def quick_content
  return unless (content = assignable_content(new_opts[:default_content]))

  @card.content = content
end

#quick_renewObject



53
54
55
56
57
58
59
60
# File 'lib/card/fetch/results.rb', line 53

def quick_renew
  return false unless quick_renew?

  update_supercard
  opts_name = new_opts[:name]
  assign_name(opts_name.present? ? opts_name : mark)
  true
end

#quick_renew?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
# File 'lib/card/fetch/results.rb', line 69

def quick_renew?
  return true if new_opts.blank?
  return false if type_change? || name_change?
  return false if fancy_renew?

  quick_content
  true
end

#renewObject



36
37
38
39
40
41
42
# File 'lib/card/fetch/results.rb', line 36

def renew
  return if new_opts.blank?

  # Rails.logger.info "renewing: #{mark}, #{new_opts}"
  @card = card.dup
  @card.newish newish_opts
end

#resultsObject



5
6
7
8
9
# File 'lib/card/fetch/results.rb', line 5

def results
  return if card.nil?

  card.new_card? ? new_result_card : finalize_result_card
end

#supercard_might_change_type?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/card/fetch/results.rb', line 116

def supercard_might_change_type?
  # ...via type_plus_right rule
  sc = new_opts[:supercard]
  @force_type_lookup = sc&.new? && (sc.type_id != Card.default_type_id)
end

#type_change?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
# File 'lib/card/fetch/results.rb', line 102

def type_change?
  return true if @card.type_id.nil?

  type_id = type_id_from_new_opts
  return true if !type_id && supercard_might_change_type?

  type_id && (type_id != @card.type_id)
end

#type_id_from_new_optsObject



111
112
113
114
# File 'lib/card/fetch/results.rb', line 111

def type_id_from_new_opts
  type_id = new_opts[:type_id] || new_opts[:type] || new_opts[:type_code]&.to_sym
  type_id.is_a?(Symbol) ? Codename.id(type_id) : type_id
end

#update_supercardObject



62
63
64
65
66
67
# File 'lib/card/fetch/results.rb', line 62

def update_supercard
  return unless (sc = new_opts[:supercard])

  @card.supercard = sc
  @card.update_superleft
end

#with_new_cardObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/card/fetch/results.rb', line 25

def with_new_card
  if new_opts
    quick_renew || renew
  elsif opts[:skip_virtual]
    return nil
  else
    assign_name mark
  end
  yield
end