Class: TableSchema::Schema
- Inherits:
-
Hash
- Object
- Hash
- TableSchema::Schema
- Defined in:
- lib/tableschema/schema.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Instance Method Summary collapse
- #cast_row(row, fail_fast: true) ⇒ Object
- #descriptor ⇒ Object
-
#initialize(descriptor, case_insensitive_headers: false, strict: false) ⇒ Schema
constructor
A new instance of Schema.
- #parse_schema(descriptor) ⇒ Object
- #save(target) ⇒ Object
Methods included from Helpers
#convert_to_boolean, #deep_symbolize_keys, #false_values, #get_class_for_type, #true_values, #type_class_lookup
Methods included from Model
#add_field, #fields, #foreign_keys, #get_constraints, #get_field, #get_fields_by_type, #get_type, #has_field?, #headers, #missing_values, #primary_keys, #remove_field, #required_headers, #unique_headers
Methods included from Validate
#load_validator!, #validate, #validate!
Constructor Details
#initialize(descriptor, case_insensitive_headers: false, strict: false) ⇒ Schema
Returns a new instance of Schema.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/tableschema/schema.rb', line 9 def initialize(descriptor, case_insensitive_headers: false, strict: false) self.merge! deep_symbolize_keys(parse_schema(descriptor)) @case_insensitive_headers = case_insensitive_headers @strict = strict load_fields! load_validator! @strict == true ? validate! : validate self end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/tableschema/schema.rb', line 7 def errors @errors end |
Instance Method Details
#cast_row(row, fail_fast: true) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/tableschema/schema.rb', line 42 def cast_row(row, fail_fast: true) errors = Set.new handle_error = lambda { |e| fail_fast == true ? raise(e) : errors << e } row = row.fields if row.class == CSV::Row if row.count != self.fields.count handle_error.call(TableSchema::ConversionError.new("The number of items to convert (#{row.count}) does not match the number of headers in the schema (#{self.fields.count})")) end self.fields.each_with_index do |field, i| begin row[i] = field.cast_value(row[i]) rescue TableSchema::Exception => e handle_error.call(e) end end unless errors.empty? raise(TableSchema::MultipleInvalid.new("There were errors parsing the data", errors)) end row end |
#descriptor ⇒ Object
20 21 22 |
# File 'lib/tableschema/schema.rb', line 20 def descriptor self.to_h end |
#parse_schema(descriptor) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/tableschema/schema.rb', line 24 def parse_schema(descriptor) if descriptor.class == Hash descriptor elsif descriptor.class == String begin JSON.parse(open(descriptor).read, symbolize_names: true) rescue Errno::ENOENT raise SchemaException.new("File not found at `#{descriptor}`") rescue OpenURI::HTTPError => e raise SchemaException.new("URL `#{descriptor}` returned #{e.message}") rescue JSON::ParserError raise SchemaException.new("File at `#{descriptor}` is not valid JSON") end else raise SchemaException.new("A schema must be a hash, path or URL") end end |
#save(target) ⇒ Object
64 65 66 67 |
# File 'lib/tableschema/schema.rb', line 64 def save(target) File.open(target, "w") { |file| file << JSON.pretty_generate(self.descriptor) } true end |