Class: UIC::Application
- Inherits:
-
Object
- Object
- UIC::Application
- Includes:
- XMLFileBacked
- Defined in:
- lib/ruic/application.rb
Overview
The Application
represents the root of your UIC application, corresponding to a .uia
file.
Defined Under Namespace
Classes: Behavior, Effect, Presentation, RenderPlugin, StateMachine
Instance Attribute Summary collapse
-
#metadata ⇒ UIC::MetaData
readonly
The metadata loaded for this application.
Attributes included from XMLFileBacked
Attributes included from FileBacked
Instance Method Summary collapse
-
#[](asset_id_or_path) ⇒ UIC::Application::Behavior, ...
Find an asset by
.uia
identifier or path to the asset. -
#assets ⇒ Array
All assets referenced by the application.
-
#at(path) ⇒ MetaData::AssetBase
(also: #/)
Find an element or asset in a presentation by scripting path.
-
#behaviors ⇒ Array<UIC::Application::Behavior>
All behaviors referenced by the application.
-
#directory_files ⇒ Array<String>
Absolute paths of all files present in the application directory (used or not).
-
#image_paths ⇒ Array<String>
Array of all image paths used by the application (not just in subfolders).
-
#image_usage ⇒ Hash
A mapping of image paths to arrays of elements/assets that reference that image.
-
#initialize(metadata, uia_path = nil) ⇒ Application
constructor
A new instance of Application.
- #inspect ⇒ Object
-
#main_presentation ⇒ UIC::Application::Presentation
(also: #main)
The main presentation rendered by the application.
-
#main_presentation=(presentation) ⇒ UIC::Application::Presentation
Change which presentation is rendered for the application.
-
#missing_files ⇒ Array<String>
Files referenced by the application but not present in the directory.
-
#on_doc_loaded ⇒ nil
Loads the application from the file.
-
#presentations ⇒ Array<UIC::Application::Presentation>
All presentations referenced by the application.
-
#referenced_files ⇒ Array<String>
Absolute paths of files referenced by the application.
-
#renderplugins ⇒ Array<UIC::Application::RenderPlugin>
All render plug-ins referenced by the application.
-
#save_all! ⇒ Object
Save changes to this application and every asset to disk.
-
#statemachines ⇒ Array<UIC::Application::StateMachine>
All state machines referenced by the application.
-
#unused_files(hierarchy = false) ⇒ Array<String>
Files in the application directory not used by the application.
Methods included from XMLFileBacked
#file=, #save!, #save_as, #to_xml
Methods included from FileBacked
#absolute_path, #file_found?, #filename, #relative_path, #save!, #save_as
Constructor Details
#initialize(metadata, uia_path = nil) ⇒ Application
Returns a new instance of Application.
16 17 18 19 20 |
# File 'lib/ruic/application.rb', line 16 def initialize(,uia_path=nil) @assets = {} @metadata = self.file = uia_path end |
Instance Attribute Details
#metadata ⇒ UIC::MetaData (readonly)
Returns the metadata loaded for this application.
10 11 12 |
# File 'lib/ruic/application.rb', line 10 def @metadata end |
Instance Method Details
#[](asset_id_or_path) ⇒ UIC::Application::Behavior, ...
Find an asset by .uia
identifier or path to the asset.
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ruic/application.rb', line 49 def [](asset_id_or_path) all = assets if asset_id_or_path.start_with?('#') id = asset_id_or_path[1..-1] all.find{ |asset| asset.id==id } else full_path = File.(asset_id_or_path,File.dirname(file)) all.find{ |asset| asset.file==full_path } end end |
#assets ⇒ Array
Returns all assets referenced by the application. Ordered by the order they appear in the .uia
.
116 117 118 |
# File 'lib/ruic/application.rb', line 116 def assets @assets.values.inject(:+) end |
#at(path) ⇒ MetaData::AssetBase Also known as: /
Find an element or asset in a presentation by scripting path.
200 201 202 203 204 205 |
# File 'lib/ruic/application.rb', line 200 def at(path) parts = path.split(':') preso = parts.length==2 ? self["##{parts.first}"] : main_presentation raise "Cannot find presentation for #{id}" unless preso preso.at(parts.last) end |
#behaviors ⇒ Array<UIC::Application::Behavior>
Returns all behaviors referenced by the application.
165 166 167 |
# File 'lib/ruic/application.rb', line 165 def behaviors @assets['behavior'] ||= [] end |
#directory_files ⇒ Array<String>
Returns absolute paths of all files present in the application directory (used or not).
110 111 112 113 |
# File 'lib/ruic/application.rb', line 110 def directory_files dir = File.dirname(file) Dir.chdir(dir){ Dir['**/*.*'] }.map{ |f| File.(f,dir) } end |
#image_paths ⇒ Array<String>
Returns array of all image paths used by the application (not just in subfolders).
155 156 157 |
# File 'lib/ruic/application.rb', line 155 def image_paths image_usage.keys end |
#image_usage ⇒ Hash
Returns a mapping of image paths to arrays of elements/assets that reference that image.
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ruic/application.rb', line 141 def image_usage # TODO: this returns the same asset multiple times, with no indication of which property is using it; should switch to Properties. Hash[ (presentations + statemachines) .map(&:image_usage) .inject{ |h1,h2| h1.merge(h2){ |path,els1,els2| [*els1,*els2] } } .sort_by do |path,assets| parts = path.downcase.split '/' [ parts.length, parts ] end ].tap{ |h| h.extend(UIC::PresentableHash) } end |
#inspect ⇒ Object
5 6 7 |
# File 'lib/ruic/application.rb', line 5 def inspect "<UIC::Application '#{File.basename(file)}'#{:' FILENOTFOUND' unless file_found?}>" end |
#main_presentation ⇒ UIC::Application::Presentation Also known as: main
Returns the main presentation rendered by the application.
124 125 126 127 128 |
# File 'lib/ruic/application.rb', line 124 def main_presentation initial_id = @doc.at('assets')['initial'] presos = presentations presos.find{ |pres| pres.id==initial_id } || presos.first end |
#main_presentation=(presentation) ⇒ UIC::Application::Presentation
Change which presentation is rendered for the application.
134 135 136 137 138 |
# File 'lib/ruic/application.rb', line 134 def main_presentation=(presentation) # TODO: set to Presentation or Application::Presentation # TODO: create a unique ID if none exists @doc.at('assets')['initial'] = presentation.id end |
#missing_files ⇒ Array<String>
Files referenced by the application but not present in the directory.
93 94 95 |
# File 'lib/ruic/application.rb', line 93 def missing_files (referenced_files - directory_files).sort end |
#on_doc_loaded ⇒ nil
Loads the application from the file. If you pass the path to your .uia
to #initialize then this method is called automatically.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ruic/application.rb', line 26 def on_doc_loaded @assets = @doc.search('assets *').map do |el| case el.name when 'behavior' then UIC::Application::Behavior when 'statemachine' then UIC::Application::StateMachine when 'presentation' then UIC::Application::Presentation when 'renderplugin' then UIC::Application::RenderPlugin end.new(self,el) end.group_by{ |asset| asset.el.name } nil end |
#presentations ⇒ Array<UIC::Application::Presentation>
Returns all presentations referenced by the application.
160 161 162 |
# File 'lib/ruic/application.rb', line 160 def presentations @assets['presentation'] ||= [] end |
#referenced_files ⇒ Array<String>
Returns absolute paths of files referenced by the application.
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ruic/application.rb', line 98 def referenced_files # TODO: state machines can reference external scripts # TODO: behaviors can reference external scripts ( [file] + assets.map{ |asset| absolute_path(asset.src) } + statemachines.flat_map(&:referenced_files) + presentations.flat_map(&:referenced_files) ).uniq end |
#renderplugins ⇒ Array<UIC::Application::RenderPlugin>
Returns all render plug-ins referenced by the application.
175 176 177 |
# File 'lib/ruic/application.rb', line 175 def renderplugins @assets['renderplugin'] ||= [] end |
#save_all! ⇒ Object
Save changes to this application and every asset to disk.
180 181 182 183 184 |
# File 'lib/ruic/application.rb', line 180 def save_all! save! presentations.each(&:save!) # TODO: enumerate other assets and save them end |
#statemachines ⇒ Array<UIC::Application::StateMachine>
Returns all state machines referenced by the application.
170 171 172 |
# File 'lib/ruic/application.rb', line 170 def statemachines @assets['statemachine'] ||= [] end |
#unused_files(hierarchy = false) ⇒ Array<String>
Files in the application directory not used by the application.
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 |
# File 'lib/ruic/application.rb', line 63 def unused_files( hierarchy=false ) unused = (directory_files - referenced_files).sort if hierarchy root = File.dirname(file) UIC.tree_hierarchy(root) do |dir| File.directory?(dir) ? Dir.chdir(dir){ Dir['*'].map{ |f| File.(f) } } : [] end.map do |prefix,file| if file all = unused.select{ |path| path[/^#{file}/] } unless all.empty? size = NiceBytes.nice_bytes(all.map{ |f| File.size(f) }.inject(:+)) partial = file.sub(/^#{root}\//o,'') if File.directory?(file) "%s %s (%d files, %s)" % [prefix,partial,all.length,size] else "%s %s (%s)" % [prefix,partial,size] end end else prefix end end.compact.join("\n") else unused end end |