Class: Csvlint::Csvw::Column
- Inherits:
-
Object
- Object
- Csvlint::Csvw::Column
- Includes:
- ErrorCollector
- Defined in:
- lib/csvlint/csvw/column.rb
Instance Attribute Summary collapse
-
#about_url ⇒ Object
readonly
Returns the value of attribute about_url.
-
#annotations ⇒ Object
readonly
Returns the value of attribute annotations.
-
#datatype ⇒ Object
readonly
Returns the value of attribute datatype.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#default_name ⇒ Object
readonly
Returns the value of attribute default_name.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#lang ⇒ Object
readonly
Returns the value of attribute lang.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#null ⇒ Object
readonly
Returns the value of attribute null.
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#ordered ⇒ Object
readonly
Returns the value of attribute ordered.
-
#property_url ⇒ Object
readonly
Returns the value of attribute property_url.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#separator ⇒ Object
readonly
Returns the value of attribute separator.
-
#source_number ⇒ Object
readonly
Returns the value of attribute source_number.
-
#suppress_output ⇒ Object
readonly
Returns the value of attribute suppress_output.
-
#text_direction ⇒ Object
readonly
Returns the value of attribute text_direction.
-
#titles ⇒ Object
readonly
Returns the value of attribute titles.
-
#value_url ⇒ Object
readonly
Returns the value of attribute value_url.
-
#virtual ⇒ Object
readonly
Returns the value of attribute virtual.
Attributes included from ErrorCollector
#errors, #info_messages, #warnings
Class Method Summary collapse
- .create_date_parser(type, warning) ⇒ Object
- .create_regexp_based_parser(regexp, warning) ⇒ Object
- .from_json(number, column_desc, base_url = nil, lang = "und", inherited_properties = {}) ⇒ Object
- .languages_match(l1, l2) ⇒ Object
Instance Method Summary collapse
-
#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
constructor
A new instance of Column.
- #validate(string_value, row = nil) ⇒ Object
- #validate_header(header, strict) ⇒ Object
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_url ⇒ Object (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 |
#annotations ⇒ Object (readonly)
Returns the value of attribute annotations.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def annotations @annotations end |
#datatype ⇒ Object (readonly)
Returns the value of attribute datatype.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def datatype @datatype end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def default @default end |
#default_name ⇒ Object (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 |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def id @id end |
#lang ⇒ Object (readonly)
Returns the value of attribute lang.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def lang @lang end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def name @name end |
#null ⇒ Object (readonly)
Returns the value of attribute null.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def null @null end |
#number ⇒ Object (readonly)
Returns the value of attribute number.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def number @number end |
#ordered ⇒ Object (readonly)
Returns the value of attribute ordered.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def ordered @ordered end |
#property_url ⇒ Object (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 |
#required ⇒ Object (readonly)
Returns the value of attribute required.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def required @required end |
#separator ⇒ Object (readonly)
Returns the value of attribute separator.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def separator @separator end |
#source_number ⇒ Object (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_output ⇒ Object (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_direction ⇒ Object (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 |
#titles ⇒ Object (readonly)
Returns the value of attribute titles.
6 7 8 |
# File 'lib/csvlint/csvw/column.rb', line 6 def titles @titles end |
#value_url ⇒ Object (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 |
#virtual ⇒ Object (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
124 125 126 127 128 129 130 131 |
# File 'lib/csvlint/csvw/column.rb', line 124 def create_date_parser(type, warning) lambda { |value, format| format = Csvlint::Csvw::DateFormat.new(nil, type) if format.nil? v = format.parse(value) return nil, warning if v.nil? [v, nil] } end |
.create_regexp_based_parser(regexp, warning) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/csvlint/csvw/column.rb', line 133 def create_regexp_based_parser(regexp, warning) lambda { |value, format| return nil, warning unless value&.match?(regexp) [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 |
# 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 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"] || false, virtual: column_properties["virtual"] || false, annotations: annotations, warnings: warnings) end |
.languages_match(l1, l2) ⇒ Object
140 141 142 143 144 |
# File 'lib/csvlint/csvw/column.rb', line 140 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}-") false end |
Instance Method Details
#validate(string_value, row = nil) ⇒ Object
92 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 |
# File 'lib/csvlint/csvw/column.rb', line 92 def validate(string_value, row = nil) reset string_value ||= @default if null.include? string_value validate_required(nil, row) nil 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 && @separator.nil? ? values[0] : values end end |
#validate_header(header, strict) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/csvlint/csvw/column.rb', line 77 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 valid? end |