Sha256: 8f94abc82049be87a0f34f750332ab3bccfbc6b96ce7fe4ed8f686ea561ee997

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Euston
  class Command
    include ActiveModel::Validations

    def initialize body, dispatch_at = nil
      @headers = { :id => Uuid.generate,
                   :type => self.class.to_s.split('::').pop.underscore.to_sym,
                   :version => 1 }

      @body = {}

      self.class._validators.keys.reject { |k| k.to_s.start_with? '__' }.each do |key|
        @body[key] = body[key]
      end
      
      @headers[:dispatch_at] = dispatch_at unless dispatch_at.nil?
    end

    def headers
      @headers.merge :version => version
    end

    def id
      @headers[:id]
    end

    def id= value
      @headers[:id] = value
    end

    def publishing_user_id
      @headers[:user_id]
    end

    def publishing_user_id= value
      @headers[:user_id] = value
    end

    def read_attribute_for_validation key
      match = /^__(.*)/.match(key.to_s)

      if match.nil?
        @body[key]
      else
        headers[match[1].to_sym]
      end
    end

    def type
      @headers[:type]
    end

    def to_hash
      { :headers => headers, :body => @body }
    end

    def version
      @headers[:version]
    end

    def version= value
      @headers[:version] = value
    end

    attr_reader :body

    validates :__id,        :presence => true, :format => { :with => /^([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}$/ }
    validates :__type,      :presence => true
    validates :__version,   :presence => true, :numericality => { :greater_than => 0, :only_integer => true }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
euston-1.2.10 lib/euston/command.rb
euston-1.2.10-java lib/euston/command.rb