obvious 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -204,6 +204,13 @@ class Hash
204
204
  return_value.call true, nil
205
205
  end
206
206
 
207
+ def nil_fields? list
208
+ list.each do |field|
209
+ return true, field unless self[field]
210
+ end
211
+
212
+ return false, nil
213
+ end
207
214
  end
208
215
 
209
216
  class ContractInputError < StandardError
@@ -0,0 +1,38 @@
1
+ module Obvious
2
+ module Obj
3
+ class << self
4
+ def included(base)
5
+ base.extend ClassMethods
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def define method, input = {}, &block
12
+ define_method(method) do |method_input = {}|
13
+ block_input = {}
14
+ method_input.each do |k,v|
15
+ if input[k].nil?
16
+ raise ArgumentError.new "invalid input field #{k}"
17
+ end
18
+
19
+ unless v.is_a? input[k][1]
20
+ raise ArgumentError.new "invalid type for #{k} expected #{input[k][1]}"
21
+ end
22
+
23
+ block_input[input[k][0]] = v
24
+ end
25
+
26
+ input.each do |k,v|
27
+ if block_input[v[0]].nil?
28
+ raise ArgumentError.new "missing input field #{k}"
29
+ end
30
+ end
31
+
32
+ self.instance_exec block_input, &block
33
+ end
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Obvious
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
data/spec/obj_spec.rb ADDED
@@ -0,0 +1,64 @@
1
+ require_relative '../lib/obvious/obj'
2
+
3
+ class TestObj
4
+ include Obvious::Obj
5
+
6
+ def initialize
7
+ @local = 'set!'
8
+ end
9
+
10
+ define :defined_method, with_foo: [:foo, String], also_bar: [:bar, Fixnum] do |input|
11
+ input
12
+ end
13
+
14
+ define :defined_local do |input|
15
+ @local
16
+ end
17
+
18
+ end
19
+
20
+ describe Obvious::Obj do
21
+
22
+ before do
23
+ @test = TestObj.new
24
+ end
25
+
26
+ describe 'self.define' do
27
+ it 'should do the right thing with correct input' do
28
+ result = @test.defined_method with_foo: 'hello', also_bar: 12
29
+ result.should eq foo: 'hello', bar: 12
30
+ end
31
+
32
+ it 'should have access to instance variables' do
33
+ result = @test.defined_local
34
+ result.should eq 'set!'
35
+ end
36
+
37
+ it 'should raise an error for missing parameters' do
38
+ expect { @test.defined_method with_foo: 'hello' }.to raise_error { |error|
39
+ error.should be_a ArgumentError
40
+ error.message.should eq 'missing input field also_bar'
41
+ }
42
+ end
43
+
44
+ it 'should raise an error for extra parameters' do
45
+ expect { @test.defined_method with_foo: 'hello', also_bar: 12, and_extra: 'this is extra!' }.to raise_error { |error|
46
+ error.should be_a ArgumentError
47
+ error.message.should eq 'invalid input field and_extra'
48
+ }
49
+ end
50
+
51
+ it 'should raise an error for invalid types' do
52
+ expect { @test.defined_method with_foo: 1, also_bar: 12 }.to raise_error { |error|
53
+ error.should be_a ArgumentError
54
+ error.message.should eq 'invalid type for with_foo expected String'
55
+ }
56
+
57
+ expect {@test.defined_method with_foo: 'hello', also_bar: nil }.to raise_error { |error|
58
+ error.should be_a ArgumentError
59
+ error.message.should eq 'invalid type for also_bar expected Fixnum'
60
+ }
61
+ end
62
+ end
63
+
64
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obvious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-30 00:00:00.000000000 Z
12
+ date: 2013-06-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70341046505020 !ruby/object:Gem::Requirement
16
+ requirement: &70139803373400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70341046505020
24
+ version_requirements: *70139803373400
25
25
  description: A set of tools to build apps using the Obvious Architecture
26
26
  email:
27
27
  - brianknapp@gmail.com
@@ -47,12 +47,14 @@ files:
47
47
  - lib/obvious/files/external/mongo_plug.rb
48
48
  - lib/obvious/files/external/mysql_plug.rb
49
49
  - lib/obvious/files/external/s3_plug.rb
50
+ - lib/obvious/obj.rb
50
51
  - lib/obvious/version.rb
51
52
  - obvious.gemspec
52
53
  - spec/.spec_helper.rb.swp
53
54
  - spec/contract_spec.rb
54
55
  - spec/entity_spec.rb
55
56
  - spec/generators/descriptor_spec.rb
57
+ - spec/obj_spec.rb
56
58
  - spec/spec_helper.rb
57
59
  homepage: http://obvious.retromocha.com/
58
60
  licenses: []
@@ -83,5 +85,6 @@ test_files:
83
85
  - spec/contract_spec.rb
84
86
  - spec/entity_spec.rb
85
87
  - spec/generators/descriptor_spec.rb
88
+ - spec/obj_spec.rb
86
89
  - spec/spec_helper.rb
87
90
  has_rdoc: