Class: Rome2rio::EncodedFlags

Inherits:
Object
  • Object
show all
Defined in:
lib/rome2rio/helper/encoded_flags.rb

Direct Known Subclasses

DayFlags, SearchRequestFlags

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags = nil) ⇒ EncodedFlags

Returns a new instance of EncodedFlags.



5
6
7
# File 'lib/rome2rio/helper/encoded_flags.rb', line 5

def initialize(flags = nil)
  @flags = flags
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rome2rio/helper/encoded_flags.rb', line 24

def method_missing(meth, *args, &block)
  if meth.to_s.end_with?("?") then
    key = meth.to_s.chop.to_sym

    return true if to_a.include?(key)

    #	    return flags.include?(key) if flags.is_a?(Array)
    #	    return flags[key] if flags.is_a?(Hash)
    #	    return true if flags == key

  end

  super(meth, *args, &block)
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



3
4
5
# File 'lib/rome2rio/helper/encoded_flags.rb', line 3

def flags
  @flags
end

Class Method Details

.parse(str) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/rome2rio/helper/encoded_flags.rb', line 9

def self.parse(str)
  value = Integer(str)
  ret = new
  ret.flags = []
  self::FLAG_VALUES.each { |key, flag| ret.flags << key if (value & flag) == flag }
  ret
end

Instance Method Details

#to_aObject



17
18
19
20
21
22
# File 'lib/rome2rio/helper/encoded_flags.rb', line 17

def to_a
  return flags.map { |k, v| k if v == true || v == 1 }.select { |k| k != nil } if flags.is_a?(Hash)
  return [ flags.to_sym ] if flags.is_a?(String)
  return [ flags ] if flags.is_a?(Symbol)
  flags
end

#to_iObject



43
44
45
46
47
48
49
# File 'lib/rome2rio/helper/encoded_flags.rb', line 43

def to_i
  keys = to_a

  ret = 0
  self.class::FLAG_VALUES.each { |key, flag| ret |= flag if keys.include?(key) }
  ret
end

#to_sObject



39
40
41
# File 'lib/rome2rio/helper/encoded_flags.rb', line 39

def to_s
  to_i.to_s
end