guess_struct 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +5 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/guess_struct.gemspec +23 -0
- data/lib/guess_struct.rb +93 -0
- data/lib/guess_struct/version.rb +3 -0
- data/lib/guess_struct_test.rb +84 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0439c5bf1d615fc6cf3062d350592c3e13f4a25c
|
4
|
+
data.tar.gz: e38c5c01da34c76554dadf11239981420bc8aa5b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 09acb1cd80c7c4d866a6fbe3cfb699c748b8cc5bcdfb151a8791af69b55e9b5462b76e2cf9ad81004835b4abd51ac42b297338ec16c22d5d102312ae9921578b
|
7
|
+
data.tar.gz: 4fce5d3281161d2684c3af8a0f73af3c8adf33da42d3116312db6d6dfb2851ba548124c14db7d7e85082922eab8caa0e842c5c12ff726ef182fd49355254b6af
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 ksss
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# GuessStruct
|
2
|
+
|
3
|
+
Struct class that guess attributes type.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
A = GuessStruct.new(:foo, :bar, :baz)
|
7
|
+
A.definition #=> {:foo=>nil, :bar=>nil, :baz=>nil}
|
8
|
+
a = A.new(foo: 1)
|
9
|
+
A.definition #=> {:foo=>Fixnum, :bar=>nil, :baz=>nil}
|
10
|
+
a.foo #=> 1
|
11
|
+
a.to_h #=> {:foo => 1, :bar => nil, :baz => nil}
|
12
|
+
a.foo = 'a' #=> GuessStruct::GuessError: A#foo expect Fixnum got "a"
|
13
|
+
|
14
|
+
# allow nil input. But not clear defined type.
|
15
|
+
a.foo = nil
|
16
|
+
A.definition #=> {:foo=>Fixnum, :bar=>nil, :baz=>nil}
|
17
|
+
|
18
|
+
# share guess type in class
|
19
|
+
A.new(foo: :sym) #=> GuessStruct::GuessError: A#foo expect Fixnum got "a"
|
20
|
+
A.new(foo: 10) #=> #<A foo=10, bar=nil, baz=nil>
|
21
|
+
```
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'guess_struct'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install guess_struct
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "guess_struct"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "guess_struct/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "guess_struct"
|
8
|
+
spec.version = GuessStruct::VERSION
|
9
|
+
spec.authors = ["ksss"]
|
10
|
+
spec.email = ["co000ri@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q(Type guess struct)
|
13
|
+
spec.description = %q(Type guess struct)
|
14
|
+
spec.homepage = "https://github.com/ksss/guess_struct"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rgot"
|
23
|
+
end
|
data/lib/guess_struct.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
class GuessStruct
|
2
|
+
require "guess_struct/version"
|
3
|
+
GuessError = Class.new(StandardError)
|
4
|
+
|
5
|
+
def initialize(**arg)
|
6
|
+
sym_arg = {}
|
7
|
+
arg.each do |k, v|
|
8
|
+
sym_arg[k.to_sym] = v
|
9
|
+
end
|
10
|
+
self.class.members.each do |k|
|
11
|
+
self[k] = sym_arg[k]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?(key, value)
|
16
|
+
t = self.class.definition[key]
|
17
|
+
return true if t.nil?
|
18
|
+
return true if value.nil?
|
19
|
+
return true if t === value
|
20
|
+
false
|
21
|
+
end
|
22
|
+
|
23
|
+
def [](key)
|
24
|
+
__send__ key
|
25
|
+
end
|
26
|
+
|
27
|
+
def []=(key, value)
|
28
|
+
__send__ "#{key}=", value
|
29
|
+
end
|
30
|
+
|
31
|
+
def inspect
|
32
|
+
m = to_h.map do |k, v|
|
33
|
+
"#{k}=#{v.inspect}"
|
34
|
+
end
|
35
|
+
"#<#{self.class} #{m.join(', ')}>"
|
36
|
+
end
|
37
|
+
alias to_s inspect
|
38
|
+
|
39
|
+
def to_h
|
40
|
+
m = {}
|
41
|
+
self.class.members.each do |k|
|
42
|
+
m[k] = self[k]
|
43
|
+
end
|
44
|
+
m
|
45
|
+
end
|
46
|
+
|
47
|
+
class << self
|
48
|
+
def definition
|
49
|
+
const_get(:DEFINITION)
|
50
|
+
end
|
51
|
+
|
52
|
+
def members
|
53
|
+
const_get(:MEMBERS)
|
54
|
+
end
|
55
|
+
|
56
|
+
def clear
|
57
|
+
d = definition
|
58
|
+
d.each_key do |k|
|
59
|
+
d[k] = nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
alias original_new new
|
64
|
+
def new(*args, &block)
|
65
|
+
c = Class.new(GuessStruct) do
|
66
|
+
const_set :MEMBERS, args
|
67
|
+
const_set :DEFINITION, Hash[members.map { |i| [i, nil] }]
|
68
|
+
|
69
|
+
class << self
|
70
|
+
alias_method :new, :original_new
|
71
|
+
end
|
72
|
+
|
73
|
+
args.each do |k|
|
74
|
+
define_method(k) do
|
75
|
+
instance_variable_get("@#{k}")
|
76
|
+
end
|
77
|
+
|
78
|
+
define_method("#{k}=") do |v|
|
79
|
+
unless valid?(k, v)
|
80
|
+
raise GuessError, "#{self.class}##{k} expect #{self.class.definition.fetch(k)} got #{v.inspect}"
|
81
|
+
end
|
82
|
+
self.class.definition[k] = v.class if !v.nil?
|
83
|
+
instance_variable_set("@#{k}", v)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
if block_given?
|
88
|
+
c.module_eval(&block)
|
89
|
+
end
|
90
|
+
c
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require "guess_struct"
|
2
|
+
|
3
|
+
module GuessStructTest
|
4
|
+
def test_class(t)
|
5
|
+
s = GuessStruct.new(:foo, :bar, :baz)
|
6
|
+
unless Class === s
|
7
|
+
t.error("GuessStruct instance is not Class")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_make_struct(t)
|
12
|
+
s = GuessStruct.new(:foo, :bar, :baz)
|
13
|
+
si = s.new
|
14
|
+
si.foo = 1
|
15
|
+
unless 1 == si.foo
|
16
|
+
t.error("set value was changed")
|
17
|
+
end
|
18
|
+
si.foo = 2
|
19
|
+
unless 2 == si.foo
|
20
|
+
t.error("set value was changed")
|
21
|
+
end
|
22
|
+
|
23
|
+
unless si.bar.nil? && si.baz.nil?
|
24
|
+
t.error("other attr was changed")
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
si.foo = "a"
|
29
|
+
rescue GuessStruct::GuessError
|
30
|
+
else
|
31
|
+
t.error("Not raise error when different type from first")
|
32
|
+
end
|
33
|
+
|
34
|
+
si = s.new
|
35
|
+
begin
|
36
|
+
si.foo = "a"
|
37
|
+
rescue GuessStruct::GuessError
|
38
|
+
else
|
39
|
+
t.error("miss share guess class")
|
40
|
+
end
|
41
|
+
si.foo = 1
|
42
|
+
si.foo = nil
|
43
|
+
si.foo = 5
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_make_struct_with_key_args(t)
|
47
|
+
s = GuessStruct.new(:foo, :bar, :baz)
|
48
|
+
si = s.new(foo: "a")
|
49
|
+
unless "a" == si.foo
|
50
|
+
t.error("initial value dose not setted")
|
51
|
+
end
|
52
|
+
|
53
|
+
si.foo = "b"
|
54
|
+
unless "b" == si.foo
|
55
|
+
t.error("initial value dose not setted")
|
56
|
+
end
|
57
|
+
|
58
|
+
begin
|
59
|
+
si.foo = 1
|
60
|
+
rescue GuessStruct::GuessError
|
61
|
+
else
|
62
|
+
t.error("invalid value")
|
63
|
+
end
|
64
|
+
|
65
|
+
begin
|
66
|
+
s.new(foo: 1)
|
67
|
+
rescue GuessStruct::GuessError
|
68
|
+
else
|
69
|
+
t.error("invalid value")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_clear(t)
|
74
|
+
s = GuessStruct.new(:foo, :bar, :baz)
|
75
|
+
si = s.new(foo: :sym)
|
76
|
+
unless s.definition == { foo: Symbol, bar: nil, baz: nil }
|
77
|
+
t.error("definition miss")
|
78
|
+
end
|
79
|
+
s.clear
|
80
|
+
unless s.definition == { foo: nil, bar: nil, baz: nil }
|
81
|
+
t.error("clear miss")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: guess_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ksss
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rgot
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Type guess struct
|
56
|
+
email:
|
57
|
+
- co000ri@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- bin/console
|
68
|
+
- bin/setup
|
69
|
+
- guess_struct.gemspec
|
70
|
+
- lib/guess_struct.rb
|
71
|
+
- lib/guess_struct/version.rb
|
72
|
+
- lib/guess_struct_test.rb
|
73
|
+
homepage: https://github.com/ksss/guess_struct
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.5.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Type guess struct
|
97
|
+
test_files: []
|