Sha256: 188a77acf2290970abdfb7024a9d715f5a6c7e005452bf615bfaafc4902387c1

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'much-plugin'
require 'mr/record'

module MR; end
module MR::Model

  module Configuration
    include MuchPlugin

    # `MR::Model::Configuration` is a mixin that provides a model's record
    # handling behavior. This includes reading/writing a model class'
    # `record_class` and reading/writing a model's `record`. These operations
    # validate what is being written to avoid confusing errors. The
    # `Configuration` mixin is a base mixin for all the other model mixins.
    #
    # * Use the `record` protected method to access the record instance.
    # * Use the `set_record` private method to write a record value.

    plugin_included do
      extend ClassMethods
      include InstanceMethods
    end

    module ClassMethods

      def record_class(*args)
        set_record_class(*args) unless args.empty?
        @record_class || raise(NoRecordClassError, "a record class hasn't been set", caller)
      end

      private

      def set_record_class(value)
        raise ArgumentError, "must be a MR::Record" unless value < MR::Record
        @record_class = value
        value.model_class = self
      end

    end

    module InstanceMethods

      def record_class
        self.class.record_class
      end

      def record
        @record || raise(NoRecordError, "a record hasn't been set", caller)
      end

      private

      def set_record(record)
        raise InvalidRecordError unless record.kind_of?(MR::Record)
        @record       = record
        @record.model = self
      end

    end

  end

  InvalidRecordError = Class.new(ArgumentError)
  NoRecordError      = Class.new(RuntimeError)
  NoRecordClassError = Class.new(RuntimeError)

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mr-0.35.2 lib/mr/model/configuration.rb