motion-define-method 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +18 -0
- data/README.md +39 -0
- data/Rakefile +19 -0
- data/app/app_delegate.rb +5 -0
- data/lib/define-method/module_ext.rb +29 -0
- data/lib/define-method/version.rb +5 -0
- data/lib/motion-define-method.rb +9 -0
- data/motion-define-method.gemspec +19 -0
- data/spec/main_spec.rb +38 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# motion-define-method
|
2
|
+
|
3
|
+
RubyMotion static compilation do not allow define method in runtime. This
|
4
|
+
gem is a hack to reimplement define_method in runtime using method_missing.
|
5
|
+
|
6
|
+
## Setup
|
7
|
+
|
8
|
+
Install the gem:
|
9
|
+
|
10
|
+
```
|
11
|
+
gem install motion-define-method
|
12
|
+
```
|
13
|
+
|
14
|
+
Require the gem in Rakefile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'rubygems'
|
18
|
+
require 'motion-define-method'
|
19
|
+
|
20
|
+
Motion::Project::App.setup do |app|
|
21
|
+
app.name = 'MyApp'
|
22
|
+
end
|
23
|
+
```
|
24
|
+
|
25
|
+
Now you can use define_method in your code! Just note that method_missing is
|
26
|
+
much slower than real method and this should really not be used in production
|
27
|
+
code. Good luck!
|
28
|
+
|
29
|
+
## License
|
30
|
+
|
31
|
+
```
|
32
|
+
Copyright (c) 2012, Francis Chong <francis@ignition.hk>
|
33
|
+
|
34
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
35
|
+
|
36
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
39
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
$:.unshift("/Library/RubyMotion/lib")
|
5
|
+
require 'motion/project'
|
6
|
+
require 'bundler'
|
7
|
+
Bundler.require :default, :development
|
8
|
+
|
9
|
+
Motion::Project::App.setup do |app|
|
10
|
+
app.name = 'define-method'
|
11
|
+
app.files += ['lib/define-method/module_ext.rb']
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Build the gem"
|
15
|
+
task :gem do
|
16
|
+
sh "bundle exec gem build motion-define-method.gemspec"
|
17
|
+
sh "mkdir -p pkg"
|
18
|
+
sh "mv *.gem pkg/"
|
19
|
+
end
|
data/app/app_delegate.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
class Module
|
2
|
+
def define_method(name, *args, &blk)
|
3
|
+
@define_method_blocks ||= {}
|
4
|
+
|
5
|
+
if args.count > 0
|
6
|
+
@define_method_blocks[name] = args[0]
|
7
|
+
elsif
|
8
|
+
@define_method_blocks[name] = lambda(&blk)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Object
|
14
|
+
attr_accessor :define_method_blocks
|
15
|
+
alias_method :original_module_method_missing, :method_missing
|
16
|
+
def method_missing(method, *args, &block)
|
17
|
+
self.class.define_method_blocks ||= {}
|
18
|
+
if self.class.define_method_blocks.include?(method)
|
19
|
+
return self.class.define_method_blocks[method].call(*args)
|
20
|
+
else
|
21
|
+
self.class.ancestors.each do |clazz|
|
22
|
+
if clazz.define_method_blocks.include?(method)
|
23
|
+
return clazz.define_method_blocks[method].call(*args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
return original_module_method_missing(method, *args)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
unless defined?(Motion::Project::Config)
|
2
|
+
raise "This file must be required within a RubyMotion project Rakefile."
|
3
|
+
end
|
4
|
+
|
5
|
+
Motion::Project::App.setup do |app|
|
6
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'define-method/*.rb')).each do |file|
|
7
|
+
app.files.unshift file
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/define-method/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Francis Chong"]
|
6
|
+
gem.email = ["francis@ignition.hk"]
|
7
|
+
gem.description = "A hack to use define_method in RubyMotion."
|
8
|
+
gem.summary = "A hack to use define_method in RubyMotion."
|
9
|
+
gem.homepage = "https://github.com/siuying/motion-define-method"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|lib_spec|features)/})
|
13
|
+
gem.name = "motion-define-method"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = Motion::DefineMethod::VERSION
|
16
|
+
|
17
|
+
gem.add_development_dependency 'motion-redgreen'
|
18
|
+
gem.add_development_dependency 'rake'
|
19
|
+
end
|
data/spec/main_spec.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
describe "define-method" do
|
2
|
+
describe "#define_method(symbol, &block)" do
|
3
|
+
class Hello
|
4
|
+
[:foo, :bar].each do |name|
|
5
|
+
define_method(name) do |*args|
|
6
|
+
"#{name} world"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class World < Hello
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should implement simple define_method" do
|
15
|
+
Hello.new.foo.should == "foo world"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should allow define_method of superclass work on subclasses" do
|
19
|
+
World.new.bar.should == "bar world"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#define_method(symbol, method)" do
|
24
|
+
class Foo
|
25
|
+
define_method(:wilma) do |*args|
|
26
|
+
"#{name} world"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Bar < Foo
|
31
|
+
define_method(:barney, Proc.new{|a| "foo world" })
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should implement define_method using another method" do
|
35
|
+
Bar.new.barney.should == "foo world"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: motion-define-method
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Francis Chong
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: motion-redgreen
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A hack to use define_method in RubyMotion.
|
47
|
+
email:
|
48
|
+
- francis@ignition.hk
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- app/app_delegate.rb
|
59
|
+
- lib/define-method/module_ext.rb
|
60
|
+
- lib/define-method/version.rb
|
61
|
+
- lib/motion-define-method.rb
|
62
|
+
- motion-define-method.gemspec
|
63
|
+
- spec/main_spec.rb
|
64
|
+
homepage: https://github.com/siuying/motion-define-method
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
hash: 2261750205190609431
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: 2261750205190609431
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 1.8.24
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: A hack to use define_method in RubyMotion.
|
94
|
+
test_files:
|
95
|
+
- spec/main_spec.rb
|