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

Extended by:
Card::Set
Defined in:
tmpsets/set/mod014-machines/abstract/machine.rb,
tmpsets/set/mod014-machines/abstract/machine/output_cache.rb,
tmpsets/set/mod014-machines/abstract/machine/output_update.rb

Overview

The default machine:

Defined Under Namespace

Modules: MachineClassMethods, OutputCache, OutputUpdate

Class Method Summary collapse

Instance Method Summary collapse

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

#abstract_set?, #all_set?, #num_set_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #ensure_set, #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



127
128
129
130
131
132
133
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 127

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



119
120
121
122
123
124
125
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 119

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 'tmpsets/set/mod014-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



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 100

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 'tmpsets/set/mod014-machines/abstract/machine.rb', line 67

def before_engine
end

#direct_machine_input?(input_card) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
150
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 147

def direct_machine_input? input_card
  !input_card.is_a?(Card::Set::Type::Pointer) ||
    input_card.respond_to?(:machine_input)
end

#engine(input) ⇒ Object



96
97
98
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 96

def engine input
  input
end

#engine_inputObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 70

def engine_input
  # TODO: replace with call of extended_item_cards
  # traverse through all levels of pointers and
  # collect all item cards as input
  items = [self]
  new_input = []
  already_extended = {} # avoid loops
  loop_limit = 5
  until items.empty?
    item = items.shift
    next if item.trash || already_extended[item.id].to_i > loop_limit
    if item.item_cards == [item] # no pointer card
      new_input << item
    else
      # item_cards instantiates non-existing cards
      # we don't want those
      items.insert(0, item.item_cards.reject(&:unknown?))
      items.flatten!

      new_input << item if item != self && item.known?
      already_extended[item] = already_extended[item].to_i + 1
    end
  end
  new_input
end

#input_from_card(input_card) ⇒ Object



163
164
165
166
167
168
169
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 163

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



187
188
189
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 187

def input_item_cards
  machine_input_card.item_cards
end

#machine_output_codenameObject



181
182
183
184
185
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 181

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



197
198
199
200
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 197

def machine_output_path
  ensure_machine_output
  machine_output_card.file.path
end

#machine_output_urlObject



191
192
193
194
195
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 191

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



171
172
173
174
175
176
177
178
179
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 171

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



152
153
154
155
156
157
158
159
160
161
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 152

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



138
139
140
141
142
143
144
145
# File 'tmpsets/set/mod014-machines/abstract/machine.rb', line 138

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