class Fabrique::DataBean

Public Class Methods

new(hash, name = nil) click to toggle source
# File lib/fabrique/data_bean.rb, line 7
def initialize(hash, name = nil)
  @hash = hash
  @name = name
end

Public Instance Methods

method_missing(sym, *args) click to toggle source
# File lib/fabrique/data_bean.rb, line 12
def method_missing(sym, *args)
  key = include?(sym)

  raise_no_method_error(sym) if key.nil?
  raise_argument_error(args) unless args.empty?

  glide(sym, fetch(sym))
end
Also aliased as: send
send(sym, *args)
Alias for: method_missing
to_s() click to toggle source
# File lib/fabrique/data_bean.rb, line 23
def to_s
  @name or ::Kernel.sprintf("0x%014x", __id__)
end

Private Instance Methods

fetch(sym) click to toggle source

TODO investigate possible optimization with Ruby 2.3 frozen strings

# File lib/fabrique/data_bean.rb, line 34
def fetch(sym)
  if @hash.include?(sym)
    @hash[sym]
  elsif @hash.include?(sym.to_s)
    @hash[sym.to_s]
  else
    raise_no_method_error(sym)
  end
end
glide(sym, v) click to toggle source
# File lib/fabrique/data_bean.rb, line 29
def glide(sym, v)
  v.is_a?(::Hash) ? ::Fabrique::DataBean.new(v, "#{to_s}.#{sym}") : v
end
include?(sym) click to toggle source
# File lib/fabrique/data_bean.rb, line 44
def include?(sym)
  if @hash.include?(sym)
    sym
  elsif @hash.include?(sym.to_s)
    sym.to_s
  end
end
raise_argument_error(args) click to toggle source
# File lib/fabrique/data_bean.rb, line 56
def raise_argument_error(args)
  ::Kernel.raise ::ArgumentError.new("wrong number of arguments (given #{args.size}, expected 0)")
end
raise_no_method_error(sym) click to toggle source
# File lib/fabrique/data_bean.rb, line 52
def raise_no_method_error(sym)
  ::Kernel.raise ::NoMethodError.new("undefined method `#{sym}' for #<Fabrique::DataBean:#{to_s}>")
end