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
- #ensure_machine_output ⇒ Object
- #input_item_cards ⇒ Object
- #lock! ⇒ Object
- #lock_cache_key ⇒ Object
- #locked? ⇒ Boolean
- #machine_output_path ⇒ Object
- #machine_output_url ⇒ Object
- #reset_machine_output! ⇒ Object
- #run_machine(joint = "\n") ⇒ Object
- #unlock! ⇒ Object
- #update_input_card ⇒ Object
- #update_machine_output ⇒ Object
Class Method Details
.included(host_class) ⇒ Object
50 51 52 53 54 55 56 57 58 59 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 |
# File 'mod/03_machines/lib/card/machine.rb', line 50 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 if item.trash or ( already_extended[item.id] and already_extended[item.id] > loop_limit) next elsif 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] ? already_extended[item] + 1 : 1 end end new_input end host_class.prepare_machine_input {} host_class.machine_engine { |input| input } host_class.store_machine_output do |output| file = Tempfile.new [ id.to_s, ".#{host_class.output_config[: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 host_class.event "reset_machine_output_#{host_class.name.gsub(':','_')}".to_sym, :after => :store_subcards, :on => :save do reset_machine_output! end end end |
Instance Method Details
#ensure_machine_output ⇒ Object
180 181 182 183 184 |
# File 'mod/03_machines/lib/card/machine.rb', line 180 def ensure_machine_output if !output = fetch(:trait => :machine_output) or !output.selected_content_action_id update_machine_output end end |
#input_item_cards ⇒ Object
166 167 168 |
# File 'mod/03_machines/lib/card/machine.rb', line 166 def input_item_cards machine_input_card.item_cards end |
#lock! ⇒ Object
154 155 156 |
# File 'mod/03_machines/lib/card/machine.rb', line 154 def lock! Card.cache.write lock_cache_key, true end |
#lock_cache_key ⇒ Object
146 147 148 |
# File 'mod/03_machines/lib/card/machine.rb', line 146 def lock_cache_key "UPDATE-LOCK:#{key}" end |
#locked? ⇒ Boolean
150 151 152 |
# File 'mod/03_machines/lib/card/machine.rb', line 150 def locked? Card.cache.read lock_cache_key end |
#machine_output_path ⇒ Object
175 176 177 178 |
# File 'mod/03_machines/lib/card/machine.rb', line 175 def machine_output_path ensure_machine_output machine_output_card.file.path end |
#machine_output_url ⇒ Object
170 171 172 173 |
# File 'mod/03_machines/lib/card/machine.rb', line 170 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
125 126 127 128 129 130 131 |
# File 'mod/03_machines/lib/card/machine.rb', line 125 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
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'mod/03_machines/lib/card/machine.rb', line 111 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
158 159 160 |
# File 'mod/03_machines/lib/card/machine.rb', line 158 def unlock! Card.cache.write lock_cache_key, false end |
#update_input_card ⇒ Object
162 163 164 |
# File 'mod/03_machines/lib/card/machine.rb', line 162 def update_input_card machine_input_card.items = engine_input end |
#update_machine_output ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 |
# File 'mod/03_machines/lib/card/machine.rb', line 134 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 |