Class: H2o::Context
Instance Method Summary collapse
-
#[](name) ⇒ Object
doing a reverse lookup FIXME: need to double check this, also changed Block#add_layer in reverse order.
- #[]=(name, value) ⇒ Object
- #apply_filters(object, filters) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(context = {}) ⇒ Context
constructor
A new instance of Context.
- #pop ⇒ Object
- #push(hash = {}) ⇒ Object
- #resolve(name) ⇒ Object
- #stack ⇒ Object
Constructor Details
Instance Method Details
#[](name) ⇒ Object
doing a reverse lookup FIXME: need to double check this, also changed Block#add_layer in reverse order
11 12 13 14 15 16 17 |
# File 'lib/h2o/context.rb', line 11 def [](name) @stack.each do |layer| value = layer[name] return value unless value.nil? end nil end |
#[]=(name, value) ⇒ Object
19 20 21 |
# File 'lib/h2o/context.rb', line 19 def []=(name, value) @stack.first[name] = value end |
#apply_filters(object, filters) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/h2o/context.rb', line 91 def apply_filters(object, filters) filters.each do |filter| name, *args = filter raise FilterError, "Filter(#{name}) not found" unless @filter_env.respond_to?(name) args.map! do |arg| if arg.kind_of? Symbol resolve(arg) else arg end end object = @filter_env.__send__(name, object, *args) end object end |
#has_key?(key) ⇒ Boolean
87 88 89 |
# File 'lib/h2o/context.rb', line 87 def has_key?(key) !send(:[], key).nil? end |
#pop ⇒ Object
23 24 25 |
# File 'lib/h2o/context.rb', line 23 def pop @stack.shift if @stack.size > 1 end |
#push(hash = {}) ⇒ Object
27 28 29 |
# File 'lib/h2o/context.rb', line 27 def push(hash = {}) @stack.unshift hash end |
#resolve(name) ⇒ Object
42 43 44 45 46 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 79 80 81 82 83 84 85 |
# File 'lib/h2o/context.rb', line 42 def resolve(name) return name unless name.is_a? Symbol object = self parts = name.to_s.split('.') part_sym = nil parts.each do |part| part_sym = part.to_sym # Hashes if object.respond_to?(:has_key?) && (object.has_key?(part_sym) || object.has_key?(part)) result = object[part_sym] || object[part] # Proc object with extra caution begin result = object[part_sym] = result.call if result.is_a?(Proc) && object.respond_to?(:[]=) rescue return nil end object = result.to_h2o # Array and Hash like objects elsif part.match(/^-?\d+$/) if (object.respond_to?(:has_key?) || object.respond_to?(:fetch)) && value = object[part.to_i] object = value.to_h2o else return nil end # H2o::DataObject Type elsif (object.is_a?(DataObject) || object.class.h2o_safe_methods && object.class.h2o_safe_methods.include?(part_sym) )&& \ object.respond_to?(part_sym) object = object.__send__(part_sym).to_h2o # Sweet array shortcuts elsif object.respond_to?(part_sym) && [:first, :length, :size, :last].include?(part_sym) object = object.__send__(part_sym).to_h2o else return nil end end object end |
#stack ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/h2o/context.rb', line 31 def stack result = nil push begin result = yield ensure pop end result end |