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, default_name: nil, 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
31
# 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, default_name: nil, 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
  @default_name = default_name
  @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

#default_nameObject (readonly)

Returns the value of attribute default_name.



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

def default_name
  @default_name
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



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

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



134
135
136
137
138
139
# File 'lib/csvlint/csvw/column.rb', line 134

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



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
70
71
72
73
74
75
76
# File 'lib/csvlint/csvw/column.rb', line 33

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"] || [""],
    default: inherited_properties["default"] || "",
    about_url: inherited_properties["aboutUrl"],
    property_url: inherited_properties["propertyUrl"],
    value_url: inherited_properties["valueUrl"],
    required: inherited_properties["required"] || false,
    separator: inherited_properties["separator"],
    ordered: inherited_properties["ordered"] || false,
    default_name: column_properties["titles"] && column_properties["titles"][lang] ? column_properties["titles"][lang][0] : nil,
    titles: column_properties["titles"],
    suppress_output: column_properties["suppressOutput"] ? column_properties["suppressOutput"] : false,
    virtual: column_properties["virtual"] || false,
    annotations: annotations,
    warnings: warnings
  )
end

.languages_match(l1, l2) ⇒ Object



141
142
143
144
145
# File 'lib/csvlint/csvw/column.rb', line 141

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

#validate(string_value, row = nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/csvlint/csvw/column.rb', line 93

def validate(string_value, row=nil)
  reset
  string_value = string_value || @default
  if null.include? string_value
    validate_required(nil, row)
    values = nil
    return values
  else
    string_values = @separator.nil? ? [string_value] : string_value.split(@separator)
    values = []
    string_values.each do |s|
      invalid = false
      value, warning = DATATYPE_PARSER[@datatype["base"] || @datatype["@id"]].call(s, @datatype["format"])
      if warning.nil?
        validate_required(value, row)
        invalid = !validate_format(value, row) || invalid
        invalid = !validate_length(value, row) || invalid
        invalid = !validate_value(value, row) || invalid
        values << (invalid ? { :invalid => s } : value)
      else
        build_errors(warning, :schema, row, @number, s, @datatype)
        values << { :invalid => s }
      end
    end
    values = (values && @separator.nil?) ? values[0] : values
    return values
  end
end

#validate_header(header, strict) ⇒ Object



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

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