class RenderWithView::HalfOpenStruct
Like OpenStruct but doesn't let you read a non-assigned value (raises instead of returning nil). This avoids issues where you read the wrong value due to a typo and don't notice.
Borrowed from @henrik, gist.github.com/henrik/19c68b2a41ab4d098ce8
Public Class Methods
new(hash = {})
click to toggle source
# File lib/render_with_view/half_open_struct.rb, line 10 def initialize(hash = {}) @hash = hash end
Public Instance Methods
fetch(name, fallback)
click to toggle source
# File lib/render_with_view/half_open_struct.rb, line 18 def fetch(name, fallback) @hash.fetch(name, fallback) end
include?(name)
click to toggle source
# File lib/render_with_view/half_open_struct.rb, line 14 def include?(name) @hash.include?(name) end
method_missing(name, *args)
click to toggle source
# File lib/render_with_view/half_open_struct.rb, line 22 def method_missing(name, *args) if name.to_s.end_with?("=") write(name.to_s.chop.to_sym, *args) elsif args.length == 0 read_or_raise(name) else raise NoMethodError end end
Private Instance Methods
read_or_raise(name)
click to toggle source
# File lib/render_with_view/half_open_struct.rb, line 38 def read_or_raise(name) @hash.fetch(name) { raise "Unknown key: #{name}" } end
write(name, value)
click to toggle source
# File lib/render_with_view/half_open_struct.rb, line 34 def write(name, value) @hash[name] = value end