Sha256: 9ac24db68b2d7d9510a252e613c1764b65568749b24fdf803913d8af4b9b54ed

Contents?: true

Size: 1.67 KB

Versions: 13

Compression:

Stored size: 1.67 KB

Contents

require 'synchronisable/dsl/macro'

module Synchronisable
  module DSL
    module Associations
      # Association builder.
      # Subclasses must implement #macro method.
      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 force_sync)

        attr_reader :name, :model, :key,
          :required, :force_sync

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

        def create(options)
          validate_options(options)

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

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

          set_defaults

          @synchronizer.associations[@key] = self
        end

        def macro
          raise NotImplementedError
        end

        def model_name
          @model.to_s.demodulize.underscore.to_sym
        end

        protected

        def set_defaults
          @required ||= false
          @force_sync ||= false

          @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

13 entries across 13 versions & 1 rubygems

Version Path
synchronisable-1.1.9 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.8 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.7 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.6 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.5 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.4 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.3 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.2 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.1 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.1.0 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.0.9 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.0.8 lib/synchronisable/dsl/associations/association.rb
synchronisable-1.0.7 lib/synchronisable/dsl/associations/association.rb