Class: IOStreams::Builder
- Inherits:
-
Object
- Object
- IOStreams::Builder
- 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
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#format_options ⇒ Object
Returns the value of attribute format_options.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#streams ⇒ Object
readonly
Returns the value of attribute streams.
Instance Method Summary collapse
-
#format ⇒ Object
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.
- #format=(format) ⇒ Object
-
#initialize(file_name = nil) ⇒ Builder
constructor
A new instance of Builder.
-
#option(stream, **options) ⇒ Object
Supply an option that is only applied once the file name extensions have been parsed.
- #option_or_stream(stream, **options) ⇒ Object
-
#pipeline ⇒ Object
Returns [Hash<Symbol:Hash>] the pipeline of streams with their options that will be applied when the reader or writer is invoked.
- #reader(io_stream, &block) ⇒ Object
-
#setting(stream) ⇒ Object
Return the options set for either a stream or option.
- #stream(stream, **options) ⇒ Object
- #writer(io_stream, &block) ⇒ Object
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_name ⇒ Object
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_options ⇒ Object
Returns the value of attribute format_options.
4 5 6 |
# File 'lib/io_streams/builder.rb', line 4 def @format_options end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/io_streams/builder.rb', line 5 def @options end |
#streams ⇒ Object (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
#format ⇒ Object
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`
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, **) 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!() else @options[stream] = .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, **) if streams stream(stream, **) elsif file_name option(stream, **) else stream(stream, **) end end |
#pipeline ⇒ Object
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] = [:encode] if &.key?(:encode) opts = || {} 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 [stream] if end |
#stream(stream, **options) ⇒ Object
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, **) 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!() else @streams[stream] = .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 |