Class: Utopia::Middleware::Controller::Variables
- Defined in:
- lib/utopia/middleware/controller.rb
Instance Method Summary collapse
- #<<(controller) ⇒ Object
- #[](key) ⇒ Object
-
#[]=(key, value) ⇒ Object
Deprecated - to support old code:.
- #fetch(key) ⇒ Object
-
#initialize ⇒ Variables
constructor
A new instance of Variables.
- #to_hash ⇒ Object
Constructor Details
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_hash ⇒ Object
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 |