From bbedca7000fa9510e7742e766fcc4d9c8d170f82 Mon Sep 17 00:00:00 2001 From: Joel AZEMAR Date: Mon, 7 Apr 2014 12:43:11 +0200 Subject: [PATCH] First commit --- .travis.yml | 4 ++ Gemfile | 4 ++ Gemfile.lock | 27 ++++++++++ LICENSE.txt | 22 ++++++++ README.md | 37 ++++++++++++++ Rakefile | 15 ++++++ lib/common.rb | 7 +++ lib/locomotive/common/exception.rb | 61 +++++++++++++++++++++++ lib/locomotive/common/plugins/notifier.rb | 19 +++++++ lib/locomotive/common/version.rb | 5 ++ locomotivecms_common.gemspec | 24 +++++++++ spec/locomotive/exception_spec.rb | 11 ++++ spec/spec_helper.rb | 9 ++++ 13 files changed, 245 insertions(+) create mode 100644 .travis.yml create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 lib/common.rb create mode 100644 lib/locomotive/common/exception.rb create mode 100644 lib/locomotive/common/plugins/notifier.rb create mode 100644 lib/locomotive/common/version.rb create mode 100644 locomotivecms_common.gemspec create mode 100644 spec/locomotive/exception_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d32b8d1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,4 @@ +language: ruby +rvm: + - 2.0.0 + - 2.1.1 \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a97942b --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in locomotivecms_common.gemspec +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..b38a76d --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,27 @@ +PATH + remote: . + specs: + locomotivecms_common (0.0.1) + +GEM + remote: https://rubygems.org/ + specs: + diff-lcs (1.2.5) + rake (10.2.2) + rspec (2.14.1) + rspec-core (~> 2.14.0) + rspec-expectations (~> 2.14.0) + rspec-mocks (~> 2.14.0) + rspec-core (2.14.8) + rspec-expectations (2.14.5) + diff-lcs (>= 1.1.3, < 2.0) + rspec-mocks (2.14.6) + +PLATFORMS + ruby + +DEPENDENCIES + bundler (~> 1.5) + locomotivecms_common! + rake (~> 10.1) + rspec (~> 2.14) diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..1fbfd58 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,22 @@ +Copyright (c) 2014 Joel AZEMAR + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b9bb0ae --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# LocomotivecmsCommon + +[![Gem Version](https://badge.fury.io/rb/common.png)](http://badge.fury.io/rb/locomotivecms_common) + +[![Code Climate](https://codeclimate.com/github/locomotivecms/common.png)](https://codeclimate.com/github/locomotivecms/common) + +[![Dependency Status](https://gemnasium.com/locomotivecms/common.png)](https://gemnasium.com/locomotivecms/common) + +[![Build Status](https://travis-ci.org/locomotivecms/common.png?branch=master)](https://travis-ci.org/locomotivecms/common) (Travis CI) + +[![Coverage Status](https://coveralls.io/repos/locomotivecms/common/badge.png)](https://coveralls.io/r/locomotivecms/common) + +## Installation + +Add this line to your application's Gemfile: + + gem 'locomotivecms_common' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install locomotivecms_common + +## Usage + +TODO: Write usage instructions here + +## Contributing + +1. Fork it ( http://github.com//locomotivecms_common/fork ) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..8dc82cf --- /dev/null +++ b/Rakefile @@ -0,0 +1,15 @@ +require 'rubygems' +require 'bundler/setup' +require 'bundler/gem_tasks' +require 'rake' +require 'rspec' +require 'rspec/core/rake_task' + +require_relative 'lib/common' + +RSpec::Core::RakeTask.new('spec:all') do |spec| + spec.pattern = 'spec/**/*_spec.rb' +end + +task spec: ['spec:all'] +task default: :spec \ No newline at end of file diff --git a/lib/common.rb b/lib/common.rb new file mode 100644 index 0000000..eac400c --- /dev/null +++ b/lib/common.rb @@ -0,0 +1,7 @@ +require_relative 'locomotive/common/version' +require_relative 'locomotive/common/exception' + +# module Locomotive +# module Common +# end +# end \ No newline at end of file diff --git a/lib/locomotive/common/exception.rb b/lib/locomotive/common/exception.rb new file mode 100644 index 0000000..b7ff93e --- /dev/null +++ b/lib/locomotive/common/exception.rb @@ -0,0 +1,61 @@ +module Locomotive + module Common + + class Notifier + def method_missing method, *args + nil + end + end + + class DefaultException < ::Exception + attr_accessor :notifier + + def initialize(message = nil, parent_exception = nil) + self.notifier = Locomotive::Common::Notifier.new + self.log_backtrace(parent_exception) if parent_exception + super(message) + init_plugins + end + + def init_plugins + @plugins = [] + Locomotive::Common::Plugins.constants.each do |name| + @plugins << ::Plugins.const_get(name).new(self) + end + end + + protected + + def log_backtrace(parent_exception) + full_error_message = "#{parent_exception.message}\n\t" + full_error_message += parent_exception.backtrace.join("\n\t") + full_error_message += "\n\n" + + notifier.fatal full_error_message + end + end + + class RendererException < DefaultException + attr_accessor :name, :template, :liquid_context + + def initialize(exception, name, template, liquid_context) + self.name, self.template, self.liquid_context = name, template, liquid_context + self.log_page_into_backtrace(exception) + super(exception.message) + self.set_backtrace(exception.backtrace) + end + + def log_page_into_backtrace(exception) + line = self.template.line_offset + line += (exception.respond_to?(:line) ? exception.line || 0 : 0) + 1 + message = "#{self.template.filepath}:#{line}:in `#{self.name}'" + notifier.fatal "[ERROR] #{exception.message} - #{message}\n".red + exception.backtrace.unshift message + end + end + + class MounterException < DefaultException() ; end + class GeneratorException < DefaultException() ; end + + end +end diff --git a/lib/locomotive/common/plugins/notifier.rb b/lib/locomotive/common/plugins/notifier.rb new file mode 100644 index 0000000..8436e55 --- /dev/null +++ b/lib/locomotive/common/plugins/notifier.rb @@ -0,0 +1,19 @@ +module Locomotive + module Common + module Plugins + + class Notifier + def initialize(exception) + exception.notifier.extend(UiDevNull) + end + + module UiWithBeep + def fatal(*) + nil # Locomotive::Steam::Logger.fatal args.first + end + end + end + + end + end +end \ No newline at end of file diff --git a/lib/locomotive/common/version.rb b/lib/locomotive/common/version.rb new file mode 100644 index 0000000..e9b5f3f --- /dev/null +++ b/lib/locomotive/common/version.rb @@ -0,0 +1,5 @@ +module Locomotive + module Common + VERSION = '0.0.1' + end +end \ No newline at end of file diff --git a/locomotivecms_common.gemspec b/locomotivecms_common.gemspec new file mode 100644 index 0000000..81ef988 --- /dev/null +++ b/locomotivecms_common.gemspec @@ -0,0 +1,24 @@ +require_relative 'lib/common' + +Gem::Specification.new do |spec| + spec.name = 'locomotivecms_common' + spec.version = Locomotive::Common::VERSION + spec.authors = ['Didier Lafforgue', 'Arnaud Sellenet', 'Joel Azemar'] + spec.email = ['did@locomotivecms.com', 'arnaud@sellenet.fr', 'joel.azemar@gmail.com'] + spec.description = %q{The LocomotiveCMS Common is a shared libraries package} + spec.summary = %q{The LocomotiveCMS Common is a shared libraries package for all LocomotiveCMS dependencies} + spec.homepage = 'http://www.locomotivecms.com' + spec.homepage = 'https://github.com/locomotivecms/common' + spec.license = 'MIT' + + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ['lib'] + + spec.add_development_dependency 'bundler', '~> 1.5' + spec.add_development_dependency 'rake', '~> 10.1' + spec.add_development_dependency 'rspec', '~> 2.14' + + spec.required_ruby_version = '~> 2.0' +end diff --git a/spec/locomotive/exception_spec.rb b/spec/locomotive/exception_spec.rb new file mode 100644 index 0000000..8b480d5 --- /dev/null +++ b/spec/locomotive/exception_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Locomotive::Common::DefaultException do + + specify do + expect do + raise DefaultException.new + end.to raise_error(DefaultException) + end + +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..09ba858 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,9 @@ +require 'rubygems' +require 'bundler/setup' + +require_relative '../lib/common' + +RSpec.configure do |config| + config.filter_run focused: true + config.run_all_when_everything_filtered = true +end \ No newline at end of file