Module: Card::Machine

Included in:
Set::Right::Script, Set::Right::Style, Set::Type::CoffeeScript, Set::Type::Css, Set::Type::JavaScript, Set::Type::Skin
Defined in:
mod/03_machines/lib/card/machine.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host_class) ⇒ Object



60
61
62
63
64
65
66
67
68
69
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'mod/03_machines/lib/card/machine.rb', line 60

def self.included(host_class)
  host_class.extend( ClassMethods )
  host_class.output_config = { filetype: 'txt' }


  if Codename[:machine_output]  # for compatibility with old migrations
    host_class.card_accessor :machine_output, type: :file
    host_class.card_accessor :machine_input, type: :pointer

    # define default machine behaviour
    host_class.collect_input_cards do
      # traverse through all levels of pointers and
      # collect all item cards as input
      items = [self]
      new_input = []
      already_extended = {} # avoid loops
      loop_limit = 5
      while items.size > 0
        item = items.shift
        next if item.trash
        next if already_extended[item.id].to_i > loop_limit
        if item.item_cards == [item]  # no pointer card
          new_input << item
        else
          items.insert(0, item.item_cards)
          items.flatten!
          new_input << item if item != self
          already_extended[item] = already_extended[item].to_i + 1
        end
      end
      new_input
    end

    host_class.prepare_machine_input {}
    host_class.machine_engine { |input| input }
    host_class.store_machine_output do |output|
      filetype = host_class.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


    host_class.format do
      view :machine_output_url do |args|
        machine_output_url
      end
    end

    event_suffix = host_class.name.gsub ':', '_'
    host_class.event(
      "reset_machine_output_#{ event_suffix }".to_sym,
      after: :expire_related, on: :save
    ) do
      reset_machine_output!
    end
  end
end

Instance Method Details

#ensure_machine_outputObject



196
197
198
199
200
201
# File 'mod/03_machines/lib/card/machine.rb', line 196

def ensure_machine_output
  output = fetch trait: :machine_output
  if !output || !output.selected_content_action_id
    update_machine_output
  end
end

#input_item_cardsObject



181
182
183
# File 'mod/03_machines/lib/card/machine.rb', line 181

def input_item_cards
  machine_input_card.item_cards
end

#lock!Object



169
170
171
# File 'mod/03_machines/lib/card/machine.rb', line 169

def lock!
  Card.cache.write lock_cache_key, true
end

#lock_cache_keyObject



161
162
163
# File 'mod/03_machines/lib/card/machine.rb', line 161

def lock_cache_key
  "UPDATE-LOCK:#{key}"
end

#locked?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'mod/03_machines/lib/card/machine.rb', line 165

def locked?
  Card.cache.read lock_cache_key
end

#machine_output_pathObject



191
192
193
194
# File 'mod/03_machines/lib/card/machine.rb', line 191

def machine_output_path
  ensure_machine_output
  machine_output_card.file.path
end

#machine_output_urlObject



185
186
187
188
189
# File 'mod/03_machines/lib/card/machine.rb', line 185

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

#reset_machine_output!Object



140
141
142
143
144
145
146
# File 'mod/03_machines/lib/card/machine.rb', line 140

def reset_machine_output!
  Auth.as_bot do
    moc = machine_output_card and moc.real? and moc.delete!
    #mic = machine_input_card  and mic.real? and mic.delete!
    update_input_card
  end
end

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



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'mod/03_machines/lib/card/machine.rb', line 126

def run_machine joint="\n"
  before_engine
  output = input_item_cards.map do |input|
    unless input.kind_of? Card::Set::Type::Pointer
      if input.respond_to? :machine_input
        engine( input.machine_input )
      else
        engine( input.format._render_raw )
      end
    end
  end.select(&:present?).join( joint )
  after_engine output
end

#unlock!Object



173
174
175
# File 'mod/03_machines/lib/card/machine.rb', line 173

def unlock!
  Card.cache.write lock_cache_key, false
end

#update_input_cardObject



177
178
179
# File 'mod/03_machines/lib/card/machine.rb', line 177

def update_input_card
  machine_input_card.items = engine_input
end

#update_machine_outputObject



149
150
151
152
153
154
155
156
157
158
159
# File 'mod/03_machines/lib/card/machine.rb', line 149

def update_machine_output
  if ok? :read and not was_already_locked = locked?
    Auth.as_bot do
      lock!
      update_input_card
      run_machine
    end
  end
ensure
   unlock! unless was_already_locked
end