Class: IOStreams::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/io_streams/builder.rb

Overview

Build the streams that need to be applied to a path druing reading or writing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name = nil) ⇒ Builder

Returns a new instance of Builder.



7
8
9
10
11
12
13
# File 'lib/io_streams/builder.rb', line 7

def initialize(file_name = nil)
  @file_name     = file_name
  @streams       = nil
  @options       = nil
  @format        = nil
  @format_option = nil
end

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



4
5
6
# File 'lib/io_streams/builder.rb', line 4

def file_name
  @file_name
end

#format_optionsObject

Returns the value of attribute format_options.



4
5
6
# File 'lib/io_streams/builder.rb', line 4

def format_options
  @format_options
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#streamsObject (readonly)

Returns the value of attribute streams.



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

def streams
  @streams
end

Instance Method Details

#formatObject

Returns the tabular format if set, otherwise tries to autodetect the format if the file_name has been set Returns [nil] if no format is set, or if it cannot be determined from the file_name



95
96
97
# File 'lib/io_streams/builder.rb', line 95

def format
  @format ||= file_name ? Tabular.format_from_file_name(file_name) : nil
end

#format=(format) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/io_streams/builder.rb', line 99

def format=(format)
  unless format.nil? || IOStreams::Tabular.registered_formats.include?(format)
    raise(ArgumentError, "Invalid format: #{format.inspect}")
  end

  @format = format
end

#option(stream, **options) ⇒ Object

Supply an option that is only applied once the file name extensions have been parsed. Note:

  • Cannot set both ‘stream` and `option`

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/io_streams/builder.rb', line 18

def option(stream, **options)
  stream = stream.to_sym unless stream.is_a?(Symbol)
  raise(ArgumentError, "Invalid stream: #{stream.inspect}") unless IOStreams.extensions.include?(stream)
  raise(ArgumentError, "Cannot call both #option and #stream on the same streams instance}") if @streams
  raise(ArgumentError, "Cannot call #option unless the `file_name` was already set}") unless file_name

  @options ||= {}
  if (opts = @options[stream])
    opts.merge!(options)
  else
    @options[stream] = options.dup
  end
  self
end

#option_or_stream(stream, **options) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/io_streams/builder.rb', line 53

def option_or_stream(stream, **options)
  if streams
    stream(stream, **options)
  elsif file_name
    option(stream, **options)
  else
    stream(stream, **options)
  end
end

#pipelineObject

Returns [Hash<Symbol:Hash>] the pipeline of streams with their options that will be applied when the reader or writer is invoked.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/io_streams/builder.rb', line 80

def pipeline
  return streams.dup.freeze if streams
  return {}.freeze unless file_name

  built_streams          = {}
  # Encode stream is always first
  built_streams[:encode] = options[:encode] if options&.key?(:encode)

  opts = options || {}
  parse_extensions.each { |stream| built_streams[stream] = opts[stream] || {} }
  built_streams.freeze
end

#reader(io_stream, &block) ⇒ Object



70
71
72
# File 'lib/io_streams/builder.rb', line 70

def reader(io_stream, &block)
  execute(:reader, pipeline, io_stream, &block)
end

#setting(stream) ⇒ Object

Return the options set for either a stream or option.



64
65
66
67
68
# File 'lib/io_streams/builder.rb', line 64

def setting(stream)
  return streams[stream] if streams

  options[stream] if options
end

#stream(stream, **options) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/io_streams/builder.rb', line 33

def stream(stream, **options)
  stream = stream.to_sym unless stream.is_a?(Symbol)
  raise(ArgumentError, "Cannot call both #option and #stream on the same streams instance}") if @options

  # To prevent any streams from being applied supply a stream named `:none`
  if stream == :none
    @streams = {}
    return self
  end
  raise(ArgumentError, "Invalid stream: #{stream.inspect}") unless IOStreams.extensions.include?(stream)

  @streams ||= {}
  if (opts = @streams[stream])
    opts.merge!(options)
  else
    @streams[stream] = options.dup
  end
  self
end

#writer(io_stream, &block) ⇒ Object



74
75
76
# File 'lib/io_streams/builder.rb', line 74

def writer(io_stream, &block)
  execute(:writer, pipeline, io_stream, &block)
end