smalltalkable 0.0.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Smalltalkable is a library to write your ruby code in smalltalkish way.
7
+ Smalltalkable allows you to write ruby code in smalltalkish way.
8
8
 
9
9
  == INSTALL:
10
10
 
@@ -12,7 +12,9 @@ Smalltalkable is a library to write your ruby code in smalltalkish way.
12
12
 
13
13
  == EXAMPLE:
14
14
 
15
- === Code:
15
+ === Define and Use a Class
16
+
17
+ ==== Code:
16
18
 
17
19
  require 'smalltalkable'
18
20
 
@@ -44,7 +46,7 @@ Smalltalkable is a library to write your ruby code in smalltalkish way.
44
46
  counter.next
45
47
  }
46
48
 
47
- === Result:
49
+ ==== Result:
48
50
 
49
51
  $ ruby -I lib/ sample.rb
50
52
  0
@@ -52,11 +54,28 @@ Smalltalkable is a library to write your ruby code in smalltalkish way.
52
54
  12
53
55
  18
54
56
 
57
+ === Smalltalkize Method
58
+
59
+ To define smalltalkish methods in rubyish way, use smalltalkize method:
60
+
61
+ class Counter
62
+ def method(arg1, arg2)
63
+ do_something
64
+ end
65
+ smalltalkize :method, :arg2
66
+ # Usage: Counter.new.method 'arg1', arg2:'arg2'
67
+ end
68
+
69
+ This can be used to smalltalkize an existing method:
70
+
71
+ String.smalltalkize :center => [:centerWidth, :padding]
72
+ # Usage: 'foo'.centerWidth 10, padding:'*'
73
+
55
74
  == LICENSE:
56
75
 
57
76
  (The MIT License)
58
77
 
59
- Copyright (c) 2012 ANDO Yasushi
78
+ Copyright (c) 2013 ANDO Yasushi
60
79
 
61
80
  Permission is hereby granted, free of charge, to any person obtaining
62
81
  a copy of this software and associated documentation files (the
@@ -1,3 +1,3 @@
1
1
  module Smalltalkable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/smalltalkable.rb CHANGED
@@ -53,28 +53,53 @@ class Class
53
53
  end
54
54
  end
55
55
 
56
- class TrueClass
57
- def if_true(true_proc, opts={})
58
- true_proc.call
59
- end
60
- alias ifTrue if_true
56
+ class Object
57
+ class <<self
58
+ def smalltalkize(method_name, *args)
59
+ added_method_name = method_name
60
+ if method_name.is_a? Hash
61
+ args = method_name.values.first
62
+ added_method_name = args.shift
63
+ method_name = method_name.keys.first
64
+ end
61
65
 
62
- def if_false(false_proc, opts={})
63
- (opts[:if_true] || opts[:ifTrue] || ->{nil}).call
66
+ method = self.instance_method method_name
67
+ return if [0, 1].include? method.arity
68
+
69
+ old_method_name = "#{method_name}__old"
70
+ new_method_name = "#{method_name}__new"
71
+ self.instance_eval do
72
+ alias_method old_method_name, method_name
73
+ end
74
+ self.class_eval <<-EOS
75
+ def #{new_method_name}(arg, opts={})
76
+ args = [arg]
77
+ #{args.map{|arg| "args.push opts[:#{arg}]"}.join("\n")}
78
+ #{old_method_name}(*args)
79
+ end
80
+ EOS
81
+ self.instance_eval do
82
+ alias_method added_method_name, new_method_name
83
+ end
84
+ end
85
+ alias smalltalkise smalltalkize
64
86
  end
65
- alias ifFalse if_false
66
- end
67
87
 
68
- class FalseClass
69
- def if_true(true_proc, opts={})
70
- (opts[:if_false] || opts[:ifFalse] || ->{nil}).call
88
+ def eigenclass
89
+ class << self
90
+ self
91
+ end
92
+ end
93
+
94
+ def if_nil(true_proc, opts={})
95
+ (opts[:if_not_nil] || opts[:ifNotNil] || ->{nil}).call
71
96
  end
72
- alias ifTrue if_true
97
+ alias ifNil if_nil
73
98
 
74
- def if_false(false_proc, opts={})
75
- false_proc.call
99
+ def if_not_nil(true_proc, opts={})
100
+ true_proc.call
76
101
  end
77
- alias ifFalse if_false
102
+ alias ifNotNil if_not_nil
78
103
  end
79
104
 
80
105
  class NilClass
@@ -89,16 +114,28 @@ class NilClass
89
114
  alias ifNotNil if_not_nil
90
115
  end
91
116
 
92
- class Object
93
- def if_nil(true_proc, opts={})
94
- (opts[:if_not_nil] || opts[:ifNotNil] || ->{nil}).call
117
+ class TrueClass
118
+ def if_true(true_proc, opts={})
119
+ true_proc.call
95
120
  end
96
- alias ifNil if_nil
121
+ alias ifTrue if_true
97
122
 
98
- def if_not_nil(true_proc, opts={})
99
- true_proc.call
123
+ def if_false(false_proc, opts={})
124
+ (opts[:if_true] || opts[:ifTrue] || ->{nil}).call
100
125
  end
101
- alias ifNotNil if_not_nil
126
+ alias ifFalse if_false
127
+ end
128
+
129
+ class FalseClass
130
+ def if_true(true_proc, opts={})
131
+ (opts[:if_false] || opts[:ifFalse] || ->{nil}).call
132
+ end
133
+ alias ifTrue if_true
134
+
135
+ def if_false(false_proc, opts={})
136
+ false_proc.call
137
+ end
138
+ alias ifFalse if_false
102
139
  end
103
140
 
104
141
  class Proc
@@ -47,6 +47,61 @@ class TestSmalltalkable < Test::Unit::TestCase
47
47
  assert_equal 'classMethod', Counter.classMethod
48
48
  end
49
49
 
50
+ def test_smalltalkize
51
+ eval <<-EOS
52
+ class ::Counter
53
+ def method0
54
+ []
55
+ end
56
+ smalltalkize :method0
57
+
58
+ def method1(arg1)
59
+ [arg1]
60
+ end
61
+ smalltalkize :method1
62
+
63
+ def method2(arg1, arg2)
64
+ [arg1, arg2]
65
+ end
66
+ smalltalkize :method2, :arg2
67
+
68
+ def method3(arg1, arg2, arg3)
69
+ [arg1, arg2, arg3]
70
+ end
71
+ smalltalkize :method3, :arg2, :arg3
72
+ end
73
+ EOS
74
+ counter = Counter.new 0
75
+ assert_equal [], counter.method0
76
+ assert_equal [1], counter.method1(1)
77
+ assert_equal [1, 2], counter.method2(1, arg2:2)
78
+ assert_equal [1, 2, 3], counter.method3(1, arg2:2, arg3:3)
79
+
80
+ String.smalltalkize :center, :padding
81
+ assert_equal '***foo****', 'foo'.center(10, padding:'*')
82
+
83
+ String.smalltalkize :rjust => [:rjustWidth, :padding]
84
+ assert_equal '*******foo', 'foo'.rjustWidth(10, padding:'*')
85
+
86
+ Time.eigenclass.smalltalkize :gm, :month, :day, :hour, :minute, :second
87
+ time = Time.gm 2013, month:1, day:2, hour:3, minute:4, second:5
88
+ assert_equal 2013, time.year
89
+ assert_equal 1, time.month
90
+ assert_equal 2, time.day
91
+ assert_equal 3, time.hour
92
+ assert_equal 4, time.min
93
+ assert_equal 5, time.sec
94
+
95
+ Time.eigenclass.smalltalkize :local => [:local_year, :month, :day, :hour, :minute, :second]
96
+ local_time = Time.local_year 2013, month:1, day:2, hour:3, minute:4, second:5
97
+ assert_equal 2013, local_time.year
98
+ assert_equal 1, local_time.month
99
+ assert_equal 2, local_time.day
100
+ assert_equal 3, local_time.hour
101
+ assert_equal 4, local_time.min
102
+ assert_equal 5, local_time.sec
103
+ end
104
+
50
105
  def test_if
51
106
  assert_equal :true, (1 == 1).if_true(->{:true}, if_false:->{:false})
52
107
  assert_equal :false, (0 == 1).if_true(->{:true}, if_false:->{:false})
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smalltalkable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-17 00:00:00.000000000 Z
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Smalltalkable is a library to write your ruby code in smalltalkish way.
15
15
  email: