Class: CSVPP::Format
- Inherits:
-
Object
- Object
- CSVPP::Format
- Defined in:
- lib/csvpp/format.rb
Instance Attribute Summary collapse
-
#col_sep ⇒ Object
readonly
Returns the value of attribute col_sep.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#skip ⇒ Object
readonly
Returns the value of attribute skip.
Class Method Summary collapse
Instance Method Summary collapse
-
#false_values(var) ⇒ Array
Returns the values that are defined as ‘false` in the the format’s json definition for the given variable.
- #index(var) ⇒ Object
-
#initialize(format) ⇒ Format
constructor
A new instance of Format.
- #length ⇒ Object
-
#missings(var) ⇒ Array
An array of missing values (can be empty if no missings were defined).
- #multiline? ⇒ Boolean
- #multiline_start?(line_id) ⇒ Boolean
- #position(var) ⇒ Object
- #to_s ⇒ Object
-
#true_values(var) ⇒ Array
Returns the values that are defined as ‘true` in the the format’s json definition for the given variable.
- #type(var) ⇒ Object
- #var_names ⇒ Object
- #vars_for_line(line_id) ⇒ Object
Constructor Details
#initialize(format) ⇒ Format
Returns a new instance of Format.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/csvpp/format.rb', line 20 def initialize(format) @name = format['name'] @description = format['description'] @multiline = format['multiline'].to_s.strip.downcase == 'true' @col_sep = format['column_separator'] @skip = format['skip'].to_i @vars = format.fetch('vars') if multiline? @vars_grouped_by_line = Hash[ vars.group_by { |var, | ['line'] }.map do |line_id, vars| [line_id, vars.map { |var, *| var }] end ] @multiline_start = format.fetch('start') end # Cache for actual indices because formats provide 1-based human readable # positions. Only matters when parsing files with 30k+ line files. See # #index(var). @indices = {} end |
Instance Attribute Details
#col_sep ⇒ Object (readonly)
Returns the value of attribute col_sep.
3 4 5 |
# File 'lib/csvpp/format.rb', line 3 def col_sep @col_sep end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
3 4 5 |
# File 'lib/csvpp/format.rb', line 3 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/csvpp/format.rb', line 3 def name @name end |
#skip ⇒ Object (readonly)
Returns the value of attribute skip.
3 4 5 |
# File 'lib/csvpp/format.rb', line 3 def skip @skip end |
Class Method Details
Instance Method Details
#false_values(var) ⇒ Array
Returns the values that are defined as ‘false` in the the format’s json definition for the given variable.
81 82 83 84 |
# File 'lib/csvpp/format.rb', line 81 def false_values(var) return [] unless type(var) == "boolean" array_from(var, 'false_values') end |
#index(var) ⇒ Object
52 53 54 |
# File 'lib/csvpp/format.rb', line 52 def index(var) @indices[var] ||= position(var) - 1 end |
#length ⇒ Object
48 49 50 |
# File 'lib/csvpp/format.rb', line 48 def length var_names.count end |
#missings(var) ⇒ Array
Returns an array of missing values (can be empty if no missings were defined).
66 67 68 |
# File 'lib/csvpp/format.rb', line 66 def missings(var) array_from(var, 'missings') end |
#multiline? ⇒ Boolean
94 95 96 |
# File 'lib/csvpp/format.rb', line 94 def multiline? @multiline end |
#multiline_start?(line_id) ⇒ Boolean
90 91 92 |
# File 'lib/csvpp/format.rb', line 90 def multiline_start?(line_id) multiline_start == line_id end |
#position(var) ⇒ Object
56 57 58 |
# File 'lib/csvpp/format.rb', line 56 def position(var) vars.fetch(var)['position'] end |
#to_s ⇒ Object
98 99 100 |
# File 'lib/csvpp/format.rb', line 98 def to_s "#{name.ljust(30)}\t| #{description}" end |
#true_values(var) ⇒ Array
Returns the values that are defined as ‘true` in the the format’s json definition for the given variable.
73 74 75 76 |
# File 'lib/csvpp/format.rb', line 73 def true_values(var) return [] unless type(var) == "boolean" array_from(var, 'true_values') end |
#type(var) ⇒ Object
60 61 62 |
# File 'lib/csvpp/format.rb', line 60 def type(var) vars.fetch(var)['type'] end |
#var_names ⇒ Object
44 45 46 |
# File 'lib/csvpp/format.rb', line 44 def var_names vars.keys end |
#vars_for_line(line_id) ⇒ Object
86 87 88 |
# File 'lib/csvpp/format.rb', line 86 def vars_for_line(line_id) vars_grouped_by_line.fetch(line_id) end |