type_struct 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 33f3efe5ddc683da818f42a1d03862f760279efe
4
- data.tar.gz: 2bd91598e3ce93198d2fa94b8e4bdf533bfebfb2
3
+ metadata.gz: a27067e9d45914510c4dc7ac39e5927ac10169e4
4
+ data.tar.gz: 55b6804d69ca33abed2512f981262bf4131640e6
5
5
  SHA512:
6
- metadata.gz: 996654d99f6f9502831e06bbc39b0052be5094b4220579f08d9c784c7e9cc98992b738ebd4b539f29e2ae3a3947bf2b64fd959fbee1661032681067d9103b78f
7
- data.tar.gz: 239ebbdff12351ec80082965174fe5c06630dbe44c3506c09d4ce8a712e15b0bab50b9ae5f35e3a1ea631f5464a5d7c09c6bd531551a46ddd8cb9a1989fffe7f
6
+ metadata.gz: 69fa0b767895bb4acfc900ee449c7c7c364f37dcdfadeb9d355ecf6bfec8aa18d360ead266e728143c7760b27118c6e20ea0d960a176c31168c51d9279faa6ec
7
+ data.tar.gz: 1360dc44909bb05bda962e05e2705406381af5f017044d411e6afb954c8415d84dd2e85beeeba412fdecb82ed491d2fa6d215273ff5a965b36efa540156fdcbe
data/README.md CHANGED
@@ -16,13 +16,13 @@ class Sample < TypeStruct.new(
16
16
 
17
17
  sample = Sample.new(
18
18
  str: "instance of String",
19
- reg: "not match to regexp",
19
+ reg: "match to regexp",
20
20
  num: 10,
21
21
  any: true,
22
22
  )
23
23
 
24
24
  p sample
25
- #=> #<TypeStructTest::Sample str="instance of String", reg="not match to regexp", num=10, any=true>
25
+ #=> #<Sample str="instance of String", reg="not match to regexp", num=10, any=true>
26
26
 
27
27
  p sample.to_h
28
28
  #=> {:str=>"instance of String", :reg=>"not match to regexp", :num=>10, :any=>true}
@@ -1,3 +1,3 @@
1
1
  class TypeStruct
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/type_struct.rb CHANGED
@@ -7,7 +7,6 @@ class TypeStruct
7
7
  class << self
8
8
  def new(**args)
9
9
  Class.new do
10
- attr_accessor *args.keys
11
10
  const_set :MEMBERS, args
12
11
 
13
12
  class << self
@@ -24,6 +23,20 @@ class TypeStruct
24
23
  end
25
24
  end
26
25
 
26
+ args.keys.each do |k, _|
27
+ define_method(k) do
28
+ instance_variable_get("@#{k}")
29
+ end
30
+
31
+ define_method("#{k}=") do |v|
32
+ raise TypeStruct::NoMemberError unless respond_to?(k)
33
+ unless self.class.valid?(k, v)
34
+ raise TypeError, "expect #{self.class.type(k)} got #{v.class}"
35
+ end
36
+ instance_variable_set("@#{k}", v)
37
+ end
38
+ end
39
+
27
40
  def initialize(**arg)
28
41
  self.class.members.each do |k, _|
29
42
  self[k] = arg[k]
@@ -31,15 +44,10 @@ class TypeStruct
31
44
  end
32
45
 
33
46
  def []=(k, v)
34
- raise TypeStruct::NoMemberError unless respond_to?(k)
35
- unless self.class.valid?(k, v)
36
- raise TypeError, "expect #{self.class.type(k)} got #{v.class}"
37
- end
38
47
  __send__("#{k}=", v)
39
48
  end
40
49
 
41
50
  def [](k)
42
- raise TypeStruct::NoMemberError unless respond_to?(k)
43
51
  __send__(k)
44
52
  end
45
53
 
@@ -77,10 +77,19 @@ module TypeStructTest
77
77
  if err != nil
78
78
  t.error("expect not raise error when valid value get. got #{err}")
79
79
  end
80
+ _, err = go { dummy.str }
81
+ if err != nil
82
+ t.error("expect not raise error when valid value get. got #{err}")
83
+ end
84
+
80
85
  _, err = go { dummy[:nothing] }
81
86
  if err == nil
82
87
  t.error("expect not raise error when invalid value get")
83
88
  end
89
+ _, err = go { dummy.nothing }
90
+ if err == nil
91
+ t.error("expect not raise error when invalid value get")
92
+ end
84
93
  end
85
94
 
86
95
  def test_setter(t)
@@ -90,17 +99,29 @@ module TypeStructTest
90
99
  if err == nil
91
100
  t.error("expect raise error when invalid value set")
92
101
  end
102
+ _, err = go { dummy.__send__("#{k}=", nil) }
103
+ if err == nil
104
+ t.error("expect raise error when invalid value set")
105
+ end
93
106
  end
94
107
 
95
108
  _, err = go { dummy[:any] = nil }
96
109
  if err != nil
97
110
  t.error("expect not raise error when valid value set got #{err}")
98
111
  end
112
+ _, err = go { dummy.any = nil }
113
+ if err != nil
114
+ t.error("expect not raise error when valid value set got #{err}")
115
+ end
99
116
 
100
117
  _, err = go { dummy[:nothing] = nil }
101
118
  if err == nil
102
119
  t.error("expect not raise error when valid value set got #{err}")
103
120
  end
121
+ _, err = go { dummy.nothing = nil }
122
+ if err == nil
123
+ t.error("expect not raise error when valid value set got #{err}")
124
+ end
104
125
  end
105
126
 
106
127
  class Sample < TypeStruct.new(
@@ -113,15 +134,15 @@ module TypeStructTest
113
134
  def example_readme
114
135
  sample = Sample.new(
115
136
  str: "instance of String",
116
- reg: "not match to regexp",
137
+ reg: "match to regexp",
117
138
  num: 10,
118
139
  any: true,
119
140
  )
120
141
  p sample
121
142
  p sample.to_h
122
143
  # Output:
123
- # #<TypeStructTest::Sample str="instance of String", reg="not match to regexp", num=10, any=true>
124
- # {:str=>"instance of String", :reg=>"not match to regexp", :num=>10, :any=>true}
144
+ # #<TypeStructTest::Sample str="instance of String", reg="match to regexp", num=10, any=true>
145
+ # {:str=>"instance of String", :reg=>"match to regexp", :num=>10, :any=>true}
125
146
  end
126
147
 
127
148
  private
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: type_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-20 00:00:00.000000000 Z
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.10'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.10'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rgot
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Imitating static typed struct.
@@ -59,8 +59,8 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
63
- - ".travis.yml"
62
+ - .gitignore
63
+ - .travis.yml
64
64
  - Gemfile
65
65
  - LICENSE.txt
66
66
  - README.md
@@ -79,17 +79,17 @@ require_paths:
79
79
  - lib
80
80
  required_ruby_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - ">="
82
+ - - '>='
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - '>='
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project:
92
- rubygems_version: 2.4.5.1
92
+ rubygems_version: 2.0.14
93
93
  signing_key:
94
94
  specification_version: 4
95
95
  summary: Imitating static typed struct.