function_object 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.md +21 -0
- data/README.md +36 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +8 -0
- data/function_object.gemspec +22 -0
- data/lib/function_object.rb +15 -0
- data/lib/function_object/arguments_builder.rb +26 -0
- data/lib/function_object/macro.rb +74 -0
- data/lib/function_object/version.rb +3 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b1d6bf4801789450f0e6f087a36c2249ebf83fdc
|
4
|
+
data.tar.gz: 71f0068301a49208ec85681f742b967ef93e39bb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8b95c4fce6542a402590e1fc8229433953b2f52cfc12682818e000bdb20eb79a5106453a3827b63a24ddb6d654569a01a8fb013cc40ead4c18bf0b5dc285caa1
|
7
|
+
data.tar.gz: 050ffacdf5bd34ce82015f30ca20efa184afb66790901c88134c8db98fbfdafb439012e4608a45785e3007f39a9b5f320f3e41b0cfa9eaea6acecaafa69f9984
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Vladimir Kochnev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# FunctionObject
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/function_object`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'function_object'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install function_object
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marshall-lee/function_object.
|
36
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'function_object/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'function_object'
|
8
|
+
spec.version = FunctionObject::VERSION
|
9
|
+
spec.authors = ['Vladimir Kochnev']
|
10
|
+
spec.email = ['hashtable@yandex.ru']
|
11
|
+
|
12
|
+
spec.summary = 'Just nice callable objects, nothing else'
|
13
|
+
spec.homepage = 'https://github.com/marshall-lee/function_object'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec)/}) }
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
19
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
20
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
21
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
22
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class FunctionObject
|
2
|
+
require 'function_object/version'
|
3
|
+
require 'function_object/arguments_builder'
|
4
|
+
require 'function_object/macro'
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def arguments(&block)
|
8
|
+
arg_descs = ArgumentsBuilder.new.build(&block)
|
9
|
+
macro = Macro.new(arg_descs)
|
10
|
+
self.class_eval { include macro.class_mixin }
|
11
|
+
singleton_class.class_eval { include macro.sclass_mixin }
|
12
|
+
end
|
13
|
+
alias_method :args, :arguments
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class FunctionObject::ArgumentsBuilder
|
2
|
+
Descriptor = Struct.new(:name, :default, :options) do
|
3
|
+
def initialize(*)
|
4
|
+
super
|
5
|
+
if default? && !self.default.respond_to?(:call)
|
6
|
+
fail ArgumentError, "#{self.default.inspect} is expected to be callable"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def default?
|
11
|
+
options.key? :default
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def build(&block)
|
16
|
+
@descriptors = []
|
17
|
+
instance_exec(&block)
|
18
|
+
@descriptors
|
19
|
+
end
|
20
|
+
|
21
|
+
def argument(name, options = {})
|
22
|
+
default = options[:default]
|
23
|
+
@descriptors << Descriptor.new(name, default, options)
|
24
|
+
end
|
25
|
+
alias_method :arg, :argument
|
26
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
class FunctionObject::Macro
|
2
|
+
def initialize(arg_descs)
|
3
|
+
@arg_descs = arg_descs
|
4
|
+
@has_defaults = arg_descs.any?(&:default?)
|
5
|
+
|
6
|
+
@arg_descs_without_defaults = arg_descs.take_while { |d| !d.default? }
|
7
|
+
@arg_descs_with_defaults = arg_descs[arg_descs_without_defaults.length .. -1]
|
8
|
+
end
|
9
|
+
|
10
|
+
def class_mixin
|
11
|
+
Module.new.tap do |mixin|
|
12
|
+
arg_descs_with_defaults.each do |desc|
|
13
|
+
mixin.module_eval do
|
14
|
+
private define_method("_default_#{desc.name}", &desc.default)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
arg_descs.each do |desc|
|
19
|
+
mixin.module_eval do
|
20
|
+
attr_reader desc.name
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
mixin.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
25
|
+
def initialize(#{arg_list_stmt})
|
26
|
+
#{ivar_assign_stmt}
|
27
|
+
end
|
28
|
+
RUBY
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def sclass_mixin
|
33
|
+
Module.new.tap do |mixin|
|
34
|
+
mixin.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
35
|
+
def call(#{arg_list_stmt})
|
36
|
+
new(#{arg_list_stmt}).call
|
37
|
+
end
|
38
|
+
RUBY
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def arg_list_stmt
|
45
|
+
stmts = arg_descs_without_defaults.map(&:name)
|
46
|
+
stmts << '*_args' if defaults?
|
47
|
+
stmts.join(',')
|
48
|
+
end
|
49
|
+
|
50
|
+
def ivar_assign_stmt
|
51
|
+
stmts = []
|
52
|
+
arg_descs_without_defaults.each do |desc|
|
53
|
+
stmts << "@#{desc.name} = #{desc.name}"
|
54
|
+
end
|
55
|
+
arg_descs_with_defaults.each_with_index do |desc, i|
|
56
|
+
stmts << <<-RUBY
|
57
|
+
@#{desc.name} = if #{i} < _args.length
|
58
|
+
_args[#{i}]
|
59
|
+
else
|
60
|
+
_default_#{desc.name}
|
61
|
+
end
|
62
|
+
RUBY
|
63
|
+
end
|
64
|
+
stmts.join($/)
|
65
|
+
end
|
66
|
+
|
67
|
+
attr_reader :arg_descs,
|
68
|
+
:arg_descs_with_defaults,
|
69
|
+
:arg_descs_without_defaults
|
70
|
+
|
71
|
+
def defaults?
|
72
|
+
@has_defaults
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: function_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vladimir Kochnev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.10'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- hashtable@yandex.ru
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.md
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- function_object.gemspec
|
86
|
+
- lib/function_object.rb
|
87
|
+
- lib/function_object/arguments_builder.rb
|
88
|
+
- lib/function_object/macro.rb
|
89
|
+
- lib/function_object/version.rb
|
90
|
+
homepage: https://github.com/marshall-lee/function_object
|
91
|
+
licenses: []
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.5.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Just nice callable objects, nothing else
|
113
|
+
test_files: []
|