Module: Protobug::Enum::InstanceMethods

Defined in:
lib/protobug/enum.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



85
86
87
88
89
90
# File 'lib/protobug/enum.rb', line 85

def self.included(base)
  base.class_eval do
    attr_reader :value
    attr_reader :name
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: ===



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/protobug/enum.rb', line 120

def ==(other)
  case other
  when self.class
    value == other.value
  when Integer
    value == other
  when String, Symbol
    name.to_s == other.to_s
  else
    raise "expected #{self.class}, got #{other.inspect}"
  end
end

#as_jsonObject



139
140
141
142
143
# File 'lib/protobug/enum.rb', line 139

def as_json
  return value unless equal?(self.class.values[value]) || equal?(self.class.values[name])

  name
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/protobug/enum.rb', line 116

def eql?(other)
  value.eql?(other.value)
end

#hashObject



108
109
110
# File 'lib/protobug/enum.rb', line 108

def hash
  value.hash
end

#initialize(name, value) ⇒ Object

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
# File 'lib/protobug/enum.rb', line 92

def initialize(name, value)
  raise ArgumentError, "expected String, got #{name.inspect}" unless name.is_a? String
  raise ArgumentError, "expected Integer, got #{value.inspect}" unless value.is_a? Integer

  @name = -name
  @value = value
end

#pretty_print(pp) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/protobug/enum.rb', line 100

def pretty_print(pp)
  pp.group 0, "#{self.class}.new(", ")" do
    pp.pp name
    pp.breakable(", ")
    pp.pp value
  end
end

#to_sObject



112
113
114
# File 'lib/protobug/enum.rb', line 112

def to_s
  name
end

#to_textObject



135
136
137
# File 'lib/protobug/enum.rb', line 135

def to_text
  "#{self.class.full_name}.#{name}"
end