Class: Protobuf::Enum
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Protobuf::Enum
- Extended by:
- Deprecator
- Includes:
- Optionable
- Defined in:
- lib/protobuf/enum.rb
Direct Known Subclasses
Google::Protobuf::FieldDescriptorProto::Label, Google::Protobuf::FieldDescriptorProto::Type, Google::Protobuf::FieldOptions::CType, Google::Protobuf::FileOptions::OptimizeMode, Rpc::DynamicDiscovery::BeaconType, Socketrpc::ErrorReason
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Attributes.
-
#parent_class ⇒ Object
readonly
Attributes.
-
#tag ⇒ Object
readonly
Attributes.
Class Method Summary collapse
- .aliases_allowed? ⇒ Boolean
-
.all_tags ⇒ Object
Public: Get all integer tags defined by this enum.
-
.define(name, tag) ⇒ Object
Internal: DSL method to create a new Enum.
-
.enum_for_name(name) ⇒ Object
Public: Get the Enum associated with the given name.
-
.enum_for_tag(tag) ⇒ Object
Public: Get the Enum object corresponding to the given tag.
-
.enums ⇒ Object
Public: All defined enums.
-
.enums_for_tag(tag) ⇒ Object
Public: Get an array of Enum objects with the given tag.
-
.fetch(candidate) ⇒ Object
Public: Get an Enum by a variety of type-checking mechanisms.
-
.name_for_tag(tag) ⇒ Object
Public: Get the Symbol name associated with the given number.
-
.valid_tag?(tag) ⇒ Boolean
Public: Check if the given tag is defined by this Enum.
-
.values ⇒ Object
Public: [DEPRECATED] Return a hash of Enum objects keyed by their :name.
Instance Method Summary collapse
-
#class ⇒ Object
Overriding the class so ActiveRecord/Arel visitor will visit the enum as a Fixnum.
-
#enum_by_value ⇒ Object
Class Deprecations.
-
#initialize(parent_class, name, tag) ⇒ Enum
constructor
Instance Methods.
- #inspect ⇒ Object
- #to_i ⇒ Object (also: #to_hash_value)
- #to_int ⇒ Object
- #to_s(format = :tag) ⇒ Object
-
#try(*args, &block) ⇒ Object
Re-implement
try
in order to fix the problem where the underlying fixnum doesn't respond to all methods (e.g. name or tag). - #value ⇒ Object
Methods included from Deprecator
deprecate_class_method, deprecate_method, warn_deprecated
Methods included from Optionable
Constructor Details
#initialize(parent_class, name, tag) ⇒ Enum
Instance Methods
256 257 258 259 260 261 |
# File 'lib/protobuf/enum.rb', line 256 def initialize(parent_class, name, tag) @parent_class = parent_class @name = name @tag = tag super(@tag) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Attributes
250 251 252 |
# File 'lib/protobuf/enum.rb', line 250 def name @name end |
#parent_class ⇒ Object (readonly)
Attributes
250 251 252 |
# File 'lib/protobuf/enum.rb', line 250 def parent_class @parent_class end |
#tag ⇒ Object (readonly)
Attributes
250 251 252 |
# File 'lib/protobuf/enum.rb', line 250 def tag @tag end |
Class Method Details
.aliases_allowed? ⇒ Boolean
34 35 36 |
# File 'lib/protobuf/enum.rb', line 34 def self.aliases_allowed? self.get_option(:allow_alias) end |
.all_tags ⇒ Object
Public: Get all integer tags defined by this enum.
Examples
class StateMachine < ::Protobuf::Enum set_option :allow_alias define :ON, 1 define :STARTED, 1 define :OFF, 2 end
StateMachine.all_tags #=> [ 1, 2 ]
Returns an array of unique integers.
53 54 55 |
# File 'lib/protobuf/enum.rb', line 53 def self. @all_tags ||= self.enums.map(&:to_i).uniq end |
.define(name, tag) ⇒ Object
Internal: DSL method to create a new Enum. The given name will become a constant for this Enum pointing to the new instance.
Examples
class StateMachine < ::Protobuf::Enum define :ON, 1 define :OFF, 2 end
StateMachine::ON #=> #StateMachine::ON=1 StateMachine::OFF #=> #StateMachine::OFF=2
Returns nothing.
72 73 74 75 76 77 |
# File 'lib/protobuf/enum.rb', line 72 def self.define(name, tag) enum = self.new(self, name, tag) @enums ||= [] @enums << enum const_set(name, enum) end |
.enum_for_name(name) ⇒ Object
Public: Get the Enum associated with the given name.
name - A string-like object. Case-sensitive.
Example
class StateMachine < ::Protobuf::Enum define :ON, 1 define :OFF, 2 end
StateMachine.enum_for_name(:ON) # => #StateMachine::ON=1 StateMachine.enum_for_name("ON") # => #StateMachine::ON=1 StateMachine.enum_for_name(:on) # => nil StateMachine.enum_for_name(:FOO) # => nil
Returns the Enum object with the given name or nil.
127 128 129 130 131 |
# File 'lib/protobuf/enum.rb', line 127 def self.enum_for_name(name) self.const_get(name) rescue ::NameError nil end |
.enum_for_tag(tag) ⇒ Object
Public: Get the Enum object corresponding to the given tag.
tag - An object that responds to to_i.
Returns an Enum object or nil. If the tag corresponds to multiple Enums, the first enum defined will be returned.
140 141 142 |
# File 'lib/protobuf/enum.rb', line 140 def self.enum_for_tag(tag) self.enums_for_tag(tag).first end |
.enums ⇒ Object
Public: All defined enums.
81 82 83 |
# File 'lib/protobuf/enum.rb', line 81 def self.enums @enums end |
.enums_for_tag(tag) ⇒ Object
Public: Get an array of Enum objects with the given tag.
tag - An object that responds to to_i.
Examples
class StateMachine < ::Protobuf::Enum set_option :allow_alias define :ON, 1 define :STARTED, 1 define :OFF, 2 end
StateMachine.enums_for_tag(1) #=> [ #StateMachine::ON=1, #StateMachine::STARTED=1 ] StateMachine.enums_for_tag(2) #=> [ #StateMachine::OFF=2 ]
Returns an array with zero or more Enum objects or nil.
103 104 105 106 107 |
# File 'lib/protobuf/enum.rb', line 103 def self.enums_for_tag(tag) self.enums.select { |enum| enum.to_i == tag.to_i } end |
.fetch(candidate) ⇒ Object
Public: Get an Enum by a variety of type-checking mechanisms.
candidate - An Enum, Numeric, String, or Symbol object.
Example
class StateMachine < ::Protobuf::Enum set_option :allow_alias define :ON, 1 define :STARTED, 1 define :OFF, 2 end
StateMachine.fetch(StateMachine::ON) # => #StateMachine::ON=1 StateMachine.fetch(:ON) # => #StateMachine::ON=1 StateMachine.fetch("STARTED") # => #StateMachine::STARTED=1 StateMachine.fetch(1) # => [ #StateMachine::ON=1, #StateMachine::STARTED=1 ]
Returns an Enum object or nil.
164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/protobuf/enum.rb', line 164 def self.fetch(candidate) case candidate when self then candidate when ::Numeric then enum_for_tag(candidate.to_i) when ::String, ::Symbol then enum_for_name(candidate) else nil end end |
.name_for_tag(tag) ⇒ Object
Public: Get the Symbol name associated with the given number.
number - An object that responds to to_i.
Examples
# Without aliases class StateMachine < ::Protobuf::Enum define :ON, 1 define :OFF, 2 end
StateMachine.name_for_tag(1) # => :ON StateMachine.name_for_tag(2) # => :OFF StateMachine.name_for_tag(3) # => nil
# With aliases class StateMachine < ::Protobuf::Enum set_option :allow_alias define :STARTED, 1 define :ON, 1 define :OFF, 2 end
StateMachine.name_for_tag(1) # => :STARTED StateMachine.name_for_tag(2) # => :OFF
Returns the symbol name of the enum constant given it's associated tag or nil. If the given tag has multiple names associated (due to allow_alias) the first defined name will be returned.
208 209 210 |
# File 'lib/protobuf/enum.rb', line 208 def self.name_for_tag(tag) self.enum_for_tag(tag).try(:name) end |
.valid_tag?(tag) ⇒ Boolean
Public: Check if the given tag is defined by this Enum.
number - An object that responds to to_i.
Returns a boolean.
218 219 220 |
# File 'lib/protobuf/enum.rb', line 218 def self.valid_tag?(tag) tag.respond_to?(:to_i) && self..include?(tag.to_i) end |
.values ⇒ Object
Public: [DEPRECATED] Return a hash of Enum objects keyed by their :name.
225 226 227 228 229 230 231 232 233 234 |
# File 'lib/protobuf/enum.rb', line 225 def self.values self.warn_deprecated(:values, :enums) @values ||= begin self.enums.inject({}) do |hash, enum| hash[enum.name] = enum hash end end end |
Instance Method Details
#class ⇒ Object
Overriding the class so ActiveRecord/Arel visitor will visit the enum as a Fixnum
265 266 267 |
# File 'lib/protobuf/enum.rb', line 265 def class Fixnum end |
#enum_by_value ⇒ Object
Class Deprecations
240 |
# File 'lib/protobuf/enum.rb', line 240 deprecate_class_method :enum_by_value, :enum_for_tag |
#inspect ⇒ Object
269 270 271 |
# File 'lib/protobuf/enum.rb', line 269 def inspect "\#<Protobuf::Enum(#{parent_class})::#{name}=#{tag}>" end |
#to_i ⇒ Object Also known as: to_hash_value
273 274 275 |
# File 'lib/protobuf/enum.rb', line 273 def to_i tag end |
#to_int ⇒ Object
277 278 279 |
# File 'lib/protobuf/enum.rb', line 277 def to_int tag.to_int end |
#to_s(format = :tag) ⇒ Object
281 282 283 284 285 286 287 288 289 290 |
# File 'lib/protobuf/enum.rb', line 281 def to_s(format = :tag) case format when :tag then self.to_i.to_s when :name then name.to_s else self.to_i.to_s end end |
#try(*args, &block) ⇒ Object
Re-implement try
in order to fix the problem where
the underlying fixnum doesn't respond to all methods (e.g. name or tag).
If we respond to the first argument, __send__
the args. Otherwise,
delegate the try
call to the underlying vlaue fixnum.
297 298 299 300 301 302 303 304 305 306 |
# File 'lib/protobuf/enum.rb', line 297 def try(*args, &block) case when args.empty? && block_given? yield self when respond_to?(args.first) __send__(*args, &block) else @tag.try(*args, &block) end end |
#value ⇒ Object
308 309 310 311 |
# File 'lib/protobuf/enum.rb', line 308 def value parent_class.warn_deprecated(:value, :to_i) to_i end |