Class: UIC::Application

Inherits:
Object
  • Object
show all
Includes:
FileBacked
Defined in:
lib/ruic/application.rb

Defined Under Namespace

Classes: Behavior, Presentation, StateMachine

Instance Attribute Summary collapse

Attributes included from FileBacked

#doc, #file

Instance Method Summary collapse

Methods included from FileBacked

#file_found?, #filename, #path_to

Constructor Details

#initialize(metadata, uia_path) ⇒ Application

Returns a new instance of Application.



9
10
11
12
13
# File 'lib/ruic/application.rb', line 9

def initialize(,uia_path)
	@metadata = 
	self.file = uia_path
	load_from_file if file_found?
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



8
9
10
# File 'lib/ruic/application.rb', line 8

def 
  @metadata
end

Instance Method Details

#[](asset_id_or_path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/ruic/application.rb', line 35

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.expand_path(asset_id_or_path,File.dirname(file))
		all.find{ |asset| asset.file==full_path }
	end
end

#assetsObject



57
58
59
# File 'lib/ruic/application.rb', line 57

def assets
	@assets.values.inject(:+)
end

#at(path) ⇒ Object Also known as: /



110
111
112
113
114
115
# File 'lib/ruic/application.rb', line 110

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

#behaviorsObject



93
94
95
# File 'lib/ruic/application.rb', line 93

def behaviors
	@assets['behavior'] ||= []
end

#errorsObject



31
32
33
# File 'lib/ruic/application.rb', line 31

def errors
	file_found? ? assets.flat_map(&:errors) : ["File not found: '#{file}'"]
end

#errors?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ruic/application.rb', line 27

def errors?
	!errors.empty?
end

#image_pathsObject



85
86
87
# File 'lib/ruic/application.rb', line 85

def image_paths
	image_usage.keys
end

#image_usageObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ruic/application.rb', line 73

def image_usage
	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

#inspectObject



4
5
6
# File 'lib/ruic/application.rb', line 4

def inspect
	"<UIC::Application '#{File.basename(file)}'>"
end

#load_from_fileObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruic/application.rb', line 15

def load_from_file
	self.doc  = Nokogiri.XML(File.read(file,encoding:'utf-8'))
	@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 }
end

#main_presentationObject



61
62
63
64
65
# File 'lib/ruic/application.rb', line 61

def main_presentation
	initial_id = @doc.at('assets')['initial']
	presos = presentations
	presos.find{ |pres| pres.id==initial_id } || presos.first
end

#main_presentation=(presentation) ⇒ Object



67
68
69
70
71
# File 'lib/ruic/application.rb', line 67

def main_presentation=(presentation)
	# TODO: set to Presentation or PresentationAsset
	# TODO: create a unique ID if none exists
	@doc.at('assets')['initial'] = presentation.id
end

#presentationsObject



89
90
91
# File 'lib/ruic/application.rb', line 89

def presentations
	@assets['presentation'] ||= []
end

#referenced_filesObject



50
51
52
53
54
55
# File 'lib/ruic/application.rb', line 50

def referenced_files
	# TODO: state machines can reference external scripts
	# TODO: behaviors can reference external scripts
	assets.map{ |asset| path_to(asset.src) }
	+ presentations.flat_map{ |pres| pres.presentation.referenced_files }
end

#renderpluginsObject



101
102
103
# File 'lib/ruic/application.rb', line 101

def renderplugins
	@assets['renderplugin'] ||= []
end

#save_all!Object



105
106
107
108
# File 'lib/ruic/application.rb', line 105

def save_all!
	# save!
	presentations.each(&:save!)
end

#statemachinesObject



97
98
99
# File 'lib/ruic/application.rb', line 97

def statemachines
	@assets['statemachine'] ||= []
end

#unused_filesObject



46
47
48
# File 'lib/ruic/application.rb', line 46

def unused_files
	referenced_files - directory_files
end

#xmlObject



118
119
120
# File 'lib/ruic/application.rb', line 118

def xml
	@doc.to_xml
end