Sha256: 758ff7dd31a747da34148352f2f8314afbefdec6fcc64c2304da7abb817e3b67

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

require 'synchronisable/dsl/macro'

module Synchronisable
  module DSL
    module Associations
      # Association builder.
      class Association
        include Synchronisable::DSL::Macro

        attribute :key_suffix, default: -> { raise NotImplementedError }

        class << self
          attr_accessor :valid_options

          def create(synchronizer, name, options)
            new(synchronizer, name).create(options)
          end
        end

        self.valid_options = %i(key class_name required type)

        attr_reader :name, :model, :key, :required, :type

        def initialize(synchronizer, name)
          @synchronizer, @name = synchronizer, name.to_sym
        end

        def create(options)
          validate_options(options)

          @key = options[:key]
          @required = options[:required]
          @type = options[:type]

          if options[:class_name].present?
            @model = options[:class_name].constantize
          end

          set_defaults

          @synchronizer.associations[@key] = self
        end

        protected

        def set_defaults
          @required ||= false
          @type ||= :ids

          @model ||= @name.to_s.classify.constantize
          @key = "#{@name}_#{self.class.key_suffix}" unless @key.present?
        end

        private

        def validate_options(options)
          options.assert_valid_keys(Association.valid_options)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
synchronisable-0.0.9 lib/synchronisable/dsl/associations/association.rb
synchronisable-0.0.8 lib/synchronisable/dsl/associations/association.rb
synchronisable-0.0.7 lib/synchronisable/dsl/associations/association.rb
synchronisable-0.0.6 lib/synchronisable/dsl/associations/association.rb
synchronisable-0.0.5 lib/synchronisable/dsl/associations/association.rb
synchronisable-0.0.4 lib/synchronisable/dsl/associations/association.rb