Class: Euston::CommandHeaders

Inherits:
Object
  • Object
show all
Defined in:
lib/euston/command_headers.rb

Constant Summary collapse

BASE_KEYS =
[:id, :type, :version]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ CommandHeaders

Returns a new instance of CommandHeaders.



6
7
8
9
10
11
12
13
14
15
# File 'lib/euston/command_headers.rb', line 6

def initialize hash
  arg_errors = []
  BASE_KEYS.each do |arg|
    arg_errors << arg unless hash.has_key?(arg)
  end
  raise Errors::CommandHeadersArgumentError.new("Missing args: #{arg_errors.join(", ")}") if arg_errors.size > 0
  @headers = {}.merge(hash)
  @headers[:type] = @headers[:type].to_sym
  @headers_keys = @headers.keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



42
43
44
45
# File 'lib/euston/command_headers.rb', line 42

def method_missing(name,*args,&block)
  n = name.to_sym
  is_dynamic_method?(n) ? @headers[n] : super
end

Class Method Details

.from_hash(hash) ⇒ Object



34
35
36
# File 'lib/euston/command_headers.rb', line 34

def self.from_hash hash
  self.new hash
end

Instance Method Details

#==(other) ⇒ Object



30
31
32
# File 'lib/euston/command_headers.rb', line 30

def ==(other)
  @headers == other.to_hash
end

#[](name) ⇒ Object



17
18
19
# File 'lib/euston/command_headers.rb', line 17

def [] name
  @headers[name.to_sym]
end

#idObject

only use method missing for uncommon attributes



22
# File 'lib/euston/command_headers.rb', line 22

def id()      @headers[:id]; end

#respond_to_missing?(name, incl_private) ⇒ Boolean

>= 1.9.2

Returns:

  • (Boolean)


48
49
50
# File 'lib/euston/command_headers.rb', line 48

def respond_to_missing?(name, incl_private)
  is_dynamic_method?(name.to_sym) || super
end

#to_hashObject



26
27
28
# File 'lib/euston/command_headers.rb', line 26

def to_hash
  @headers.dup
end

#to_sObject



38
39
40
# File 'lib/euston/command_headers.rb', line 38

def to_s
  "#{id} #{type} (v#{version})"
end

#typeObject



23
# File 'lib/euston/command_headers.rb', line 23

def type()    @headers[:type]; end

#versionObject



24
# File 'lib/euston/command_headers.rb', line 24

def version() @headers[:version]; end