Class: Streamer::FactProviders::CSVProvider
- Inherits:
-
Object
- Object
- Streamer::FactProviders::CSVProvider
- Defined in:
- lib/streamer/fact_providers/csv_provider.rb
Overview
CSVProvider implements Finder Provider interface
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #field_number(field) ⇒ Object
- #find(key) ⇒ Object
- #hash_result ⇒ Object
- #headers ⇒ Object
-
#initialize(path) ⇒ CSVProvider
constructor
A new instance of CSVProvider.
- #lines ⇒ Object
- #processing_statement ⇒ Object
- #scalar_result ⇒ Object
- #segments ⇒ Object
Constructor Details
#initialize(path) ⇒ CSVProvider
Returns a new instance of CSVProvider.
9 10 11 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 9 def initialize(path) @path = path end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 7 def key @key end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 7 def path @path end |
Instance Method Details
#field_number(field) ⇒ Object
39 40 41 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 39 def field_number(field) headers.index(field) + 1 end |
#find(key) ⇒ Object
13 14 15 16 17 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 13 def find(key) @key = key return hash_result if segments.size == 2 return scalar_result if segments.size == 3 end |
#hash_result ⇒ Object
23 24 25 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 23 def hash_result Hash[headers.zip CSV.parse_line(lines)] end |
#headers ⇒ Object
35 36 37 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 35 def headers @headers ||= CSV.parse_line(`head -n 1 #{path}`) end |
#lines ⇒ Object
31 32 33 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 31 def lines `#{processing_statement}`.strip end |
#processing_statement ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 43 def processing_statement segs = segments return unless segs.size > 1 && segs.size < 4 <<-STMT awk -F'","|,' '($#{field_number(segs[0])} ~ /^#{segs[1]}$/){print $0}' #{path} STMT end |
#scalar_result ⇒ Object
27 28 29 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 27 def scalar_result hash_result[segments[2]] end |
#segments ⇒ Object
19 20 21 |
# File 'lib/streamer/fact_providers/csv_provider.rb', line 19 def segments key.split('.') end |