Sha256: 727765b46dc8e857553626945e482a2f885d7f440f86d640ef45dc953c74ea23

Contents?: true

Size: 880 Bytes

Versions: 1

Compression:

Stored size: 880 Bytes

Contents

module Configurable
  module ConfigTypes
    class NestType < ObjectType
      class << self
        def init(&block)
          define_method(:init, &block) if block
          self
        end
      end
      matches Hash
      
      attr_reader :configurable
      
      def initialize(attrs={})
        @configurable = attrs[:configurable]
        
        unless configurable.respond_to?(:config) && configurable.class.respond_to?(:configs)
          raise ArgumentError, "invalid configurable: #{configurable.inspect}"
        end
        
        super
      end
      
      def init(input)
        obj = configurable.dup
        obj.config.merge!(input)
        obj
      end
      
      def cast(input)
        configurable.class.configs.import(input)
      end
      
      def uncast(value)
        configurable.class.configs.export(value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
configurable-1.0.0 lib/configurable/config_types/nest_type.rb