Skip to content

Commit

Permalink
Add linting with rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiana-b committed Jun 29, 2017
1 parent e79283a commit d4da6eb
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 28 deletions.
20 changes: 20 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,20 @@
AllCops:
DisabledByDefault: true
Exclude:
- 'lib/tableschema/exceptions.rb'

Security:
Enabled: true

Lint:
Enabled: true

Style/HashSyntax:
Enabled: true
EnforcedStyle: ruby19_no_mixed_keys

Style/MutableConstant:
Enabled: true

Metrics/CyclomaticComplexity:
Severity: error
16 changes: 15 additions & 1 deletion .travis.yml
@@ -1,9 +1,23 @@
---
language: ruby

rvm:
- 2.3.1
- 2.4.1
before_install: gem install bundler -v 1.11.2

before_install:
gem install bundler -v 1.11.2

install:
- bundle
- gem install rubocop

script:
- rake spec

after_success:
- rubocop

deploy:
provider: rubygems
api_key:
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -4,7 +4,7 @@ require "open-uri"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec

task :update_profiles do
open('https://specs.frictionlessdata.io/schemas/table-schema.json') do |remote_schema|
Expand Down
38 changes: 27 additions & 11 deletions lib/tableschema/constraints/constraints.rb
Expand Up @@ -49,7 +49,7 @@ def underscore(value)

def ordered_constraints
constraints = @field.fetch(:constraints, {})
ordered_constraints = constraints.select{ |k,v| k == :required}
ordered_constraints = constraints.select{ |key| key == :required}
ordered_constraints.merge!(constraints)
end

Expand All @@ -59,20 +59,36 @@ def is_supported_type?(constraint)
end

def parse_constraint(constraint)
if @value.is_a?(::Integer) && constraint.is_a?(::String)
if constraint.is_a?(Array)
parse_array(constraint)
else
parse_string(constraint)
end
end

def parse_array(constraint)
case @value
when ::Float
constraint.map { |c| Float(c) }
when ::Boolean
constraint.map { |c| convert_to_boolean(c) }
when ::Date
constraint.map { |c| Date.parse(c) }
else
constraint
end
end

def parse_string(constraint)
case @value
when ::Integer
constraint.to_i
elsif @value.is_a?(::Tod::TimeOfDay)
when ::Tod::TimeOfDay
Tod::TimeOfDay.parse(constraint)
elsif @value.is_a?(::DateTime)
when ::DateTime
DateTime.parse(constraint)
elsif @value.is_a?(::Date) && constraint.is_a?(::String)
when ::Date
Date.parse(constraint)
elsif @value.is_a?(::Float) && constraint.is_a?(Array)
constraint.map { |c| Float(c) }
elsif @value.is_a?(Boolean) && constraint.is_a?(Array)
constraint.map { |c| convert_to_boolean(c) }
elsif @value.is_a?(Date) && constraint.is_a?(Array)
constraint.map { |c| Date.parse(c) }
else
constraint
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tableschema/constraints/pattern.rb
Expand Up @@ -3,7 +3,7 @@ class Constraints
module Pattern

def check_pattern
if !@value.to_json.match /#{@constraints[:pattern]}/
if !@value.to_json.match(/#{@constraints[:pattern]}/)
raise TableSchema::ConstraintError.new("The value for the field `#{@field[:name]}` must match the pattern")
end
true
Expand Down
8 changes: 4 additions & 4 deletions lib/tableschema/data.rb
Expand Up @@ -6,11 +6,10 @@ module Data
def cast_rows(rows, fail_fast = true, limit = nil)
@errors ||= []
parsed_rows = []
rows.each_with_index do |r, i|
rows.each_with_index do |row, i|
begin
break if limit && (limit <= i)
r = r.fields if r.class == CSV::Row
parsed_rows << cast_row(r, fail_fast)
parsed_rows << cast_row(row, fail_fast)
rescue MultipleInvalid, ConversionError => e
raise e if fail_fast == true
@errors << e if e.is_a?(ConversionError)
Expand All @@ -24,6 +23,7 @@ def cast_rows(rows, fail_fast = true, limit = nil)

def cast_row(row, fail_fast = true)
@errors ||= []
row = row.fields if row.class == CSV::Row
raise_header_error(row) if row.count != fields.count
fields.each_with_index do |field,i|
row[i] = cast_column(field, row[i], fail_fast)
Expand All @@ -46,7 +46,7 @@ def check_for_errors

def cast_column(field, col, fail_fast)
field.cast_value(col)
rescue Exception => e
rescue TableSchema::Exception => e
if fail_fast == true
raise e
else
Expand Down
2 changes: 1 addition & 1 deletion lib/tableschema/defaults.rb
Expand Up @@ -3,5 +3,5 @@ module TableSchema
format: 'default',
type: 'string',
missing_values: ['']
}
}.freeze
end
13 changes: 7 additions & 6 deletions lib/tableschema/infer.rb
Expand Up @@ -33,16 +33,16 @@ def fields
constraints = {}
constraints[:required] = @explicit === true
constraints[:unique] = (header == @primary_key)
constraints.delete_if { |k,v| v == false } unless @explicit === true
constraints.delete_if { |_,v| v == false } unless @explicit === true
descriptor[:constraints] = constraints if constraints.count > 0
TableSchema::Field.new(descriptor)
end
end

def infer!
type_matches = []
@rows.each_with_index do |row, i|
break if @row_limit && i > @row_limit
@rows.each_with_index do |row, index|
break if @row_limit && index > @row_limit
row = row.fields if row.class == CSV::Row

row_length = row.count
Expand All @@ -56,9 +56,9 @@ def infer!
row = row.push(fill).flatten
end

row.each_with_index do |col, i|
type_matches[i] ||= []
type_matches[i] << guess_type(col, i)
row.each_with_index do |col, idx|
type_matches[idx] ||= []
type_matches[idx] << guess_type(col, idx)
end

end
Expand Down Expand Up @@ -97,6 +97,7 @@ def guess_format(converter, col)
guessed_format = format
break
rescue TableSchema::Exception
next
end
end
guessed_format
Expand Down
2 changes: 1 addition & 1 deletion lib/tableschema/types/number.rb
Expand Up @@ -26,7 +26,7 @@ def currency_symbols

def cast_default(value)
return value if value.class == type
return Float(value) if value.class == ::Fixnum
return Float(value) if value.class == ::Integer

value = preprocess_value(value)
return Float(value)
Expand Down
2 changes: 1 addition & 1 deletion lib/tableschema/version.rb
@@ -1,3 +1,3 @@
module TableSchema
VERSION = "0.3.1"
VERSION = "0.3.1".freeze
end
2 changes: 1 addition & 1 deletion spec/types_spec.rb
Expand Up @@ -774,7 +774,7 @@
}
}

let (:missing_values) {
let(:missing_values) {
[
'null',
'NaN'
Expand Down
1 change: 1 addition & 0 deletions tableschema.gemspec
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "pry", "~> 0.10.0"
spec.add_development_dependency "webmock", "~> 2.3.0"
spec.add_development_dependency "coveralls", "~> 0.8.13"
spec.add_development_dependency "rubocop", "~> 0.49.1"

spec.add_dependency "json-schema", "~> 2.6.0"
spec.add_dependency "uuid", "~> 2.3.8"
Expand Down

0 comments on commit d4da6eb

Please sign in to comment.