Class: Protobuf::Field::BaseField
- Inherits:
-
Object
- Object
- Protobuf::Field::BaseField
- Defined in:
- lib/protobuf/message/field.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#message_class ⇒ Object
Returns the value of attribute message_class.
-
#name ⇒ Object
Returns the value of attribute name.
-
#rule ⇒ Object
Returns the value of attribute rule.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #acceptable?(val) ⇒ Boolean
- #clear(message) ⇒ Object
- #default_value ⇒ Object
- #descriptor ⇒ Object
- #error_message ⇒ Object
- #get(value) ⇒ Object
-
#initialize(message_class, rule, type, name, tag, opts = {}) ⇒ BaseField
constructor
A new instance of BaseField.
- #initialized?(message) ⇒ Boolean
- #max ⇒ Object
- #merge(message_instance, value) ⇒ Object
- #merge_array(message_instance, value) ⇒ Object
- #merge_value(message_instance, value) ⇒ Object
- #min ⇒ Object
- #optional? ⇒ Boolean
- #ready? ⇒ Boolean
-
#repeated? ⇒ Boolean
utility methods.
- #required? ⇒ Boolean
-
#set(message_instance, bytes) ⇒ Object
encoder/decoder related methods.
- #to_s ⇒ Object
- #typed_default_value(default = nil) ⇒ Object
Constructor Details
#initialize(message_class, rule, type, name, tag, opts = {}) ⇒ BaseField
Returns a new instance of BaseField.
35 36 37 38 39 40 |
# File 'lib/protobuf/message/field.rb', line 35 def initialize(, rule, type, name, tag, opts={}) @message_class, @rule, @type, @name, @tag, @default, @extension = , rule, type, name, tag, opts[:default], opts[:extension] @error_message = 'Type invalid' define_accessor end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
29 30 31 |
# File 'lib/protobuf/message/field.rb', line 29 def default @default end |
#message_class ⇒ Object
Returns the value of attribute message_class.
29 30 31 |
# File 'lib/protobuf/message/field.rb', line 29 def @message_class end |
#name ⇒ Object
Returns the value of attribute name.
29 30 31 |
# File 'lib/protobuf/message/field.rb', line 29 def name @name end |
#rule ⇒ Object
Returns the value of attribute rule.
29 30 31 |
# File 'lib/protobuf/message/field.rb', line 29 def rule @rule end |
#tag ⇒ Object
Returns the value of attribute tag.
29 30 31 |
# File 'lib/protobuf/message/field.rb', line 29 def tag @tag end |
#type ⇒ Object
Returns the value of attribute type.
29 30 31 |
# File 'lib/protobuf/message/field.rb', line 29 def type @type end |
Class Method Details
.default ⇒ Object
26 |
# File 'lib/protobuf/message/field.rb', line 26 def default; nil end |
.descriptor ⇒ Object
22 23 24 |
# File 'lib/protobuf/message/field.rb', line 22 def descriptor @descriptor ||= Protobuf::Descriptor::FieldDescriptor.new end |
Instance Method Details
#acceptable?(val) ⇒ Boolean
182 183 184 |
# File 'lib/protobuf/message/field.rb', line 182 def acceptable?(val) true end |
#clear(message) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/protobuf/message/field.rb', line 59 def clear() if repeated? [name].clear else .instance_variable_get(:@values).delete(name) end end |
#default_value ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/protobuf/message/field.rb', line 67 def default_value case rule when :repeated FieldArray.new self when :required nil when :optional typed_default_value default else raise InvalidRuleError end end |
#descriptor ⇒ Object
31 32 33 |
# File 'lib/protobuf/message/field.rb', line 31 def descriptor @descriptor ||= Protobuf::Descriptor::FieldDescriptor.new self end |
#error_message ⇒ Object
186 187 188 |
# File 'lib/protobuf/message/field.rb', line 186 def @error_message end |
#get(value) ⇒ Object
143 144 145 |
# File 'lib/protobuf/message/field.rb', line 143 def get(value) encode(value) end |
#initialized?(message) ⇒ Boolean
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/protobuf/message/field.rb', line 44 def initialized?() case rule when :required return false if [name].nil? return false if is_a?(Protobuf::Field::MessageField) and not [name].initialized? when :repeated return [name].all? {|msg| (not is_a?(Protobuf::Field::MessageField)) or msg.initialized? } when :optional return false if [name] and is_a?(Protobuf::Field::MessageField) and not [name].initialized? end true end |
#max ⇒ Object
179 |
# File 'lib/protobuf/message/field.rb', line 179 def max; self.class.max end |
#merge(message_instance, value) ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/protobuf/message/field.rb', line 157 def merge(, value) if repeated? merge_array , value else merge_value , value end end |
#merge_array(message_instance, value) ⇒ Object
165 166 167 |
# File 'lib/protobuf/message/field.rb', line 165 def merge_array(, value) [tag].concat value end |
#merge_value(message_instance, value) ⇒ Object
169 170 171 |
# File 'lib/protobuf/message/field.rb', line 169 def merge_value(, value) [tag] = value end |
#min ⇒ Object
180 |
# File 'lib/protobuf/message/field.rb', line 180 def min; self.class.min end |
#optional? ⇒ Boolean
177 |
# File 'lib/protobuf/message/field.rb', line 177 def optional?; rule == :optional end |
#ready? ⇒ Boolean
42 |
# File 'lib/protobuf/message/field.rb', line 42 def ready?; true end |
#repeated? ⇒ Boolean
utility methods
175 |
# File 'lib/protobuf/message/field.rb', line 175 def repeated?; rule == :repeated end |
#required? ⇒ Boolean
176 |
# File 'lib/protobuf/message/field.rb', line 176 def required?; rule == :required end |
#set(message_instance, bytes) ⇒ Object
encoder/decoder related methods
134 135 136 137 138 139 140 141 |
# File 'lib/protobuf/message/field.rb', line 134 def set(, bytes) value = decode(bytes) if repeated? .send(name) << value else .send("#{name}=", value) end end |
#to_s ⇒ Object
190 191 192 |
# File 'lib/protobuf/message/field.rb', line 190 def to_s "#{rule} #{type} #{name} = #{tag} #{default ? "[default=#{default}]" : ''}" end |
#typed_default_value(default = nil) ⇒ Object
80 81 82 |
# File 'lib/protobuf/message/field.rb', line 80 def typed_default_value(default=nil) default or self.class.default end |