From fd6c9ed6769641cd214781c74551e944919171f8 Mon Sep 17 00:00:00 2001 From: Joel Low Date: Wed, 13 Jan 2016 16:38:50 +0800 Subject: [PATCH] Import the language definitions from the Web application. --- .gitignore | 1 + coursemology-polyglot.gemspec | 2 + lib/coursemology/polyglot.rb | 14 ++++-- lib/coursemology/polyglot/language.rb | 45 +++++++++++++++++++ lib/coursemology/polyglot/language/python.rb | 17 +++++++ .../polyglot/language/python_spec.rb | 15 +++++++ spec/coursemology/polyglot/language_spec.rb | 21 +++++++++ spec/coursemology/polyglot_spec.rb | 4 -- spec/spec_helper.rb | 4 ++ 9 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 lib/coursemology/polyglot/language.rb create mode 100644 lib/coursemology/polyglot/language/python.rb create mode 100644 spec/coursemology/polyglot/language/python_spec.rb create mode 100644 spec/coursemology/polyglot/language_spec.rb diff --git a/.gitignore b/.gitignore index db4592f..022fdb2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ # Ignore bundler config and gems. /.bundle /vendor/bundle +/Gemfile.lock # Ignore generated code coverage information /spec/coverage/* diff --git a/coursemology-polyglot.gemspec b/coursemology-polyglot.gemspec index 3d05c24..cac4cca 100644 --- a/coursemology-polyglot.gemspec +++ b/coursemology-polyglot.gemspec @@ -27,4 +27,6 @@ programming languages supported in Coursemology. spec.add_development_dependency 'simplecov' spec.add_development_dependency 'coveralls' spec.add_development_dependency 'codeclimate-test-reporter' + + spec.add_dependency 'activesupport', '~> 4.2.0', '>= 4.2.2' end diff --git a/lib/coursemology/polyglot.rb b/lib/coursemology/polyglot.rb index b40ae87..40e7b5b 100644 --- a/lib/coursemology/polyglot.rb +++ b/lib/coursemology/polyglot.rb @@ -1,7 +1,15 @@ +require 'active_support/all' require 'coursemology/polyglot/version' -module Coursemology - module Polyglot - # Your code goes here... +module Coursemology::Polyglot + extend ActiveSupport::Autoload + + eager_autoload do + autoload :Language + end + + def self.eager_load! + super + Coursemology::Polyglot::Language.eager_load! end end diff --git a/lib/coursemology/polyglot/language.rb b/lib/coursemology/polyglot/language.rb new file mode 100644 index 0000000..7063d13 --- /dev/null +++ b/lib/coursemology/polyglot/language.rb @@ -0,0 +1,45 @@ +if defined?(ActiveRecord) + # :nocov: + # TODO: This is for compatibility with the Web application. A future refactoring might be able + # to remove this dependency. + class Coursemology::Polyglot::Language < ActiveRecord::Base; end + # :nocov: +else + class Coursemology::Polyglot::Language; end +end + +class Coursemology::Polyglot::Language + extend ActiveSupport::Autoload + + eager_autoload do + autoload :Python + end + + # Gets the display name of the language. + # + # @abstract + # @return [String] + def self.display_name + fail NotImplementedError + end + + # The stylesheets that need to be packaged with the rest of the application. + # + # This should include the Rouge/Pygments stylesheet for formatting code. + # + # @abstract + # @return [Array] + def self.stylesheets + fail NotImplementedError + end + + # The script files that need to be packaged with the rest of the application. + # + # This should include the Ace mode for the language. + # + # @abstract + # @return [Array] + def self.javascript + fail NotImplementedError + end +end diff --git a/lib/coursemology/polyglot/language/python.rb b/lib/coursemology/polyglot/language/python.rb new file mode 100644 index 0000000..6b0750f --- /dev/null +++ b/lib/coursemology/polyglot/language/python.rb @@ -0,0 +1,17 @@ +class Coursemology::Polyglot::Language::Python < Coursemology::Polyglot::Language + class Python2Point7 < Coursemology::Polyglot::Language::Python + class << self + def display_name + 'Python 2.7' + end + end + end + + class Python3Point4 < Coursemology::Polyglot::Language::Python + class << self + def display_name + 'Python 3.4' + end + end + end +end diff --git a/spec/coursemology/polyglot/language/python_spec.rb b/spec/coursemology/polyglot/language/python_spec.rb new file mode 100644 index 0000000..f449e78 --- /dev/null +++ b/spec/coursemology/polyglot/language/python_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +RSpec.describe Coursemology::Polyglot::Language::Python do + describe Coursemology::Polyglot::Language::Python::Python2Point7 do + it 'returns the correct display name' do + expect(subject.class.display_name).to eq('Python 2.7') + end + end + + describe Coursemology::Polyglot::Language::Python::Python3Point4 do + it 'returns the correct display name' do + expect(subject.class.display_name).to eq('Python 3.4') + end + end +end diff --git a/spec/coursemology/polyglot/language_spec.rb b/spec/coursemology/polyglot/language_spec.rb new file mode 100644 index 0000000..58c947a --- /dev/null +++ b/spec/coursemology/polyglot/language_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +RSpec.describe Coursemology::Polyglot::Language do + describe '.display_name' do + it 'fails with NotImplementedError' do + expect { subject.class.display_name }.to raise_error(NotImplementedError) + end + end + + describe '.stylesheets' do + it 'fails with NotImplementedError' do + expect { subject.class.stylesheets }.to raise_error(NotImplementedError) + end + end + + describe '.javascript' do + it 'fails with NotImplementedError' do + expect { subject.class.javascript }.to raise_error(NotImplementedError) + end + end +end diff --git a/spec/coursemology/polyglot_spec.rb b/spec/coursemology/polyglot_spec.rb index 67b2ef3..aa29be5 100644 --- a/spec/coursemology/polyglot_spec.rb +++ b/spec/coursemology/polyglot_spec.rb @@ -4,8 +4,4 @@ it 'has a version number' do expect(Coursemology::Polyglot::VERSION).not_to be nil end - - it 'does something useful' do - expect(false).to eq(true) - end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0dc30d8..893d1d2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -93,4 +93,8 @@ # test failures related to randomization by passing the same `--seed` value # as the one that triggered the failure. Kernel.srand config.seed + + config.before(:suite) do + Coursemology::Polyglot.eager_load! + end end