typed 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,8 +4,11 @@ require "typed/version"
4
4
  require "typed/hash"
5
5
 
6
6
  module Typed
7
- NotDefined = Class.new(RuntimeError)
8
- FixedValue = Class.new(RuntimeError)
7
+ Boolean = Must::Boolean
8
+
9
+ NotDefined = Class.new(RuntimeError)
10
+ FixedValue = Class.new(RuntimeError)
11
+ SizeMismatch = Class.new(RuntimeError)
9
12
 
10
13
  autoload :Schema , "typed/schema"
11
14
  autoload :Default, "typed/default"
@@ -15,19 +15,16 @@ module Typed
15
15
  end
16
16
 
17
17
  def [](key)
18
- if __attrs__.schema.exist?(key)
19
- __attrs__[key.to_s]
18
+ if self.class.variables[key.to_s]
19
+ self.__send__(key)
20
20
  else
21
21
  raise Typed::NotDefined, "#{key} is not a member of #{self.class}"
22
22
  end
23
23
  end
24
24
 
25
25
  def []=(key, val)
26
- if __attrs__.schema.exist?(key)
27
- if self.class.vals[key.to_s] and __attrs__.exist?(key)
28
- raise Typed::FixedValue, "reassignment to #{key}"
29
- end
30
- __attrs__[key.to_s] = val
26
+ if self.class.variables[key.to_s]
27
+ self.__send__("#{key}=", val)
31
28
  else
32
29
  raise Typed::NotDefined, "#{key} is not a member of #{self.class}"
33
30
  end
@@ -4,8 +4,25 @@ module Typed
4
4
  ######################################################################
5
5
  ### Builder
6
6
  module Builder
7
+ def check(hash)
8
+ build(hash)
9
+ return hash
10
+ end
11
+
7
12
  # Build instance from hash
8
- def build(hash = {})
13
+ def build(hash)
14
+ hash.must(::Hash) { raise ArgumentError, "#{self} expects Hash, but got #{hash.class}" }
15
+
16
+ if hash.size != variables.size
17
+ keys1 = variables.keys
18
+ keys2 = hash.keys.map(&:to_s)
19
+ minus = (keys1 - keys2).map{|i| "-#{i}"}
20
+ plus = (keys2 - keys1).map{|i| "+#{i}"}
21
+
22
+ msg = "#{self} expects #{variables.size}, but got #{hash.size} (%s)" % (minus + plus).join(",")
23
+ raise Typed::SizeMismatch, msg
24
+ end
25
+
9
26
  obj = new
10
27
  hash.each_pair do |k,v|
11
28
  obj[k] = v
@@ -16,7 +33,7 @@ module Typed
16
33
  # Build instance from array
17
34
  def apply(*args)
18
35
  if args.size > variables.size
19
- raise "#{self}.apply expect #{variables.size} args, but got #{args.size}"
36
+ raise Typed::SizeMismatch, "#{self}.apply expects #{variables.size} args, but got #{args.size}"
20
37
  end
21
38
 
22
39
  obj = new
@@ -30,7 +47,7 @@ module Typed
30
47
  # Build instance from array strictly. Raised when args size is differ.
31
48
  def apply!(*args)
32
49
  if args.size != variables.size
33
- raise "#{self}.apply expect #{variables.size} args, but got #{args.size}"
50
+ raise Typed::SizeMismatch, "#{self} expects #{variables.size} args, but got #{args.size}"
34
51
  end
35
52
 
36
53
  obj = new
@@ -10,7 +10,7 @@ module Typed
10
10
 
11
11
  def apply(klass, type, caller, obj)
12
12
  dcl = parse(klass, type, caller)
13
- dcl.value = obj
13
+ dcl.value = (obj == Typed::Hash) ? ::Hash : obj
14
14
 
15
15
  define_schema(dcl)
16
16
  define_method(dcl)
@@ -25,14 +25,26 @@ module Typed
25
25
  end
26
26
 
27
27
  def define_method(dcl)
28
- name = dcl.name
28
+ name = dcl.name
29
+ klass = dcl.klass
30
+ typed = [dcl.value].flatten.map{|k| (k == ::Hash) ? '::Hash' : k.to_s}.join(', ')
31
+
29
32
  dcl.klass.class_eval <<-STR, __FILE__, __LINE__ + 1
30
33
  def #{name}
31
- self['#{name}']
34
+ k = '#{name}'
35
+ h = __attrs__
36
+ raise Typed::NotDefined, "'#{name}' is not initialized" unless h.key?(k)
37
+ return h[k]
32
38
  end
33
39
 
34
40
  def #{name}=(v)
35
- self['#{name}'] = v
41
+ k = '#{name}'
42
+ h = __attrs__
43
+ if self.class.vals[k] and h.key?(k)
44
+ raise Typed::FixedValue, "reassignment to %s" % k
45
+ end
46
+ v.must(#{typed}) { raise TypeError, '#{klass}##{name}= expected #{typed} but got %s' % [v.class] }
47
+ h[k] = v
36
48
  end
37
49
  STR
38
50
  end
@@ -60,10 +72,7 @@ module Typed
60
72
  end
61
73
 
62
74
  def build_attrs(klass)
63
- attrs = Typed::Hash.new
64
- klass.vals.each_pair{|name, obj| attrs[name] = obj}
65
- klass.vars.each_pair{|name, obj| attrs[name] = obj}
66
- return attrs
75
+ return {}
67
76
  end
68
77
 
69
78
  def build(klass)
@@ -1,3 +1,3 @@
1
1
  module Typed
2
- VERSION = "0.2.8"
2
+ VERSION = "0.2.9"
3
3
  end
@@ -27,12 +27,6 @@ describe Typed::Scala do
27
27
  end
28
28
 
29
29
  describe ".build" do
30
- context "()" do
31
- subject { A.build }
32
- specify { lambda { subject.key }.should raise_error(Typed::NotDefined) }
33
- specify { lambda { subject.attrs }.should raise_error(Typed::NotDefined) }
34
- end
35
-
36
30
  context '(:key=>"x", :attrs=>{:a=>1})' do
37
31
  subject { A.build(:key=>"x", :attrs=>{:a=>1}) }
38
32
  its(:key) { should == "x" }
@@ -41,13 +41,13 @@ describe Typed::Scala do
41
41
  end
42
42
 
43
43
  specify "more args" do
44
- lambda { User.apply("001", "aya", 12, "!") }.should raise_error(/expect 3 args/)
44
+ lambda { User.apply("001", "aya", 12, "!") }.should raise_error(/expects 3 args/)
45
45
  end
46
46
  end
47
47
 
48
48
  describe ".apply!" do
49
49
  specify "less args" do
50
- lambda { User.apply!("001", "aya") }.should raise_error(/expect 3 args/)
50
+ lambda { User.apply!("001", "aya") }.should raise_error(/expects 3 args/)
51
51
  end
52
52
 
53
53
  specify "exact args" do
@@ -58,29 +58,64 @@ describe Typed::Scala do
58
58
  end
59
59
 
60
60
  specify "more args" do
61
- lambda { User.apply!("001", "aya", 12, "!") }.should raise_error(/expect 3 args/)
61
+ lambda { User.apply!("001", "aya", 12, "!") }.should raise_error(/expects 3 args/)
62
62
  end
63
63
  end
64
64
 
65
- describe ".build" do
66
- # "creates a new instance"
67
- its(:build) { should be_kind_of User }
65
+ describe ".check" do
66
+ before { @hash = {:key=>"turi", :name=>"aya", :age=>12 } }
67
+ subject { User.check(@hash) }
68
68
 
69
- specify "ok" do
70
- User.build.should be_kind_of(User)
71
- User.build(:name=>"foo").name.should == "foo"
69
+ specify "(valid hash) return given hash itself" do
70
+ expect( subject ).to eq @hash
71
+ end
72
72
 
73
- user = User.build(:key=>"x", :age=>12)
74
- user.key.should == "x"
75
- user.age.should == 12
73
+ specify "(invalid hash) raise errors" do
74
+ @hash.clear
75
+ expect { subject }.to raise_error(Typed::SizeMismatch)
76
+ end
77
+ end
78
+
79
+
80
+ describe ".build" do
81
+ specify "(no args) raise ArgumentError" do
82
+ expect { User.build }.to raise_error(ArgumentError)
76
83
  end
77
84
 
78
- specify "not member" do
79
- lambda { User.build(:foo => "!") }.should raise_error(Typed::NotDefined)
85
+ specify "(non hash args) raise ArgumentError" do
86
+ expect { User.build([]) }.to raise_error(ArgumentError)
80
87
  end
81
88
 
82
- specify "type error" do
83
- lambda { User.build(:age => "!") }.should raise_error(TypeError)
89
+ context "(hash)" do
90
+ before { @hash = {:key=>"turi", :name=>"aya", :age=>12 } }
91
+ subject { User.build(@hash) }
92
+
93
+ its(:key ) { should == "turi" }
94
+ its(:name) { should == "aya" }
95
+ its(:age ) { should == 12 }
96
+
97
+ specify "when less args, raise Typed::SizeMismatch" do
98
+ @hash.delete(:age)
99
+ expect { subject }.to raise_error(Typed::SizeMismatch)
100
+ expect { subject }.to raise_error(/age/)
101
+ end
102
+
103
+ specify "when more args, raise Typed::SizeMismatch" do
104
+ @hash[:extra] = 10
105
+ expect { subject }.to raise_error(Typed::SizeMismatch)
106
+ expect { subject }.to raise_error(/extra/)
107
+ end
108
+
109
+ specify "when unknown field, raise Typed::NotDefined" do
110
+ @hash.delete(:age)
111
+ @hash[:xxx] = 100
112
+ expect { subject }.to raise_error(Typed::NotDefined)
113
+ end
114
+
115
+ specify "when type mismatch, raise TypeError" do
116
+ @hash[:age] = "!"
117
+ expect { subject }.to raise_error(TypeError)
118
+ end
84
119
  end
85
120
  end
86
121
  end
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_dependency "activesupport"
22
- s.add_dependency "must", ">= 0.2.7"
22
+ s.add_dependency "must", ">= 0.3.2"
23
23
 
24
24
  s.add_development_dependency "rspec"
25
25
  end
metadata CHANGED
@@ -1,76 +1,71 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: typed
3
- version: !ruby/object:Gem::Version
4
- hash: 7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.9
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 8
10
- version: 0.2.8
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - maiha
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2013-09-03 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2013-10-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: activesupport
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: must
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
38
25
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 25
43
- segments:
44
- - 0
45
- - 2
46
- - 7
47
- version: 0.2.7
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: must
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.3.2
48
38
  type: :runtime
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
51
- name: rspec
52
39
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
54
41
  none: false
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
- version: "0"
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.3.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
62
54
  type: :development
63
- version_requirements: *id003
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
64
62
  description: Typed::Hash
65
- email:
63
+ email:
66
64
  - maiha@wota.jp
67
65
  executables: []
68
-
69
66
  extensions: []
70
-
71
67
  extra_rdoc_files: []
72
-
73
- files:
68
+ files:
74
69
  - .gitignore
75
70
  - Gemfile
76
71
  - README.rdoc
@@ -109,36 +104,26 @@ files:
109
104
  - typed.gemspec
110
105
  homepage: https://github.com/maiha/typed
111
106
  licenses: []
112
-
113
107
  post_install_message:
114
108
  rdoc_options: []
115
-
116
- require_paths:
109
+ require_paths:
117
110
  - lib
118
- required_ruby_version: !ruby/object:Gem::Requirement
111
+ required_ruby_version: !ruby/object:Gem::Requirement
119
112
  none: false
120
- requirements:
121
- - - ">="
122
- - !ruby/object:Gem::Version
123
- hash: 3
124
- segments:
125
- - 0
126
- version: "0"
127
- required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
118
  none: false
129
- requirements:
130
- - - ">="
131
- - !ruby/object:Gem::Version
132
- hash: 3
133
- segments:
134
- - 0
135
- version: "0"
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
136
123
  requirements: []
137
-
138
124
  rubyforge_project: typed
139
- rubygems_version: 1.8.15
125
+ rubygems_version: 1.8.23
140
126
  signing_key:
141
127
  specification_version: 3
142
128
  summary: A Ruby library for Typed variables
143
129
  test_files: []
144
-