Class: Utopia::Controller::Variables
- Inherits:
-
Object
- Object
- Utopia::Controller::Variables
- Defined in:
- lib/utopia/controller/variables.rb
Instance Method Summary collapse
- #<<(controller) ⇒ Object
- #[](key) ⇒ Object
- #fetch(key) ⇒ Object
-
#initialize ⇒ Variables
constructor
A new instance of Variables.
- #to_hash ⇒ Object
Constructor Details
#initialize ⇒ Variables
Returns a new instance of Variables.
24 25 26 |
# File 'lib/utopia/controller/variables.rb', line 24 def initialize @controllers = [] end |
Instance Method Details
#<<(controller) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/utopia/controller/variables.rb', line 28 def << controller top = @controllers.last @controllers << controller # This ensures that most variables will be at the top and controllers can naturally interactive with instance variables. controller.copy_instance_variables(top) if top end |
#[](key) ⇒ Object
65 66 67 |
# File 'lib/utopia/controller/variables.rb', line 65 def [] key fetch("@#{key}".to_sym) { nil } end |
#fetch(key) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/utopia/controller/variables.rb', line 37 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
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/utopia/controller/variables.rb', line 51 def to_hash attributes = {} @controllers.each do |controller| controller.instance_variables.each do |name| key = name[1..-1] attributes[key] = controller.instance_variable_get(name) end end return attributes end |