Module: Clusta::Serialization::JSON
- Included in:
- Geometry::Element
- Defined in:
- lib/clusta/serialization/json.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(klass) ⇒ Object
72 73 74 |
# File 'lib/clusta/serialization/json.rb', line 72 def self.included klass klass.extend(ClassMethods) end |
Instance Method Details
#non_key_field_data ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/clusta/serialization/json.rb', line 28 def non_key_field_data {}.tap do |data| non_key_fields.each do |field| value = send(field[:name]) value = value.to_hash if value.respond_to?(:to_hash) data[field[:name]] = value unless value.nil? && field[:optional] end end end |
#process_args(*args) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/clusta/serialization/json.rb', line 38 def process_args *args json_index = 0 self.class.keys.each_with_index do |key, index| self.send("#{key[:name]}=", args[index]) json_index = index + 1 end if args[json_index] if args[json_index].is_a?(Hash) data = args[json_index] else data = ::JSON.parse(args[json_index]) end else data = {} end non_key_fields.each do |field| name = field[:name].to_s case when field[:optional] self.send("#{name}=", data[name]) if data.has_key?(name) when (!data.has_key?(name)) raise ArgumentError.new("A #{self.class} requires a non-nil value for #{name}.") when field[:type] == :geometry self.send("#{name}=", data[name]) else self.send("#{name}=", data[name]) end end self.extra_inputs = (args[(json_index + 1)..-1] || []) end |
#to_flat ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/clusta/serialization/json.rb', line 17 def to_flat [stream_name].tap do |record| keys.each do |key| record << self.send(key[:name]) end data = non_key_field_data record << data.to_json unless data.empty? record.concat(extra_outputs) end end |
#to_hash ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/clusta/serialization/json.rb', line 7 def to_hash {}.tap do |json| fields.each do |field| value = send(field[:name]) value = value.to_hash if value.respond_to?(:to_hash) json[field[:name]] = value end end end |