typed 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/lib/typed.rb +1 -0
- data/lib/typed/scala.rb +60 -0
- data/lib/typed/version.rb +1 -1
- data/spec/scala_spec.rb +56 -0
- data/spec/spec_helper.rb +3 -0
- metadata +6 -4
data/.gitignore
CHANGED
data/lib/typed.rb
CHANGED
data/lib/typed/scala.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Typed
|
2
|
+
module Scala
|
3
|
+
def vars
|
4
|
+
unless @vars
|
5
|
+
vars = Typed::Hash.new
|
6
|
+
self.class.types.each_pair do |name, obj|
|
7
|
+
# vars.default[name] = obj
|
8
|
+
vars[name] = obj
|
9
|
+
end
|
10
|
+
@vars = vars
|
11
|
+
end
|
12
|
+
return @vars
|
13
|
+
end
|
14
|
+
|
15
|
+
def types
|
16
|
+
self.class.types
|
17
|
+
end
|
18
|
+
|
19
|
+
module Var
|
20
|
+
ParseError = Class.new(SyntaxError)
|
21
|
+
def types
|
22
|
+
@types ||= {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def register_var(name, obj)
|
26
|
+
types[name] = obj
|
27
|
+
|
28
|
+
define_method(name) { vars[name.to_s] }
|
29
|
+
define_method("#{name}=") {|v| vars[name.to_s] = v }
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse_var_name(caller)
|
33
|
+
# "/tmp/adsvr/dsl.rb:23"
|
34
|
+
case caller
|
35
|
+
when %r{^(.*?):(\d+)}o
|
36
|
+
file = $1
|
37
|
+
line = $2.to_i
|
38
|
+
@lines ||= File.readlines(file)
|
39
|
+
case @lines[line-1].to_s
|
40
|
+
when /^\s*var\s+(\S+)\s+=/o
|
41
|
+
return $1
|
42
|
+
else
|
43
|
+
raise ParseError, "#{self} from file:#{file} (line: #{line})"
|
44
|
+
end
|
45
|
+
else
|
46
|
+
raise ParseError, "#{self} from caller:#{caller[0]}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def var(obj)
|
51
|
+
name = parse_var_name(caller[0])
|
52
|
+
register_var(name, obj)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.included(klass)
|
57
|
+
klass.extend Scala::Var
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/typed/version.rb
CHANGED
data/spec/scala_spec.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Typed::Scala do
|
4
|
+
before { write_and_load(source) }
|
5
|
+
|
6
|
+
let(:source) { commented_source.gsub(/^\s*#/m, '') }
|
7
|
+
let(:user) { User.new }
|
8
|
+
subject { user}
|
9
|
+
|
10
|
+
def write_and_load(code)
|
11
|
+
path = tmp_path("scala/inline.rb")
|
12
|
+
path.parent.mkpath
|
13
|
+
path.open("w+"){|f| f.puts(code) }
|
14
|
+
load(path.to_s)
|
15
|
+
end
|
16
|
+
|
17
|
+
context "User" do
|
18
|
+
# comment outed for the editor issues
|
19
|
+
let(:commented_source) { <<-EOF
|
20
|
+
# class User
|
21
|
+
# include Typed::Scala
|
22
|
+
#
|
23
|
+
# var name = String
|
24
|
+
# var age = Fixnum
|
25
|
+
# end
|
26
|
+
EOF
|
27
|
+
}
|
28
|
+
|
29
|
+
it { should respond_to(:name) }
|
30
|
+
it { should respond_to(:age) }
|
31
|
+
it { should_not respond_to(:xxx) }
|
32
|
+
|
33
|
+
describe "#types" do
|
34
|
+
its(:types) { should == {
|
35
|
+
"age" => Fixnum,
|
36
|
+
"name" => String,
|
37
|
+
} }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe ".types" do
|
41
|
+
specify "same as User#type" do
|
42
|
+
user.types.should == User.types
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#name=" do
|
47
|
+
specify "accept 'maiha'" do
|
48
|
+
(user.name = 'maiha').should == 'maiha'
|
49
|
+
end
|
50
|
+
|
51
|
+
specify "reject 100" do
|
52
|
+
lambda { user.name = 100 }.should raise_error(TypeError)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typed
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 6
|
10
|
+
version: 0.2.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- maiha
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2013-08-26 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: activesupport
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/typed/default.rb
|
81
81
|
- lib/typed/events.rb
|
82
82
|
- lib/typed/hash.rb
|
83
|
+
- lib/typed/scala.rb
|
83
84
|
- lib/typed/schema.rb
|
84
85
|
- lib/typed/version.rb
|
85
86
|
- spec/changes_spec.rb
|
@@ -89,6 +90,7 @@ files:
|
|
89
90
|
- spec/hash_spec.rb
|
90
91
|
- spec/merge_spec.rb
|
91
92
|
- spec/path_spec.rb
|
93
|
+
- spec/scala_spec.rb
|
92
94
|
- spec/schema_spec.rb
|
93
95
|
- spec/spec_helper.rb
|
94
96
|
- spec/time_spec.rb
|