Class: RUIC

Inherits:
Object
  • Object
show all
Defined in:
lib/ruic.rb,
lib/ruic.rb,
lib/ruic/version.rb

Overview

The RUIC class provides the interface for running scripts using the special DSL, and for running the interactive REPL. See the README file for description of the DSL.

Constant Summary collapse

DEFAULTMETADATA =
'C:/Program Files (x86)/NVIDIA Corporation/UI Composer 8.0/res/DataModelMetadata/en-us/MetaData.xml'
VERSION =
'0.4.6'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata = DEFAULTMETADATA) ⇒ RUIC

Creates a new environment for executing a RUIC script.

Parameters:

  • metadata (String) (defaults to: DEFAULTMETADATA)

    Path to the MetaData.xml file to use.



82
83
84
85
# File 'lib/ruic.rb', line 82

def initialize( =DEFAULTMETADATA )
	@metadata = 
	@apps = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *a) ⇒ UIC::Application

Used to resolve bare app and app2 calls to a loaded Application.

Returns:



112
113
114
# File 'lib/ruic.rb', line 112

def method_missing(name,*a)
	@apps[name] || (name=~/^app\d*/ ? "(no #{name} loaded)".extend(SelfInspecting) : super)
end

Class Method Details

.run(opts = {}) ⇒ nil

Execute a script and/or launch the interactive REPL.

If you both run a :script and then enter the :repl all local variables created by the script will be available in the REPL.

# Just run a script
RUIC.run script:'my.ruic'

# Load an application and then enter the REPL
RUIC.run uia:'my.uia', repl:true

# Run a script and then drop into the REPL
RUIC.run script:'my.ruic', repl:true

The working directory for scripts is set to the directory of the script.

The working directory for the repl is set to the directory of the first loaded application (if any); failing that, the directory of the script (if any); failing that, the current directory.

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :script (String)

    A path to a .ruic script to run (optional).

  • :uia (String)

    An .uia file to load (before running the script and/or REPL) (optional).

  • :repl (Boolean)

    Pass true to enter the command-line REPL after executing the script (if any).

Returns:

  • (nil)


47
48
49
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
# File 'lib/ruic.rb', line 47

def self.run(opts={})
	opts = opts
	ruic = nil
	if opts[:script]
		script = File.read(opts[:script],encoding:'utf-8')
		Dir.chdir(File.dirname(opts[:script])) do
			ruic = self.new
			ruic.uia(opts[:uia]) if opts[:uia]
			ruic.env.eval(script,opts[:script])
		end
	end

	if opts[:repl]
		location = (ruic && ruic.app && ruic.app.respond_to?(:file) && ruic.app.file) || opts[:uia] || opts[:script] || '.'
		Dir.chdir( File.dirname(location) ) do
			ruic ||= self.new.tap{ |r| r.uia(opts[:uia]) if opts[:uia] }
			require 'ripl/irb'
			require 'ripl/multi_line'
			require 'ripl/multi_line/live_error.rb'
			require_relative 'ruic/ripl-after-result'
			Ripl::MultiLine.engine = Ripl::MultiLine::LiveError
			Ripl::Shell.include Ripl::MultiLine.engine
			Ripl::Shell.include Ripl::AfterResult
			Ripl.config.merge! prompt:"", result_prompt:'#=> ', multi_line_prompt:'  ', irb_verbose:false, after_result:"\n"
			ARGV.clear # So that RIPL doesn't try to interpret the options
			puts "(RUIC v#{RUIC::VERSION} interactive session; 'quit' or ctrl-d to end)"
			ruic.instance_eval{ puts @apps.map{ |n,app| "(#{n} is #{app.inspect})" } }
			puts "" # blank line before first input
			Ripl.start binding:ruic.env
		end
	end
end

Instance Method Details

#assert(condition = :CONDITIONNOTSUPPLIED, msg = nil, &block) ⇒ Object

Simple assertion mechanism to be used within scripts.

Examples:

1) simple call syntax

# Provides a generic failure message
assert a==b
#=> assertion failed (my.ruic line 17)

# Provides a custom failure message
assert a==b, "a should equal b"
#=> a should equal b : assertion failed (my.ruic line 17)

2) block with string syntax

# The code in the string to eval is also the failure message
assert{ "a==b" }
#=> a==b : assertion failed (my.ruic line 17)

Parameters:

  • condition (Boolean) (defaults to: :CONDITIONNOTSUPPLIED)

    the value to evaluate.

  • msg (String) (defaults to: nil)

    the nice error message to display.

Yield Returns:

  • (String)

    the code to evaluate as a condition.



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ruic.rb', line 135

def assert(condition=:CONDITIONNOTSUPPLIED,msg=nil,&block)
	if block && condition==:CONDITIONNOTSUPPLIED
		msg = yield
		condition = msg.is_a?(String) ? eval(msg,block.binding) : msg
	end
	condition || begin
		file, line, _ = caller.first.split(':')
		puts "#{"#{msg} : " unless msg.nil?}assertion failed (#{file} line #{line})"
		exit 1
	end
end

#envBinding

Returns the shared binding used for evaluating the script and REPL.

Returns:

  • (Binding)

    the shared binding used for evaluating the script and REPL



103
104
105
# File 'lib/ruic.rb', line 103

def env
	@env ||= binding
end

#inspectObject



151
152
153
# File 'lib/ruic.rb', line 151

def inspect
	"<RUIC #{@apps.empty? ? "(no app loaded)" : Hash[ @apps.map{ |id,app| [id,File.basename(app.file)] } ]}>"
end

#metadata(path) ⇒ Object

Set the metadata to use; generally called from the RUIC DSL.

Parameters:

  • path (String)

    Path to the MetaData.xml file, either absolute or relative to the working directory.



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

def (path)
	@metadata = path
end

#show(*a) ⇒ Object

Nicer name for puts to be used in the DSL, printing the 'nice' string equivalent for all supplied arguments.



149
# File 'lib/ruic.rb', line 149

def show(*a); puts *a.map(&:to_s); end

#uia(path) ⇒ UIC::Application

Load an application, making it available as app, app2, etc.

Parameters:

  • path (String)

    Path to the *.uia application file.

Returns:



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

def uia(path)
	meta = UIC.MetaData @metadata
	name = @apps.empty? ? :app : :"app#{@apps.length+1}"
	@apps[name] = UIC.Application(meta,path)
end