Sha256: eebd4baf4b19a1dc38b383efd9821e95ce911d8f6a903b81f77133e4a3cca57c

Contents?: true

Size: 1.23 KB

Versions: 13

Compression:

Stored size: 1.23 KB

Contents

module MagnumPI
  module API
    class Scheme < DSL

      def initialize
        super
        uri ::String
        format ::Symbol
      end

      def finalize(params = {})
        @types = (types = _types).deep_clone
        @values = (values = _values).deep_clone

        params.each do |name, value|
          process_value name, [value], nil
        end

        to_hash
      ensure
        @types = types
        @values = values
      end

      def deep_clone
        clone = super

        types = @types.deep_clone if @types

        clone.instance_eval do
          @types = types
        end

        clone
      end

    private

      def _types
        @types ||= {}
      end

      def process_value(name, args, block)
        value = args[0]
        if name == :resources
          (_values[name] ||= {}).merge! value
        else
          is_a_class = value.class == ::Class
          set_type name, is_a_class ? value : value.class
          set_value name, value unless is_a_class
        end
      end

      def set_type(name, type)
        _types[name] ||= type
      end

      def value_error_message(name, value)
        unless value.is_a?(type = _types[name])
          "expected #{type}"
        end
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
magnum-pi-0.2.11 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.10 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.9 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.8 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.7 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.6 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.5 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.4 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.3 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.2 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.1 lib/magnum-pi/api/scheme.rb
magnum-pi-0.2.0 lib/magnum-pi/api/scheme.rb
magnum-pi-0.1.4 lib/magnum-pi/api/scheme.rb