Sha256: 8b38c9b242928c99fdfdb1004cd3954157dd7d81084c925d9c8933ed76432686
Contents?: true
Size: 951 Bytes
Versions: 3
Compression:
Stored size: 951 Bytes
Contents
module Yaks class Primitivize attr_reader :mappings def initialize @mappings = {} end def call(object) mappings.each do |pattern, block| return instance_exec(object, &block) if pattern === object end raise "don't know how to turn #{object.class} (#{object.inspect}) into a primitive" end def map(*types, &blk) types.each do |type| mappings[type] = blk end end def self.create new.tap do |p| p.map String, TrueClass, FalseClass, NilClass, Numeric do |object| object end p.map Symbol do |object| object.to_s end p.map Hash do |object| object.to_enum(:each).with_object({}) do |(key, value), output| output[call(key)] = call(value) end end p.map Enumerable do |object| object.map(&method(:call)) end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
yaks-0.4.4 | lib/yaks/primitivize.rb |
yaks-0.4.3 | lib/yaks/primitivize.rb |
yaks-0.4.2 | lib/yaks/primitivize.rb |