# encoding: UTF-8 # Persistent-๐Ÿ’Ž: Ruby gem for easily creating immutable data structures # Copyright (c) 2017 Ivo Anjo # # This file is part of Persistent-๐Ÿ’Ž. # # MIT License # # Copyright (c) 2017 Ivo Anjo # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # frozen_string_literal: true require 'persistent_dmnd/ruby_1_9_and_2_0_support' require 'hamster' module Persistent๐Ÿ’Ž class Dmndifier extend Persistent๐Ÿ’Ž DEFAULT_OPTIONS = { on_unknown: proc { |arg| raise ArgumentError, "Could not ๐Ÿ’Žify an object of class #{arg.class}. Maybe you need to implement :to_๐Ÿ’Ž?" }, }.freeze private_constant :DEFAULT_OPTIONS def self.[](arg, options = DEFAULT_OPTIONS) options = DEFAULT_OPTIONS.merge(options) case when arg.respond_to?(:to_๐Ÿ’Ž) arg.to_๐Ÿ’Ž when arg.respond_to?(:to_dmnd) arg.to_dmnd when arg.respond_to?(:to_hash) h๐Ÿ’Ž[arg.to_hash] when arg.is_a?(Hamster::Set) || arg.is_a?(Hamster::SortedSet) s๐Ÿ’Ž[*arg.to_a] when arg.respond_to?(:to_ary) a๐Ÿ’Ž[*arg.to_ary] when defined?(Concurrent::Tuple) && arg.is_a?(Concurrent::Tuple) a๐Ÿ’Ž[*arg.to_a] when arg.respond_to?(:to_set) s๐Ÿ’Ž[*arg.to_set.to_a] when defined?(Concurrent::Map) && arg.is_a?(Concurrent::Map) h๐Ÿ’Ž[Persistent๐Ÿ’Ž::Ruby19And20Support.enumerable_to_h(arg.enum_for(:each_pair))] when arg.respond_to?(:each_pair) h๐Ÿ’Ž[Persistent๐Ÿ’Ž::Ruby19And20Support.enumerable_to_h(arg.each_pair)] else options.fetch(:on_unknown).call(arg) end end end end