Sha256: be31763a0504dbce0da79ea232cda4e3a81e6506d125c5d78ea58511198525e3

Contents?: true

Size: 1.17 KB

Versions: 45

Compression:

Stored size: 1.17 KB

Contents

require 'singleton'
require 'multi_json/options'

module MultiJson
  class Adapter
    extend Options
    include Singleton
    class << self

      def defaults(action, value)
        metaclass = class << self; self; end

        metaclass.instance_eval do
          define_method("default_#{action}_options"){ value }
        end
      end

      def load(string, options={})
        instance.load(string, collect_load_options(options).clone)
      end

      def dump(object, options={})
        instance.dump(object, collect_dump_options(options).clone)
      end

    protected

      def collect_load_options(options)
        cache('load', options){ collect_options(:load_options, options).merge(options) }
      end

      def collect_dump_options(options)
        cache('dump', options){ collect_options(:dump_options, options).merge(options) }
      end

      def collect_options(method, *args)
        global, local = *[MultiJson, self].map{ |r| r.send(method, *args) }
        local.merge(global)
      end

      def cache(method, options)
        cache_key = [self, options].map(&:hash).join + method
        MultiJson.cached_options[cache_key] ||= yield
      end

    end
  end
end

Version data entries

45 entries across 44 versions & 6 rubygems

Version Path
scout-5.8.8 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.7 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.7.pre vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.6 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.5 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.5.pre vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.4 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.3 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.1 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.0 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.8.0.pre vendor/multi_json/lib/multi_json/adapter.rb
scout-5.7.5 vendor/multi_json/lib/multi_json/adapter.rb
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/multi_json-1.7.8/lib/multi_json/adapter.rb
candlepin-api-0.4.0 bundle/ruby/gems/multi_json-1.7.8/lib/multi_json/adapter.rb
scout-5.7.4 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.7.3.pre.2 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.7.3.pre vendor/multi_json/lib/multi_json/adapter.rb
scout-5.7.2 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.7.2.pre.2 vendor/multi_json/lib/multi_json/adapter.rb
scout-5.7.2.pre vendor/multi_json/lib/multi_json/adapter.rb