brainopia/cassandra-mapper

View on GitHub
lib/cassandra/mapper/utility/config.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Cassandra::Mapper::Utility
  class Config
    extend DelegateKeys
    delegate_keys 'dsl.options', :key, :subkey, :types, :before_insert,
                                 :after_insert, :after_remove, :before_remove

    def initialize(&block)
      @dsl = DSL.new &block
    end

    def type(field)
      types[field.to_sym]
    end

    def dsl(&block)
      @dsl.instance_eval(&block) if block
      @dsl
    end

    class DSL
      attr_reader :options

      def initialize(&block)
        @options = { types: {}, subkey: [] }
        reset_callbacks!
        instance_eval &block
      end

      def key(*fields)
        @options[:key] = fields
      end

      def subkey(*fields)
        @options[:subkey] = fields
      end

      def type(field, type)
        @options[:types][field.to_sym] = type
      end

      def reset_callbacks!
        @options.merge! \
          before_insert: [],
          before_remove: [],
          after_insert:  [],
          after_remove:  []
      end

      def before_insert(&block)
        @options[:before_insert].push block
      end

      def before_remove(&block)
        @options[:before_remove].push block
      end

      def after_insert(&block)
        @options[:after_insert].push block
      end

      def after_remove(&block)
        @options[:after_remove].push block
      end
    end
  end
end