Class: Protip::StandardConverter
- Inherits:
-
Object
- Object
- Protip::StandardConverter
- Includes:
- Converter
- Defined in:
- lib/protip/standard_converter.rb
Constant Summary collapse
- TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON']
- FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF']
Class Attribute Summary collapse
-
.conversions ⇒ Object
readonly
Returns the value of attribute conversions.
Class Method Summary collapse
-
.value_to_boolean(value) ⇒ Object
Similar to Rails 3 value_to_boolean.
Instance Method Summary collapse
- #convertible?(message_class) ⇒ Boolean
-
#name ⇒ Object
Standard wrappers.
- #to_message(object, message_class) ⇒ Object
- #to_object(message) ⇒ Object
Class Attribute Details
.conversions ⇒ Object (readonly)
Returns the value of attribute conversions.
20 21 22 |
# File 'lib/protip/standard_converter.rb', line 20 def conversions @conversions end |
Class Method Details
.value_to_boolean(value) ⇒ Object
Similar to Rails 3 value_to_boolean
111 112 113 114 115 116 117 |
# File 'lib/protip/standard_converter.rb', line 111 def value_to_boolean(value) return true if TRUE_VALUES.include?(value) return false if FALSE_VALUES.include?(value) # If we don't get a truthy/falsey value, use the original value (which should raise an # exception) value end |
Instance Method Details
#convertible?(message_class) ⇒ Boolean
97 98 99 |
# File 'lib/protip/standard_converter.rb', line 97 def convertible?() self.class.conversions.has_key?(.descriptor.name) end |
#name ⇒ Object
Standard wrappers
60 61 62 63 64 65 66 67 |
# File 'lib/protip/standard_converter.rb', line 60 %w(Int64 Int32 UInt64 UInt32 Double Float String Bytes).map{|type| "google.protobuf.#{type}Value"}.each do |name| @conversions[name] = { to_object: ->() { .value }, to_message: lambda do |value, | .new value: value end } end |
#to_message(object, message_class) ⇒ Object
105 106 107 |
# File 'lib/protip/standard_converter.rb', line 105 def (object, ) self.class.conversions[.descriptor.name][:to_message].call(object, ) end |
#to_object(message) ⇒ Object
101 102 103 |
# File 'lib/protip/standard_converter.rb', line 101 def to_object() self.class.conversions[.class.descriptor.name][:to_object].call() end |