Class: Utopia::Middleware::Controller::Variables

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/middleware/controller.rb

Instance Method Summary collapse

Constructor Details

#initializeVariables

Returns a new instance of Variables.



34
35
36
# File 'lib/utopia/middleware/controller.rb', line 34

def initialize
	@controllers = [Object.new]
end

Instance Method Details

#<<(controller) ⇒ Object



38
39
40
# File 'lib/utopia/middleware/controller.rb', line 38

def << controller
	@controllers << controller
end

#[](key) ⇒ Object



73
74
75
# File 'lib/utopia/middleware/controller.rb', line 73

def [] key
	fetch("@#{key}".to_sym) { nil }
end

#[]=(key, value) ⇒ Object

Deprecated - to support old code:



78
79
80
# File 'lib/utopia/middleware/controller.rb', line 78

def []= key, value
	@controllers.first.instance_variable_set("@#{key}".to_sym, value)
end

#fetch(key) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/utopia/middleware/controller.rb', line 42

def fetch(key)
	@controllers.reverse_each do |controller|
		if controller.instance_variables.include?(key)
			return controller.instance_variable_get(key)
		end
	end
	
	if block_given?
		yield key
	else
		raise KeyError.new(key)
	end
end

#to_hashObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/utopia/middleware/controller.rb', line 56

def to_hash
	attributes = {}

	@controllers.each do |controller|
		controller.instance_variables.each do |name|
			key = name[1..-1]
			
			# Instance variables that start with an underscore are considered private and not exposed:
			next if key.start_with?('_')
			
			attributes[key] = controller.instance_variable_get(name)
		end
	end

	return attributes
end