Skip to content

Commit

Permalink
Add reek commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbeales committed Apr 4, 2018
1 parent 5bf09fe commit 2e4d838
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .fasterer.yml
@@ -0,0 +1,5 @@
exclude_paths:
- vendor/**/*.rb
- script/**/*.rb


7 changes: 6 additions & 1 deletion .overcommit.yml
Expand Up @@ -19,7 +19,12 @@ PreCommit:
RuboCop:
enabled: true
on_warn: fail # Treat all warnings as failures
#
Fasterer:
enabled: true
BundleAudit:
enabled: true
Reek:
enabled: true
# TrailingWhitespace:
# enabled: true
# exclude:
Expand Down
1 change: 1 addition & 0 deletions .rubocop.yml
Expand Up @@ -54,6 +54,7 @@ Layout/CaseIndentation:
Enabled: true

Metrics/BlockLength:
Max: 30
Exclude:
- 'Rakefile'
- '**/*.rake'
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Expand Up @@ -8,7 +8,6 @@ gem 'audited', '>= 4.7.0', require: false # adds table auiting support
gem 'bugsnag', '>= 6.6', require: false # online bug reporting
gem 'bundler-audit', '>= 0.6.0', require: false # check gems for security issues
gem 'dotenv', '>= 2.2.1', require: false # adds environment variables from .env files
gem 'fast_blank', '>= 1.0.0', platform: :ruby, require: false # faster implementation of blank?
gem 'fast_stack', '>= 0.2.0', platform: :ruby, require: false # stack profiler
gem 'flamegraph', '>= 0.9.5', require: false # rack profiling
gem 'i18n', '>= 1.0.0', require: false # internationalization support
Expand Down Expand Up @@ -58,9 +57,11 @@ group :development, :test do
gem 'annotate', '>= 2.7.2', require: false # annotate models
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', '>= 10.0.2', platforms: %i[mri mingw x64_mingw]
gem 'fasterer', require: false # find slow code
gem 'guard', '>= 2.14.2', require: false # watch for changed files
gem 'guard-rspec', '>= 4.7.3', require: false # run rspec on code change
gem 'overcommit', require: false # git hooks
gem 'reek', require: false # find code smells
gem 'ruby-prof', platform: :ruby, require: false
gem 'timecop', '>= 0.9.1', require: false # testing time dependent
end
Expand Down
32 changes: 30 additions & 2 deletions Gemfile.lock
Expand Up @@ -22,6 +22,10 @@ GEM
ast (2.4.0)
audited (4.7.0)
activerecord (>= 4.0, < 5.2)
axiom-types (0.1.1)
descendants_tracker (~> 0.0.4)
ice_nine (~> 0.11.0)
thread_safe (~> 0.3, >= 0.3.1)
backports (3.11.1)
bugsnag (6.6.4)
concurrent-ruby (~> 1.0)
Expand All @@ -31,7 +35,12 @@ GEM
byebug (10.0.2)
childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11)
codeclimate-engine-rb (0.4.1)
virtus (~> 1.0)
coderay (1.1.2)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
colorize (0.8.1)
concurrent-ruby (1.0.5)
coveralls (0.7.1)
multi_json (~> 1.3)
Expand All @@ -40,11 +49,14 @@ GEM
term-ansicolor
thor
database_cleaner (1.6.1)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.3)
docile (1.3.0)
domain_name (0.5.20170404)
unf (>= 0.0.5, < 1.0.0)
dotenv (2.2.1)
equalizer (0.0.11)
ethon (0.11.0)
ffi (>= 1.3.0)
factory_bot (4.8.2)
Expand All @@ -55,8 +67,10 @@ GEM
multipart-post (>= 1.2, < 3)
faraday_middleware (0.12.2)
faraday (>= 0.7.4, < 1.0)
fast_blank (1.0.0)
fast_stack (0.2.0)
fasterer (0.4.0)
colorize (~> 0.7)
ruby_parser (~> 3.9)
ffi (1.9.23)
ffi (1.9.23-x64-mingw32)
flamegraph (0.9.5)
Expand Down Expand Up @@ -92,6 +106,7 @@ GEM
domain_name (~> 0.5)
i18n (1.0.0)
concurrent-ruby (~> 1.0)
ice_nine (0.11.2)
iniparse (1.4.4)
json (2.1.0)
jsonb_accessor (1.0.0)
Expand Down Expand Up @@ -167,6 +182,10 @@ GEM
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
rdoc (6.0.3)
reek (4.8.0)
codeclimate-engine-rb (~> 0.4.0)
parser (>= 2.5.0.0, < 2.6)
rainbow (~> 3.0)
rest-client (2.0.2)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
Expand Down Expand Up @@ -203,12 +222,15 @@ GEM
ruby_audit (1.2.0)
bundler-audit (~> 0.6.0)
ruby_dep (1.5.0)
ruby_parser (3.11.0)
sexp_processor (~> 4.9)
scrutinizer-ocular (1.0.1)
simplecov (>= 0.7)
sdoc (1.0.0)
rdoc (>= 5.0)
secure_headers (5.0.5)
useragent (>= 0.15.0)
sexp_processor (4.10.1)
shellany (0.0.1)
simplecov (0.16.1)
docile (~> 1.1)
Expand Down Expand Up @@ -271,6 +293,11 @@ GEM
valid_email2 (2.2.2)
activemodel (>= 3.2)
mail (~> 2.5)
virtus (1.0.5)
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
equalizer (~> 0.0, >= 0.0.9)
wdm (0.1.1)
websocket (1.2.5)
win32console (1.3.2)
Expand All @@ -291,8 +318,8 @@ DEPENDENCIES
dotenv (>= 2.2.1)
factory_bot (>= 4.0)
faker (>= 1.8.7)
fast_blank (>= 1.0.0)
fast_stack (>= 0.2.0)
fasterer
flamegraph (>= 0.9.5)
fuubar (>= 2.3.1)
guard (>= 2.14.2)
Expand All @@ -315,6 +342,7 @@ DEPENDENCIES
rack-timeout (>= 0.4.2)
rake (>= 12.3.1)
ralyxa (>= 1.7.0)
reek
rest-client (>= 2.0.2)
route_downcaser (>= 1.2.1)
rspec (>= 3.7.0)
Expand Down
2 changes: 1 addition & 1 deletion Guardfile
Expand Up @@ -20,7 +20,7 @@ guard :rspec, cmd: 'bundle exec rspec' do
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

%w[models config db services intents bin public].select do |d|
%w[app/models config db app/services intents bin public].select do |d|
Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/models/choice.rb
Expand Up @@ -38,6 +38,11 @@ class Choice < ApplicationRecord
validates_uniqueness_of :value, scope: :response_scale
validates_numericality_of :score, message: 'is not a number'

def self.load_choice(response_scale, choice)
Choice.create_with(description: choice['text']) \
.find_or_create_by(response_scale: response_scale, value: choice['value'])
end

def to_s
"#{response_scale} #{value} #{description}"
end
Expand Down
10 changes: 4 additions & 6 deletions app/services/instrument_loader.rb
Expand Up @@ -6,9 +6,7 @@
class InstrumentLoader
include Singleton
attr_reader :instrument
attr_reader :response_scale



# load method saves an instrument to DB from either CSV or json
def load(instrument:, type: :json)
raise AppConstants::LOADER_NIL_INSTRUMENT unless instrument
Expand All @@ -22,14 +20,14 @@ def load(instrument:, type: :json)
protected

def check_valid_instrument_name
raise 'Instrument name is missing' if @instrument.name.blank?
raise 'Instrument name is missing' if instrument.name.blank?
end

# delegates loading to either Json Loader or CSV Loader
def load!(type: :json)
loader = get_loader_instance(type: type)
loader.load!(instrument: @instrument)
@instrument.save!
loader.load_instrument(instrument: instrument)
instrument.save!
end

private
Expand Down
15 changes: 9 additions & 6 deletions app/services/instrument_loader_csv.rb
Expand Up @@ -4,26 +4,29 @@

# Loads instruments from csv
class InstrumentLoaderCsv < InstrumentLoader

attr_reader :instrument
attr_reader :response_scale

# Returns an array of Items that represent the questions in an instrument.
def load!(instrument:)
def load_instrument(instrument:)
@instrument = instrument
load_content
load_attributes
load_response_scale
load_items
instrument
end

private

def load_content
@csv_source = CsvSource.new("#{AppConstants::INSTRUMENTS_FOLDER}#{@instrument.name}.csv", {})
@instrument.csv_content = @csv_source.to_s
csv_source = CsvSource.new("#{AppConstants::INSTRUMENTS_FOLDER}#{instrument.name}.csv", {})
instrument.csv_content = csv_source.to_s
end

def load_attributes
@instrument.instrument_type = :csv
@instrument.instructions = ''
instrument.instrument_type = :csv
instrument.instructions = ''
end

def load_response_scale(name:)
Expand Down
40 changes: 19 additions & 21 deletions app/services/instrument_loader_json.rb
Expand Up @@ -2,34 +2,36 @@

# Loads instruments from json
class InstrumentLoaderJson < InstrumentLoader
attr_reader :instrument
attr_reader :response_scale

# Loads attributes, an array of Items, and rating scale that represent the questions in an instrument.
def load!(instrument:)
def load_instrument(instrument:)
@instrument = instrument
load_content
load_attributes
load_elements
@instrument
instrument
end

private

def load_content
check_valid_instrument_name

filename = "#{AppConstants::INSTRUMENTS_FOLDER}#{@instrument.name}.json"
filename = "#{AppConstants::INSTRUMENTS_FOLDER}#{instrument.name}.json"
json_data = File.read(filename)
@instrument.json_content = MultiJson.load(json_data)
instrument.json_content = MultiJson.load(json_data)
end

def load_attributes
@instrument.instrument_type = :json
@instrument.instructions = @instrument.pages[0]['title']
instrument.instrument_type = :json
instrument.instructions = instrument.pages[0]['title']
end

def load_elements
elements = []
@instrument.pages.each { |p| elements += p['elements'] }
instrument.pages.each { |page| elements += page['elements'] }
load_response_scale(elements: elements, name: instrument.name)
load_items(elements)
end
Expand All @@ -38,25 +40,21 @@ def load_response_scale(elements:, name:)
@response_scale = ResponseScale.find_or_create_by!(name: name)
elements.each do |el|
el['choices'].map do |ch|
choice = load_choice(ch)
@response_scale.choices.concat(choice)
choice = Choice.load_choice(response_scale, ch)
response_scale.choices.concat(choice)
end
end
@response_scale
end

def load_choice(choice)
Choice.create_with(description: choice['text']) \
.find_or_create_by(response_scale: @response_scale, value: choice['value'])
response_scale
end

def load_items(elements)
elements.map do |e|
item = Item.create!(instrument: @instrument, name: e['name'], \
item_type: e['type'], is_required: e['isRequired'], \
response_scale: @response_scale, title: e['title'])
@instrument.items.concat(item)
items = instrument.items
elements.map do |element|
item = Item.create!(instrument: instrument, name: element['name'], \
item_type: element['type'], is_required: element['isRequired'], \
response_scale: response_scale, title: element['title'])
items.concat(item)
end
@instrument.items
items
end
end

0 comments on commit 2e4d838

Please sign in to comment.