Sha256: 4dde42a6d55e92195013bc2ad36b2acaaf2f720d4e1fcb089885f7570efc913e

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Roll

  # Attributes DSL
  module Attributes

    def self.included(base)
      base.extend(ClassMethods)
    end

    #
    def valid?
      begin
        validate
        return true
      rescue ValidationError
        return false
      end
    end

    #
    def validate
      self.class.validation.each do |message, block|
        raise(ValidationError, message) unless instance_eval(&block)
      end
    end

    alias_method :assert_valid, :validate

    #

    module ClassMethods

      def instance_attributes
        @@attributes ||= []
      end

      # Define an attribute.

      def attr_accessor(name, *aliases)
        instance_attributes << name.to_s
        instance_attributes.uniq!
        super(name)
        aliases.each{ |aliaz| alias_accessor(aliaz, name) }
      end

      # Define an attribute alias.

      def alias_accessor(aliaz, name)
        alias_method aliaz, name
        alias_method "#{aliaz}=", "#{name}="
      end

      def validation
        @@validation ||= []
      end

  #     # Does this class provide open access?
  #     def open_access?
  #       false
  #     end

      def validate(message, &block)
        validation << [message, block]
      end

    end
  end
end

#
class ValidationError < Exception
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roll-0.8.0 lib/roll/attributes.rb