Module: RSMP::MessageSxlCodec::ClassMethods
- Defined in:
- lib/rsmp/message/sxl_codec.rb
Overview
Class-level value transforms used by Message and command collectors.
Constant Summary collapse
- STRING_TYPES =
%w[string base64 timestamp].freeze
- INTEGER_AS_STRING_TYPES =
%w[integer_as_string long_as_string].freeze
Instance Method Summary collapse
- #decode_sxl_array(value, descriptor) ⇒ Object
- #decode_sxl_array_item(item, descriptor) ⇒ Object
- #decode_sxl_boolean(value) ⇒ Object
- #decode_sxl_integer(value) ⇒ Object
- #decode_sxl_list(value, type) ⇒ Object
- #decode_sxl_number(value) ⇒ Object
- #decode_sxl_object(value, properties) ⇒ Object
- #decode_sxl_value(value, descriptor) ⇒ Object
- #descriptor_type(descriptor) ⇒ Object
- #encode_string_type?(type) ⇒ Boolean
- #encode_sxl_array(value, descriptor) ⇒ Object
- #encode_sxl_boolean(value) ⇒ Object
- #encode_sxl_list(value) ⇒ Object
- #encode_sxl_object(value, properties) ⇒ Object
- #encode_sxl_value(value, descriptor) ⇒ Object
- #list_item_descriptor(type) ⇒ Object
- #list_type?(type) ⇒ Boolean
- #transform_sxl_object(value, properties, transformer) ⇒ Object
Instance Method Details
#decode_sxl_array(value, descriptor) ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/rsmp/message/sxl_codec.rb', line 167 def decode_sxl_array(value, descriptor) return value unless value.is_a?(Array) items = descriptor['items'] return value unless items.is_a?(Hash) value.map { |item| decode_sxl_array_item(item, items) } end |
#decode_sxl_array_item(item, descriptor) ⇒ Object
176 177 178 |
# File 'lib/rsmp/message/sxl_codec.rb', line 176 def decode_sxl_array_item(item, descriptor) descriptor['type'] ? decode_sxl_value(item, descriptor) : decode_sxl_object(item, descriptor) end |
#decode_sxl_boolean(value) ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rsmp/message/sxl_codec.rb', line 127 def decode_sxl_boolean(value) case value when 'True' true when 'False' false else value end end |
#decode_sxl_integer(value) ⇒ Object
138 139 140 141 142 |
# File 'lib/rsmp/message/sxl_codec.rb', line 138 def decode_sxl_integer(value) return value unless value.is_a?(String) && value.match?(/\A[+-]?\d+\z/) value.to_i end |
#decode_sxl_list(value, type) ⇒ Object
152 153 154 155 156 |
# File 'lib/rsmp/message/sxl_codec.rb', line 152 def decode_sxl_list(value, type) items = value.is_a?(String) ? value.split(',') : Array(value) descriptor = list_item_descriptor(type) items.map { |item| decode_sxl_value(item, descriptor) } end |
#decode_sxl_number(value) ⇒ Object
144 145 146 147 148 149 150 |
# File 'lib/rsmp/message/sxl_codec.rb', line 144 def decode_sxl_number(value) return value unless value.is_a?(String) Float(value) rescue ArgumentError value end |
#decode_sxl_object(value, properties) ⇒ Object
180 181 182 |
# File 'lib/rsmp/message/sxl_codec.rb', line 180 def decode_sxl_object(value, properties) transform_sxl_object(value, properties, :decode_sxl_value) end |
#decode_sxl_value(value, descriptor) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rsmp/message/sxl_codec.rb', line 70 def decode_sxl_value(value, descriptor) return nil if value.nil? type = descriptor_type(descriptor) return decode_sxl_boolean(value) if type == 'boolean_as_string' return decode_sxl_integer(value) if INTEGER_AS_STRING_TYPES.include?(type) return decode_sxl_number(value) if type == 'number_as_string' return decode_sxl_list(value, type) if list_type?(type) return decode_sxl_array(value, descriptor) if type == 'array' return decode_sxl_object(value, descriptor['properties']) if type == 'object' value end |
#descriptor_type(descriptor) ⇒ Object
84 85 86 |
# File 'lib/rsmp/message/sxl_codec.rb', line 84 def descriptor_type(descriptor) descriptor.is_a?(Hash) ? descriptor['type'] : descriptor.to_s end |
#encode_string_type?(type) ⇒ Boolean
92 93 94 |
# File 'lib/rsmp/message/sxl_codec.rb', line 92 def encode_string_type?(type) STRING_TYPES.include?(type) || INTEGER_AS_STRING_TYPES.include?(type) || type == 'number_as_string' end |
#encode_sxl_array(value, descriptor) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/rsmp/message/sxl_codec.rb', line 114 def encode_sxl_array(value, descriptor) return value unless value.is_a?(Array) items = descriptor['items'] return value unless items.is_a?(Hash) value.map { |item| encode_sxl_object(item, items) } end |
#encode_sxl_boolean(value) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rsmp/message/sxl_codec.rb', line 96 def encode_sxl_boolean(value) case value when true 'True' when false 'False' else value end end |
#encode_sxl_list(value) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/rsmp/message/sxl_codec.rb', line 107 def encode_sxl_list(value) return value if value.is_a?(String) return encode_sxl_boolean(value).to_s unless value.is_a?(Array) value.map { |item| encode_sxl_boolean(item) }.join(',') end |
#encode_sxl_object(value, properties) ⇒ Object
123 124 125 |
# File 'lib/rsmp/message/sxl_codec.rb', line 123 def encode_sxl_object(value, properties) transform_sxl_object(value, properties, :encode_sxl_value) end |
#encode_sxl_value(value, descriptor) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rsmp/message/sxl_codec.rb', line 57 def encode_sxl_value(value, descriptor) return nil if value.nil? type = descriptor_type(descriptor) return encode_sxl_boolean(value) if type == 'boolean_as_string' return encode_sxl_list(value) if list_type?(type) return value.is_a?(String) ? value : value.to_s if encode_string_type?(type) return encode_sxl_array(value, descriptor) if type == 'array' return encode_sxl_object(value, descriptor['properties']) if type == 'object' value end |
#list_item_descriptor(type) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/rsmp/message/sxl_codec.rb', line 158 def list_item_descriptor(type) case type when /\Aboolean_list/ then 'boolean_as_string' when /\Ainteger_list/ then 'integer_as_string' when /\Anumber_list/ then 'number_as_string' else 'string' end end |
#list_type?(type) ⇒ Boolean
88 89 90 |
# File 'lib/rsmp/message/sxl_codec.rb', line 88 def list_type?(type) type.match?(/_list(_as_string)?\z/) end |
#transform_sxl_object(value, properties, transformer) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/rsmp/message/sxl_codec.rb', line 184 def transform_sxl_object(value, properties, transformer) return value unless value.is_a?(Hash) && properties.is_a?(Hash) value.each_with_object({}) do |(key, item_value), memo| descriptor = properties[key] || properties[key.to_sym] memo[key] = descriptor ? public_send(transformer, item_value, descriptor) : item_value end end |