Class: CSVPP::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/csvpp/format.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format) ⇒ Format

Returns a new instance of Format.

Parameters:

  • format (Hash)


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, meta| meta['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_sepObject (readonly)

Returns the value of attribute col_sep.



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

def col_sep
  @col_sep
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#skipObject (readonly)

Returns the value of attribute skip.



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

def skip
  @skip
end

Class Method Details

.load(path) ⇒ Format

Parameters:

  • path (String)

    path to format file

Returns:



7
8
9
10
# File 'lib/csvpp/format.rb', line 7

def self.load(path)
  return path if path.is_a? Format
  load_from_str File.read(path)
end

.load_from_str(json) ⇒ Format

Parameters:

  • json (String)

Returns:



14
15
16
17
# File 'lib/csvpp/format.rb', line 14

def self.load_from_str(json)
  return json if json.is_a? Format
  new Oj.load(json)
end

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.

Returns:

  • (Array)

    all values that should be interpreted as ‘false` for this 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

#lengthObject



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).

Parameters:

  • var (String)

    : name of the variable for which the missings are required

Returns:

  • (Array)

    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

Returns:

  • (Boolean)


94
95
96
# File 'lib/csvpp/format.rb', line 94

def multiline?
  @multiline
end

#multiline_start?(line_id) ⇒ Boolean

Returns:

  • (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_sObject



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.

Returns:

  • (Array)

    all values that should be interpreted as true for this 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_namesObject



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)
  .fetch(line_id)
end