smalltalkable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
6
+ sample.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in smalltalkable.gemspec
4
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,78 @@
1
+ = Smalltalkable
2
+
3
+ * Smalltalkable (http://github.com/technohippy/smalltalkable)
4
+
5
+ == DESCRIPTION:
6
+
7
+ Smalltalkable is a library to write your ruby code in smalltalkish way.
8
+
9
+ == INSTALL:
10
+
11
+ * sudo gem install sapphire
12
+
13
+ == EXAMPLE:
14
+
15
+ === Code:
16
+
17
+ require 'smalltalkable'
18
+
19
+ Object.subclass :Counter,
20
+ instanceVariableNames: 'value step',
21
+ classVariableNames: 'instanceCount',
22
+ poolDictionaries: '',
23
+ category: 'Category-Name'
24
+
25
+ Counter.compile '
26
+ initialize value, step:step
27
+ @value = value
28
+ @step = step'
29
+ Counter.compile '
30
+ next
31
+ @value += @step'
32
+ Counter.compile '
33
+ value
34
+ @value'
35
+ Counter.compile '
36
+ setValue newValue
37
+ @value = newValue'
38
+
39
+ counter = Counter.new 0, step:2
40
+ ->{counter.value < 20}.while_true ->{
41
+ (counter.value % 3 == 0).if_true ->{
42
+ puts counter.value
43
+ }
44
+ counter.next
45
+ }
46
+
47
+ === Result:
48
+
49
+ $ ruby -I lib/ sample.rb
50
+ 0
51
+ 6
52
+ 12
53
+ 18
54
+
55
+ == LICENSE:
56
+
57
+ (The MIT License)
58
+
59
+ Copyright (c) 2012 ANDO Yasushi
60
+
61
+ Permission is hereby granted, free of charge, to any person obtaining
62
+ a copy of this software and associated documentation files (the
63
+ 'Software'), to deal in the Software without restriction, including
64
+ without limitation the rights to use, copy, modify, merge, publish,
65
+ distribute, sublicense, and/or sell copies of the Software, and to
66
+ permit persons to whom the Software is furnished to do so, subject to
67
+ the following conditions:
68
+
69
+ The above copyright notice and this permission notice shall be
70
+ included in all copies or substantial portions of the Software.
71
+
72
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
73
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
75
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
76
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
77
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
78
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,114 @@
1
+ require "smalltalkable/version"
2
+
3
+ module Smalltalkable
4
+ end
5
+
6
+ class Class
7
+ attr_accessor :category
8
+
9
+ def subclass(name, opt={})
10
+ klass = Class.new(self)
11
+ if opt[:instanceVariableNames]
12
+ opt[:instanceVariableNames].split(' ').each do |ivar|
13
+ klass.instance_variable_set "@#{ivar}", nil
14
+ end
15
+ end
16
+ if opt[:classVariableNames]
17
+ opt[:classVariableNames].split(' ').each do |cvar|
18
+ klass.class_variable_set "@@#{cvar}", nil
19
+ end
20
+ end
21
+ if opt[:poolDictionaries] && !opt[:poolDictionaries].empty?
22
+ raise NotImplementedError.new('pool dictionary is not available')
23
+ end
24
+ if opt[:category]
25
+ klass.category = opt[:category] unless opt[:category].empty?
26
+ end
27
+ Object.const_set name, klass
28
+ end
29
+
30
+ def compile(code)
31
+ sig, method = code.strip.split "\n", 2
32
+ name, str_arg = sig.split /\s+/, 2
33
+ args = []
34
+ pairs = {}
35
+ if str_arg
36
+ first_arg, other_args = str_arg.split /\s*,\s*/, 2
37
+ args.push first_arg
38
+ if other_args
39
+ args.push 'opts={}'
40
+ other_args.split(/\s+/).each do |pair|
41
+ key, value = pair.split /\s*:\s*/
42
+ pairs[key] = value
43
+ end
44
+ end
45
+ end
46
+
47
+ self.class_eval <<-EOS
48
+ def #{name.strip}(#{args.join ', '})
49
+ #{pairs.map do |k, v| "#{v} = opts[:#{k}]" end.join "\n"}
50
+ #{method}
51
+ end
52
+ EOS
53
+ end
54
+ end
55
+
56
+ class TrueClass
57
+ def if_true(true_proc, opts={})
58
+ true_proc.call
59
+ end
60
+ alias ifTrue if_true
61
+
62
+ def if_false(false_proc, opts={})
63
+ (opts[:if_true] || opts[:ifTrue] || ->{nil}).call
64
+ end
65
+ alias ifFalse if_false
66
+ end
67
+
68
+ class FalseClass
69
+ def if_true(true_proc, opts={})
70
+ (opts[:if_false] || opts[:ifFalse] || ->{nil}).call
71
+ end
72
+ alias ifTrue if_true
73
+
74
+ def if_false(false_proc, opts={})
75
+ false_proc.call
76
+ end
77
+ alias ifFalse if_false
78
+ end
79
+
80
+ class NilClass
81
+ def if_nil(true_proc, opts={})
82
+ true_proc.call
83
+ end
84
+ alias ifNil if_nil
85
+
86
+ def if_not_nil(true_proc, opts={})
87
+ (opts[:if_nil] || opts[:ifNil] || ->{nil}).call
88
+ end
89
+ alias ifNotNil if_not_nil
90
+ end
91
+
92
+ class Object
93
+ def if_nil(true_proc, opts={})
94
+ (opts[:if_not_nil] || opts[:ifNotNil] || ->{nil}).call
95
+ end
96
+ alias ifNil if_nil
97
+
98
+ def if_not_nil(true_proc, opts={})
99
+ true_proc.call
100
+ end
101
+ alias ifNotNil if_not_nil
102
+ end
103
+
104
+ class Proc
105
+ def while_true(block)
106
+ if self.call
107
+ block.call
108
+ while_true block
109
+ end
110
+ end
111
+ alias whileTrue while_true
112
+
113
+ alias value call
114
+ end
@@ -0,0 +1,3 @@
1
+ module Smalltalkable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "smalltalkable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "smalltalkable"
7
+ s.version = Smalltalkable::VERSION
8
+ s.authors = ["Ando Yasushi"]
9
+ s.email = ["andyjpn@gmail.com"]
10
+ s.homepage = "https://github.com/technohippy/smalltalkable"
11
+ s.summary = %q{Write your ruby code in smalltalkish way}
12
+ s.description = %q{Smalltalkable is a library to write your ruby code in smalltalkish way.}
13
+
14
+ s.rubyforge_project = "smalltalkable"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,78 @@
1
+ require 'test/unit'
2
+ require 'smalltalkable'
3
+
4
+ class TestSmalltalkable < Test::Unit::TestCase
5
+ def test_define_class
6
+ assert_raise(NameError) do Counter end
7
+ Object.subclass :Counter,
8
+ instanceVariableNames: 'counterValue',
9
+ classVariableNames: 'instanceCount',
10
+ poolDictionaries: '',
11
+ category: 'Category-Name'
12
+ assert_nothing_raised do Counter end
13
+ assert_equal Class, Counter.class
14
+ assert_equal [:@counterValue, :@category], Counter.instance_variables
15
+ assert_equal [:@@instanceCount], Counter.class_variables
16
+ assert_equal 'Category-Name', Counter.category
17
+ end
18
+
19
+ def test_define_method
20
+ Counter.compile '
21
+ initialize value
22
+ @counterValue = value'
23
+ Counter.compile '
24
+ counterValue
25
+ @counterValue'
26
+ Counter.compile '
27
+ setCounterValue newValue
28
+ @counterValue = newValue'
29
+ Counter.compile '
30
+ method a1, arg2:a2, arg3:a3
31
+ [a1, a2, a3]'
32
+
33
+ counter = Counter.new 0
34
+ assert_equal 0, counter.counterValue
35
+ counter.setCounterValue 1
36
+ assert_equal 1, counter.counterValue
37
+ assert_equal [1, 2, 3], counter.method(1, arg2:2, arg3:3)
38
+ assert_equal [1, 2, 3], counter.method(1, arg3:3, arg2:2)
39
+ assert_equal [1, 2, nil], counter.method(1, arg2:2)
40
+ assert_equal [1, nil, 3], counter.method(1, arg3:3)
41
+ end
42
+
43
+ def test_define_class_method
44
+ Counter.class.compile '
45
+ classMethod
46
+ "classMethod"'
47
+ assert_equal 'classMethod', Counter.classMethod
48
+ end
49
+
50
+ def test_if
51
+ assert_equal :true, (1 == 1).if_true(->{:true}, if_false:->{:false})
52
+ assert_equal :false, (0 == 1).if_true(->{:true}, if_false:->{:false})
53
+ assert_equal :true, (1 == 1).if_true(->{:true})
54
+ assert_nil (0 == 1).if_true(->{:true})
55
+
56
+ assert_equal :true, (1 == 1).if_false(->{:false}, if_true:->{:true})
57
+ assert_equal :false, (0 == 1).if_false(->{:false}, if_true:->{:true})
58
+ assert_nil (1 == 1).if_false(->{:false})
59
+ assert_equal :false, (0 == 1).if_false(->{:false})
60
+ end
61
+
62
+ def test_if_nil
63
+ assert_equal :true, nil.if_nil(->{:true}, if_not_nil:->{:false})
64
+ assert_equal :false, 1.if_nil(->{:true}, if_not_nil:->{:false})
65
+ assert_equal :true, nil.if_nil(->{:true})
66
+ assert_nil 1.if_nil(->{:true})
67
+ end
68
+
69
+ def test_while
70
+ ret = ''
71
+ c = 9
72
+ ->{0 < c}.while_true ->{
73
+ ret += c.to_s
74
+ c -= 1
75
+ }
76
+ assert_equal '987654321', ret
77
+ end
78
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smalltalkable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ando Yasushi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Smalltalkable is a library to write your ruby code in smalltalkish way.
15
+ email:
16
+ - andyjpn@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.rdoc
24
+ - Rakefile
25
+ - lib/smalltalkable.rb
26
+ - lib/smalltalkable/version.rb
27
+ - smalltalkable.gemspec
28
+ - test/test_smalltalkable.rb
29
+ homepage: https://github.com/technohippy/smalltalkable
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: smalltalkable
49
+ rubygems_version: 1.8.10
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Write your ruby code in smalltalkish way
53
+ test_files:
54
+ - test/test_smalltalkable.rb