Class: BoaVista::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/boa_vista/line.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#layoutObject

Returns the value of attribute layout.



3
4
5
# File 'lib/boa_vista/line.rb', line 3

def layout
  @layout
end

Class Method Details

.prepare_for_read(value, layout) ⇒ Object



5
6
7
# File 'lib/boa_vista/line.rb', line 5

def self.prepare_for_read(value, layout)
  new(value, layout)
end

.prepare_for_write(attributes, layout) ⇒ Object



9
10
11
# File 'lib/boa_vista/line.rb', line 9

def self.prepare_for_write(attributes, layout)
  new(nil, layout, attributes)
end

Instance Method Details

#create_method_to_field(name) ⇒ Object



48
49
50
# File 'lib/boa_vista/line.rb', line 48

def create_method_to_field(name)
  define_singleton_method(name) { @fields[name].value }
end

#parseObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/boa_vista/line.rb', line 32

def parse
  @layout.attributes.each do |field, options|
    value_for_option = { value: @attributes[field] }

    @fields = @fields.merge(field => BoaVista::Field::Writer.new(field, options.merge(value_for_option)))

    create_method_to_field(field)
  end

  self
end

#readObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/boa_vista/line.rb', line 13

def read
  @layout.attributes.each do |field, options|
    value = { value: @value }

    @fields = @fields.merge(field =>
                            BoaVista::Field::Reader.new(field, options.merge(value)).call)
    create_method_to_field(field)
  end

  self
end

#to_hashObject



44
45
46
# File 'lib/boa_vista/line.rb', line 44

def to_hash
  Hash[@fields.map { |k, v| [v.name, v.value]}]
end

#writeObject



25
26
27
28
29
30
# File 'lib/boa_vista/line.rb', line 25

def write
  parse
  @value = @layout.attributes.keys.inject('') do |line, attribute|
    line << @fields.fetch(attribute).call
  end
end