Skip to content

Commit

Permalink
fix: Refactors Obvious::Obj to be simpler (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianknapp authored Jan 28, 2022
1 parent 676987d commit a78c2f2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
26 changes: 12 additions & 14 deletions lib/obvious/obj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@ def included(base)
base.extend ClassMethods
end
end

module ClassMethods

def define method, input = {}, &block
define_method(method) do |method_input = {}|
def define method, input = {}, &block
define_method(method) do |method_input = {}|
block_input = {}
method_input.each do |k,v|
if input[k].nil?
method_input.each do |k,v|
if input[k].nil?
raise ArgumentError.new "invalid input field #{k}"
end

unless v.is_a? input[k][1]
raise ArgumentError.new "invalid type for #{k} expected #{input[k][1]}"
end
end

block_input[input[k][0]] = v
unless v.is_a? input[k]
raise ArgumentError.new "invalid type for #{k} expected #{input[k]}"
end
end

input.each do |k,v|
if block_input[v[0]].nil?
if method_input[k].nil?
raise ArgumentError.new "missing input field #{k}"
end
end

self.instance_exec block_input, &block
self.instance_exec method_input, &block
end
end

Expand Down
40 changes: 25 additions & 15 deletions spec/obj_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
require_relative '../lib/obvious/obj'

class TestObj
include Obvious::Obj
include Obvious::Obj

def initialize
def initialize
@local = 'set!'
end

define :defined_method, with_foo: [:foo, String], also_bar: [:bar, Integer] do |input|
define :defined_method, foo: String, bar: Integer do |input|
input
end

define :defined_local do |input|
@local
end

define :early_return_example do |input|
next true
false
end

end

describe Obvious::Obj do
Expand All @@ -25,39 +30,44 @@ def initialize

describe 'self.define' do
it 'should do the right thing with correct input' do
result = @test.defined_method with_foo: 'hello', also_bar: 12
result = @test.defined_method foo: 'hello', bar: 12
expect(result).to eq foo: 'hello', bar: 12
end

it 'should have access to instance variables' do
result = @test.defined_local
expect(result).to eq 'set!'
expect(result).to eq 'set!'
end

it 'should raise an error for missing parameters' do
expect { @test.defined_method with_foo: 'hello' }.to raise_error { |error|
expect { @test.defined_method foo: 'hello' }.to raise_error { |error|
expect(error).to be_a ArgumentError
expect(error.message).to eq 'missing input field also_bar'
expect(error.message).to eq 'missing input field bar'
}
end

it 'should raise an error for extra parameters' do
expect { @test.defined_method with_foo: 'hello', also_bar: 12, and_extra: 'this is extra!' }.to raise_error { |error|
expect { @test.defined_method foo: 'hello', bar: 12, extra: 'this is extra!' }.to raise_error { |error|
expect(error).to be_a ArgumentError
expect(error.message).to eq 'invalid input field and_extra'
expect(error.message).to eq 'invalid input field extra'
}
end

it 'should raise an error for invalid types' do
expect { @test.defined_method with_foo: 1, also_bar: 12 }.to raise_error { |error|
expect(error).to be_a ArgumentError
expect(error.message).to eq 'invalid type for with_foo expected String'
expect { @test.defined_method foo: 1, bar: 12 }.to raise_error { |error|
expect(error).to be_a ArgumentError
expect(error.message).to eq 'invalid type for foo expected String'
}

expect {@test.defined_method with_foo: 'hello', also_bar: nil }.to raise_error { |error|
expect {@test.defined_method foo: 'hello', bar: nil }.to raise_error { |error|
expect(error).to be_a ArgumentError
expect(error.message).to eq 'invalid type for also_bar expected Integer'
}
expect(error.message).to eq 'invalid type for bar expected Integer'
}
end

it 'should allow for early returns in control flow' do
result = @test.early_return_example
expect(result).to eq true
end
end

Expand Down

0 comments on commit a78c2f2

Please sign in to comment.