module Forwarder::Evaller

Constants

NotSerializable

Public Instance Methods

evaluable?(an_object, cache={}) click to toggle source
# File lib/forwarder/evaller.rb, line 6
def evaluable? an_object, cache={}
  !!_serialize_one( an_object, cache )
rescue NotSerializable
  false
end
serialize(args) click to toggle source
# File lib/forwarder/evaller.rb, line 12
def serialize args
  return "" if args.nil? || args.empty?
  serialize_without_arg_suffix( args ).join(", ") + ", "
rescue NotSerializable
  nil
end
serialize_without_arg_suffix(args, cache={}) click to toggle source
# File lib/forwarder/evaller.rb, line 19
def serialize_without_arg_suffix args, cache={}
  args.map{ | arg |
    _serialize_one arg, cache
  }
end

Private Instance Methods

_serialize_hash(hsh, cache) click to toggle source
# File lib/forwarder/evaller.rb, line 26
def _serialize_hash hsh, cache
  hsh.inject [] do | r, (k, v) |
    k = _serialize_one k, cache
    v = _serialize_one v, cache
    r << "#{k} => #{v}"
  end.join( ", ")
end
_serialize_object(arg, cache) click to toggle source
# File lib/forwarder/evaller.rb, line 45
def _serialize_object arg, cache
  oid = arg.object_id
  raise NotSerializable if cache[oid]
  cache[oid]=true
  case arg
  when Array
    ["[ ", serialize_without_arg_suffix( arg, cache ).join(", "), " ]"].join
  when Hash
    [ "{ ", _serialize_hash( arg, cache ), " }" ].join
  else
    raise NotSerializable
  end
end
_serialize_one(arg, cache) click to toggle source
# File lib/forwarder/evaller.rb, line 34
def _serialize_one arg, cache
  case arg
  when String
    "'" + arg + "'"
  when Symbol, Fixnum, NilClass, FalseClass, TrueClass
    arg.inspect
  else
    _serialize_object arg, cache
  end
end