Class: Csvlint::Csvw::Column

Inherits:
Object
  • Object
show all
Includes:
ErrorCollector
Defined in:
lib/csvlint/csvw/column.rb

Instance Attribute Summary collapse

Attributes included from ErrorCollector

#errors, #info_messages, #warnings

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ErrorCollector

#build_errors, #build_info_messages, #build_warnings, #reset, #valid?

Constructor Details

#initialize(number, name, id: nil, about_url: nil, datatype: { "@id" => "http://www.w3.org/2001/XMLSchema#string" }, default: "", lang: "und", null: [""], ordered: false, property_url: nil, required: false, separator: nil, source_number: nil, suppress_output: false, text_direction: :inherit, titles: {}, value_url: nil, virtual: false, annotations: [], warnings: []) ⇒ Column

Returns a new instance of Column.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/csvlint/csvw/column.rb', line 8

def initialize(number, name, id: nil, about_url: nil, datatype: { "@id" => "http://www.w3.org/2001/XMLSchema#string" }, default: "", lang: "und", null: [""], ordered: false, property_url: nil, required: false, separator: nil, source_number: nil, suppress_output: false, text_direction: :inherit, titles: {}, value_url: nil, virtual: false, annotations: [], warnings: [])
  @number = number
  @name = name
  @id = id
  @about_url = about_url
  @datatype = datatype
  @default = default
  @lang = lang
  @null = null
  @ordered = ordered
  @property_url = property_url
  @required = required
  @separator = separator
  @source_number = source_number || number
  @suppress_output = suppress_output
  @text_direction = text_direction
  @titles = titles
  @value_url = value_url
  @virtual = virtual
  @annotations = annotations
  reset
  @warnings += warnings
end

Instance Attribute Details

#about_urlObject (readonly)

Returns the value of attribute about_url.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def about_url
  @about_url
end

#annotationsObject (readonly)

Returns the value of attribute annotations.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def annotations
  @annotations
end

#datatypeObject (readonly)

Returns the value of attribute datatype.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def datatype
  @datatype
end

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def default
  @default
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def id
  @id
end

#langObject (readonly)

Returns the value of attribute lang.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def lang
  @lang
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def null
  @null
end

#numberObject (readonly)

Returns the value of attribute number.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def number
  @number
end

#orderedObject (readonly)

Returns the value of attribute ordered.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def ordered
  @ordered
end

#property_urlObject (readonly)

Returns the value of attribute property_url.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def property_url
  @property_url
end

#requiredObject (readonly)

Returns the value of attribute required.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def required
  @required
end

#separatorObject (readonly)

Returns the value of attribute separator.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def separator
  @separator
end

#source_numberObject (readonly)

Returns the value of attribute source_number.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def source_number
  @source_number
end

#suppress_outputObject (readonly)

Returns the value of attribute suppress_output.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def suppress_output
  @suppress_output
end

#text_directionObject (readonly)

Returns the value of attribute text_direction.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def text_direction
  @text_direction
end

#titlesObject (readonly)

Returns the value of attribute titles.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def titles
  @titles
end

#value_urlObject (readonly)

Returns the value of attribute value_url.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def value_url
  @value_url
end

#virtualObject (readonly)

Returns the value of attribute virtual.



6
7
8
# File 'lib/csvlint/csvw/column.rb', line 6

def virtual
  @virtual
end

Class Method Details

.create_date_parser(type, warning) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/csvlint/csvw/column.rb', line 111

def create_date_parser(type, warning)
  return lambda { |value, format|
    format = Csvlint::Csvw::DateFormat.new(nil, type) if format.nil?
    v = format.parse(value)
    return nil, warning if v.nil?
    return v, nil
  }
end

.create_regexp_based_parser(regexp, warning) ⇒ Object



120
121
122
123
124
125
# File 'lib/csvlint/csvw/column.rb', line 120

def create_regexp_based_parser(regexp, warning)
  return lambda { |value, format|
    return nil, warning unless value =~ regexp
    return value, nil
  }
end

.from_json(number, column_desc, base_url = nil, lang = "und", inherited_properties = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/csvlint/csvw/column.rb', line 32

def self.from_json(number, column_desc, base_url=nil, lang="und", inherited_properties={})
  annotations = {}
  warnings = []
  column_properties = {}
  inherited_properties = inherited_properties.clone

  column_desc.each do |property,value|
    if property == "@type"
      raise Csvlint::Csvw::MetadataError.new("columns[#{number}].@type"), "@type of column is not 'Column'" if value != 'Column'
    else
      v, warning, type = Csvw::PropertyChecker.check_property(property, value, base_url, lang)
      warnings += Array(warning).map{ |w| Csvlint::ErrorMessage.new(w, :metadata, nil, nil, "#{property}: #{value}", nil) } unless warning.nil? || warning.empty?
      if type == :annotation
        annotations[property] = v
      elsif type == :common || type == :column
        column_properties[property] = v
      elsif type == :inherited
        inherited_properties[property] = v
      else
        warnings << Csvlint::ErrorMessage.new(:invalid_property, :metadata, nil, nil, "column: #{property}", nil)
      end
    end
  end

  return self.new(number, column_properties["name"],
    id: column_properties["@id"],
    datatype: inherited_properties["datatype"] || { "@id" => "http://www.w3.org/2001/XMLSchema#string" },
    lang: inherited_properties["lang"] || "und",
    null: inherited_properties["null"] || [""],
    property_url: column_desc["propertyUrl"],
    required: inherited_properties["required"] || false,
    separator: inherited_properties["separator"],
    titles: column_properties["titles"],
    virtual: column_properties["virtual"] || false,
    annotations: annotations,
    warnings: warnings
  )
end

.languages_match(l1, l2) ⇒ Object



127
128
129
130
131
# File 'lib/csvlint/csvw/column.rb', line 127

def languages_match(l1, l2)
  return true if l1 == l2 || l1 == "und" || l2 == "und"
  return true if l1 =~ Regexp.new("^#{l2}-") || l2 =~ Regexp.new("^#{l1}-")
  return false
end

Instance Method Details

#parse(string_value, row = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/csvlint/csvw/column.rb', line 92

def parse(string_value, row=nil)
  return nil if null.include? string_value
  string_values = @separator.nil? ? [string_value] : string_value.split(@separator)
  values = []
  string_values.each do |s|
    value, warning = DATATYPE_PARSER[@datatype["base"] || @datatype["@id"]].call(s, @datatype["format"])
    if warning.nil?
      values << value
    else
      build_errors(warning, :schema, row, @number, s, @datatype)
      values << s
    end
  end
  return values
end

#validate(string_value, row = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/csvlint/csvw/column.rb', line 78

def validate(string_value, row=nil)
  reset
  values = parse(string_value || "", row)
  # STDERR.puts "#{name} - #{string_value.inspect} - #{values.inspect}"
  values.each do |value|
    validate_required(value, row)
    validate_format(value, row)
    validate_length(value, row)
    validate_value(value, row)
  end unless values.nil?
  validate_required(values, row) if values.nil?
  return valid?
end

#validate_header(header) ⇒ Object



71
72
73
74
75
76
# File 'lib/csvlint/csvw/column.rb', line 71

def validate_header(header)
  reset
  valid_headers = @titles ? @titles.map{ |l,v| v if Column.languages_match(l, lang) }.flatten : []
  build_errors(:invalid_header, :schema, 1, @number, header, @titles) unless valid_headers.include? header
  return valid?
end