Skip to content

Commit

Permalink
Extensible prototype.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaHydrae committed Oct 10, 2014
1 parent 1c67081 commit 48385b7
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
2.1.2
14 changes: 14 additions & 0 deletions lib/errapi/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Errapi

class Configuration
attr_reader :plugins

def initialize
@plugins = []
end

def plugin plugin
@plugins << plugin
end
end
end
7 changes: 7 additions & 0 deletions lib/errapi/plugins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Errapi

module Plugins
end
end

Dir[File.join File.dirname(__FILE__), File.basename(__FILE__, '.*'), '*.rb'].each{ |lib| require lib }
17 changes: 17 additions & 0 deletions lib/errapi/plugins/message.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Errapi::Plugins

module Message

module ValidationErrorMixin
attr_accessor :message
end

def self.build_error error, options
error.message = options[:message]
end

def self.error_matches? error, criteria
!criteria.key?(:message) || (criteria[:message].kind_of?(Regexp) ? !!error.message.match(criteria[:message]) : error.message == criteria[:message])
end
end
end
23 changes: 20 additions & 3 deletions lib/errapi/validation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@ module Errapi
class ValidationContext
attr_reader :errors

def initialize
def initialize config

@config = config
@errors = []

@error_class = Class.new do
config.plugins.each do |plugin|
include plugin.const_get('ValidationErrorMixin') if plugin.const_defined? 'ValidationErrorMixin'
end
end
end

def add message, options = {}
@errors << ValidationError.new(message, options)
def add options = {}

error = @error_class.new

@config.plugins.each do |plugin|
plugin.build_error error, options if plugin.respond_to? :build_error
end

yield error if block_given?

@errors << error
end

def error? criteria = {}
Expand Down
8 changes: 0 additions & 8 deletions lib/errapi/validation_error.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
module Errapi

class ValidationError
attr_accessor :message, :code, :type, :location

def initialize message, options = {}
@message = message
@code = options[:code]
@type = options[:type]
@location = options[:location]
end
end
end
21 changes: 21 additions & 0 deletions spec/example_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'helper'

RSpec.describe 'errapi' do

let(:config){ Errapi::Configuration.new }
let(:context){ Errapi::ValidationContext.new config }

before :each do
config.plugin Errapi::Plugins::Message
end

it "should collect and find errors" do

context.add message: 'foo'
context.add message: 'bar'
context.add message: 'baz'

%w(foo bar baz).each{ |message| expect(context.error?(message: message)).to be(true) }
expect(context.error?(message: /ba/)).to be(true)
end
end
33 changes: 0 additions & 33 deletions spec/validation_context_spec.rb

This file was deleted.

24 changes: 0 additions & 24 deletions spec/validation_error_spec.rb

This file was deleted.

0 comments on commit 48385b7

Please sign in to comment.