Module: Card::Set::Abstract::Machine

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb,
card/tmpsets/set/mod016-card-mod-machines/abstract/machine/output_cache.rb,
card/tmpsets/set/mod016-card-mod-machines/abstract/machine/output_update.rb,
card/tmpsets/set/mod015-card-mod-machines/abstract/machine.rb,
card/tmpsets/set/mod015-card-mod-machines/abstract/machine/output_cache.rb,
card/tmpsets/set/mod015-card-mod-machines/abstract/machine/output_update.rb

Overview

The default machine:

Defined Under Namespace

Modules: MachineClassMethods, OutputCache, OutputUpdate Classes: EngineInput

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.define_machine_events(host_class) ⇒ Object



155
156
157
158
159
160
161
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 155

def define_machine_events host_class
  event_suffix = host_class.name.tr ":", "_"
  event_name = "reset_machine_output_#{event_suffix}".to_sym
  host_class.event event_name, after: :expire_related, changed: :content, on: :save do
    reset_machine_output
  end
end

.included(host_class) ⇒ Object



147
148
149
150
151
152
153
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 147

def included host_class
  host_class.extend(MachineClassMethods)
  host_class.mattr_accessor :output_config
  host_class.output_config = { filetype: "txt" }

  define_machine_events host_class
end

.source_locationObject



24
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 24

def self.source_location; "/Users/ethan/dev/decko/gem/card-mod-machines/set/abstract/machine.rb"; end

Instance Method Details

#after_engine(output) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 128

def after_engine output
  filetype = output_config[:filetype]
  file = Tempfile.new [id.to_s, ".#{filetype}"]
  file.write output
  file.rewind
  Card::Auth.as_bot do
    p = machine_output_card
    p.file = file
    p.save!
  end
  file.close
  file.unlink
end

#before_engineObject



67
68
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 67

def before_engine
end

#direct_machine_input?(input_card) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
178
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 175

def direct_machine_input? input_card
  !input_card.collection? ||
    input_card.respond_to?(:machine_input)
end

#engine(input) ⇒ Object



124
125
126
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 124

def engine input
  input
end

#engine_inputObject



70
71
72
73
74
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 70

def engine_input
  ei = EngineInput.new self
  ei.process
  ei.new_input
end

#input_from_card(input_card) ⇒ Object



191
192
193
194
195
196
197
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 191

def input_from_card input_card
  if input_card.respond_to? :machine_input
    input_card.machine_input
  else
    input_card.format._render_raw
  end
end

#input_item_cardsObject



215
216
217
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 215

def input_item_cards
  machine_input_card.item_cards
end

#machine_output_codenameObject



209
210
211
212
213
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 209

def machine_output_codename
  machine_output_card.name.parts.map do |part|
    Card[part].codename&.to_s || Card[part].name.safe_key
  end.join "_"
end

#machine_output_pathObject



225
226
227
228
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 225

def machine_output_path
  ensure_machine_output
  machine_output_card.file.path
end

#machine_output_urlObject



219
220
221
222
223
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 219

def machine_output_url
  ensure_machine_output
  machine_output_card.file.url # (:default, timestamp: false)
  # to get rid of additional number in url
end

#make_machine_output_coded(mod = :machines) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 199

def make_machine_output_coded mod=:machines
  update_machine_output
  Card::Auth.as_bot do
    ENV["STORE_CODED_FILES"] = "true"
    machine_output_card.update! storage_type: :coded, mod: mod,
                                codename: machine_output_codename
    ENV["STORE_CODED_FILES"] = nil
  end
end

#run_engine(input_card) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 180

def run_engine input_card
  return unless direct_machine_input? input_card
  if (cached = fetch_cache_card(input_card)) && cached.content?
    return cached.content
  end

  engine(input_from_card(input_card)).tap do |output|
    cache_output_part input_card, output
  end
end

#run_machine(joint = "\n") ⇒ Object



166
167
168
169
170
171
172
173
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 166

def run_machine joint="\n"
  before_engine
  output =
    input_item_cards.map do |input_card|
      run_engine input_card
    end.select(&:present?).join(joint)
  after_engine output
end

#view: machine_output_urlObject



142
143
144
# File 'card/tmpsets/set/mod016-card-mod-machines/abstract/machine.rb', line 142

view :machine_output_url do
  machine_output_url
end