Module: Clusta::Serialization::TSV

Included in:
Geometry::Element
Defined in:
lib/clusta/serialization/tsv.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



61
62
63
# File 'lib/clusta/serialization/tsv.rb', line 61

def self.included klass
  klass.extend(ClassMethods)
end

Instance Method Details

#extra_inputs=(inputs) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/clusta/serialization/tsv.rb', line 41

def extra_inputs= inputs
  @extra_inputs = inputs.map do |input|
    if input =~ /^[A-Z].*;/
      self.class.from_tsv_component_string(input)
    else
      input
    end
  end
end

#extra_outputsObject



51
52
53
54
55
56
57
58
59
# File 'lib/clusta/serialization/tsv.rb', line 51

def extra_outputs
  @extra_inputs.map do |input|
    if input.respond_to?(:to_tsv_component)
      input.to_tsv_component
    else
      input
    end
  end
end

#process_args(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/clusta/serialization/tsv.rb', line 25

def process_args *args
  self.class.fields.each_with_index do |field, index|
    case
    when field[:optional]
      self.send("#{field[:name]}=", args[index]) if args[index]
    when args[index].nil?
      raise ArgumentError.new("A #{self.class} requires a non-nil value for #{field[:name]} as its #{index}#{suffix(index)} argument.")
    when field[:type] == :geometry
      self.send("#{field[:name]}=", self.class.from_tsv_component_string(args[index]))
    else
      self.send("#{field[:name]}=", args[index])
    end
  end
  self.extra_inputs = (args[self.class.fields.size..-1] || [])
end

#suffix(index) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/clusta/serialization/tsv.rb', line 16

def suffix index
  case index.to_s
  when /1$/ then 'st'
  when /2$/ then 'nd'
  when /3$/ then 'rd'
  else 'th'
  end
end

#to_flatObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/clusta/serialization/tsv.rb', line 5

def to_flat
  [stream_name].tap do |record|
    fields.each do |field|
      value = send(field[:name])
      value = value.to_tsv_component if field[:type] == :geometry
      record << value.to_s unless value.nil? && field[:optional]
    end
    record.concat(extra_outputs)
  end
end

#to_tsv_componentObject



65
66
67
# File 'lib/clusta/serialization/tsv.rb', line 65

def to_tsv_component
  to_flat.join(";")
end