function 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.
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Jason Kenney
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # Function
2
+
3
+ Functions is a small Ruby library that allows you to create Function objects. These differ
4
+ from Proc objects in a few key places.
5
+
6
+ ## Scope
7
+ The scope of the block is the class instance, not the surrounding scope where it was
8
+ declared.
9
+
10
+ ```ruby
11
+ p = Proc.new {
12
+ self.class
13
+ }
14
+
15
+ p.call # <-- Will return Object
16
+
17
+ f = Function.new {
18
+ self.class
19
+ }
20
+
21
+ f.call # <-- Will return Function
22
+
23
+ ```
24
+
25
+ ## Attributes
26
+ You can add attributes to the function similar to Python.
27
+
28
+ ```ruby
29
+
30
+ f = Function.new {
31
+ self.x + self.y
32
+ }
33
+
34
+ f.x = 3
35
+ f.y = 4
36
+
37
+ f.call # <-- returns 7
38
+
39
+ ```
40
+
41
+ ## Contributing
42
+ Fork the project, create an issue and make a pull request.
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Function'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,40 @@
1
+
2
+ #
3
+ # The Functon module provides python-like functions
4
+ # to ruby.
5
+ #
6
+
7
+ class Function
8
+
9
+ # Supposed to quack like Proc here.
10
+ # However, note how it uses instance_exec, which
11
+ # sets the scope to the class instance.
12
+ def call(*args)
13
+ self.instance_exec(*args, &@block)
14
+ end
15
+
16
+ # Like Proc, you pass a block to new.
17
+ def initialize(&block)
18
+ @block = block
19
+ super()
20
+ end
21
+
22
+ # This allows one to set new attributes so long as you
23
+ # do so using '='.
24
+ def method_missing(meth, *args, &block)
25
+ if meth.to_s =~ /^.+=$/
26
+ self.class.send(:attr_accessor,meth.to_s.gsub(/=/,'').to_sym)
27
+ return self.send(meth, *args, &block)
28
+ else
29
+ super
30
+ end
31
+ end
32
+
33
+ # Modified to agree with method_missing.
34
+ def respond_to?(meth, include_private = false)
35
+ meth.to_s =~ /^.+=$/ || super
36
+ end
37
+
38
+ alias_method :[], :call
39
+
40
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'function'
3
+
4
+ describe Function do
5
+ it "should run a block using itself as scope" do
6
+ f = Function.new do
7
+ self.class
8
+ end
9
+
10
+ f.call.should equal(Function)
11
+ end
12
+
13
+ it "should allow adding attributes" do
14
+ f = Function.new { return }
15
+
16
+ f.x = 2
17
+
18
+ f.x.should equal(2)
19
+ end
20
+
21
+ it "should respond_to anything ending with an =" do
22
+ f = Function.new { return }
23
+
24
+ f.should be_respond_to(:"x=" )
25
+ end
26
+
27
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: function
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Kenney
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
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
+ description: Python-like functions.
31
+ email:
32
+ - bloodycelt@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - MIT-LICENSE
38
+ - Rakefile
39
+ - README.md
40
+ - lib/function.rb
41
+ - spec/function_spec.rb
42
+ - spec/spec_helper.rb
43
+ homepage: https://github.com/bloodycelt
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.24
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Function Object that executes its block in its instance context, inspired
67
+ by Python.
68
+ test_files:
69
+ - spec/function_spec.rb
70
+ - spec/spec_helper.rb