Module: Protobug::Enum::InstanceMethods
- Defined in:
- lib/protobug/enum.rb
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #===)
- #as_json ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #initialize(name, value) ⇒ Object
- #pretty_print(pp) ⇒ Object
- #to_s ⇒ Object
- #to_text ⇒ Object
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 132 133 134 135 136 |
# File 'lib/protobug/enum.rb', line 120 def ==(other) case other when self.class value == other.value when Integer value == other when String # name is already a frozen String (see #initialize), so no allocation here. name == other when Symbol # Symbol#name returns the frozen interned string, avoiding the allocation # that Symbol#to_s would incur. name == other.name else false end end |
#as_json ⇒ Object
144 145 146 147 148 |
# File 'lib/protobug/enum.rb', line 144 def as_json return value unless equal?(self.class.values[value]) || equal?(self.class.values[name]) name end |
#eql?(other) ⇒ Boolean
116 117 118 |
# File 'lib/protobug/enum.rb', line 116 def eql?(other) value.eql?(other.value) end |
#hash ⇒ Object
108 109 110 |
# File 'lib/protobug/enum.rb', line 108 def hash value.hash end |
#initialize(name, value) ⇒ Object
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_s ⇒ Object
112 113 114 |
# File 'lib/protobug/enum.rb', line 112 def to_s name end |
#to_text ⇒ Object
140 141 142 |
# File 'lib/protobug/enum.rb', line 140 def to_text "#{self.class.full_name}.#{name}" end |