rns 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +6 -0
- data/.gitignore +49 -0
- data/.rvmrc +1 -0
- data/.travis.yml +12 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +41 -0
- data/Guardfile +13 -0
- data/LICENSE.txt +24 -0
- data/README.md +61 -0
- data/Rakefile +13 -0
- data/lib/rns/version.rb +3 -0
- data/lib/rns.rb +30 -0
- data/rns.gemspec +25 -0
- data/spec/rns/rns_spec.rb +15 -0
- data/spec/spec_helper.rb +7 -0
- metadata +126 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
*.gem
|
17
|
+
|
18
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
19
|
+
#
|
20
|
+
# * Create a file at ~/.gitignore
|
21
|
+
# * Include files you want ignored
|
22
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
23
|
+
#
|
24
|
+
# After doing this, these files will be ignored in all your git projects,
|
25
|
+
# saving you from having to 'pollute' every project you touch with them
|
26
|
+
#
|
27
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
28
|
+
#
|
29
|
+
# For MacOS:
|
30
|
+
#
|
31
|
+
.DS_Store
|
32
|
+
|
33
|
+
# For TextMate
|
34
|
+
#*.tmproj
|
35
|
+
#tmtags
|
36
|
+
|
37
|
+
# For emacs:
|
38
|
+
#*~
|
39
|
+
#\#*
|
40
|
+
#.\#*
|
41
|
+
|
42
|
+
# For vim:
|
43
|
+
#*.swp
|
44
|
+
|
45
|
+
# For redcar:
|
46
|
+
#.redcar
|
47
|
+
|
48
|
+
# For rubinius:
|
49
|
+
#*.rbc
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.3@rns
|
data/.travis.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
|
5
|
+
# Only build master branch
|
6
|
+
branches:
|
7
|
+
only:
|
8
|
+
- master
|
9
|
+
|
10
|
+
# To stop Travis from running tests for a new commit,
|
11
|
+
# add the following to your commit message: [ci skip]
|
12
|
+
# You should add this when you edit documentation or comments, etc.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rns (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
ffi (1.0.11)
|
11
|
+
guard (1.0.1)
|
12
|
+
ffi (>= 0.5.0)
|
13
|
+
thor (~> 0.14.6)
|
14
|
+
guard-rspec (0.5.8)
|
15
|
+
guard (>= 0.8.4)
|
16
|
+
multi_json (1.0.3)
|
17
|
+
rake (0.9.2.2)
|
18
|
+
rspec (2.7.0)
|
19
|
+
rspec-core (~> 2.7.0)
|
20
|
+
rspec-expectations (~> 2.7.0)
|
21
|
+
rspec-mocks (~> 2.7.0)
|
22
|
+
rspec-core (2.7.1)
|
23
|
+
rspec-expectations (2.7.0)
|
24
|
+
diff-lcs (~> 1.1.2)
|
25
|
+
rspec-mocks (2.7.0)
|
26
|
+
simplecov (0.5.4)
|
27
|
+
multi_json (~> 1.0.3)
|
28
|
+
simplecov-html (~> 0.5.3)
|
29
|
+
simplecov-html (0.5.3)
|
30
|
+
thor (0.14.6)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
guard
|
37
|
+
guard-rspec
|
38
|
+
rake
|
39
|
+
rns!
|
40
|
+
rspec
|
41
|
+
simplecov
|
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
if RbConfig::CONFIG['host_os'] =~ /linux/
|
2
|
+
notification :notifysend
|
3
|
+
end
|
4
|
+
|
5
|
+
guard 'rspec',
|
6
|
+
version: 2,
|
7
|
+
cli: '--format documentation',
|
8
|
+
all_on_start: false,
|
9
|
+
all_after_pass: false do
|
10
|
+
watch(%r{^spec/.+_spec\.rb$})
|
11
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
12
|
+
watch('spec/spec_helper.rb') { "spec" }
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
rns is distributed under the following terms:
|
2
|
+
|
3
|
+
Copyright (c) Alan Dipert <alan@dipert.org>. All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions
|
7
|
+
are met:
|
8
|
+
1. Redistributions of source code must retain the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
10
|
+
2. Redistributions in binary form must reproduce the above copyright
|
11
|
+
notice, this list of conditions and the following disclaimer in the
|
12
|
+
documentation and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
15
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
17
|
+
ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
18
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
20
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
21
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
22
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
23
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
24
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
`rns`, which stands for "Ruby namespaces", is a small library for
|
2
|
+
using classes and modules as packages of functions in order to support
|
3
|
+
functional programming in Ruby. It is inspired by
|
4
|
+
[Clojure's](http://clojure.org) `ns` macro and namespace system.
|
5
|
+
|
6
|
+
[![Build Status](https://secure.travis-ci.org/alandipert/rns.png)](http://travis-ci.org/alandipert/rns)
|
7
|
+
|
8
|
+
# Usage
|
9
|
+
|
10
|
+
## Importing Methods into Classes
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
require 'rns'
|
14
|
+
|
15
|
+
module Arithmetic
|
16
|
+
def self.dec(n) n - 1; end
|
17
|
+
def self.inc(n) n + 1; end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Statistics
|
21
|
+
def self.avg(arr); arr.reduce(:+) / arr.count; end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Main < Rns
|
25
|
+
def use
|
26
|
+
{Math => [:inc],
|
27
|
+
Statistics => [:avg]}
|
28
|
+
end
|
29
|
+
|
30
|
+
def main
|
31
|
+
puts "1+1 is #{inc 1} and the average of [1,2,3] is #{avg [1,2,3]}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Main.new.main
|
36
|
+
```
|
37
|
+
|
38
|
+
## Importing Methods into Blocks
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
Rns::using [Math, [:inc], Statistics, [:avg]] do
|
42
|
+
puts avg((1..10).to_a.map(&method(:inc)))
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Please see the
|
47
|
+
[tests](https://github.com/alandipert/rns/tree/master/spec/rns) for more
|
48
|
+
usage examples.
|
49
|
+
|
50
|
+
# Rationale
|
51
|
+
|
52
|
+
Ruby has good functional programming support, but the class and module
|
53
|
+
system doesn't lend itself to organizing and accessing functions.
|
54
|
+
With `rns` I hope to make it at least slightly easier to build Ruby
|
55
|
+
programs primarily out of pure functions.
|
56
|
+
|
57
|
+
# Thanks
|
58
|
+
|
59
|
+
To [Sam Umbach](https://twitter.com/samumbach) for helping me tame the
|
60
|
+
eigenclass, and to my employer [Relevance](http://thinkrelevance.com)
|
61
|
+
for indulging me with time to work on free software.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require "rspec/core/rake_task"
|
4
|
+
require 'rake'
|
5
|
+
|
6
|
+
desc "Run all specs"
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
8
|
+
t.pattern = "spec/**/*_spec.rb"
|
9
|
+
t.ruby_opts = '-Ilib -Ispec -I.'
|
10
|
+
t.rspec_opts = '--color'
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :spec
|
data/lib/rns/version.rb
ADDED
data/lib/rns.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
class Rns
|
2
|
+
def self.to_pairs(use_spec)
|
3
|
+
case use_spec
|
4
|
+
when Array then use_spec.each_slice(2).reduce([]){|xs,y| xs + [y]}
|
5
|
+
when Hash then use_spec.to_a
|
6
|
+
else
|
7
|
+
raise "Don't know how to make module/class:methods pairs out of #{use_spec.class}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.populate(obj, use_spec)
|
12
|
+
obj.tap do |o|
|
13
|
+
to_pairs(use_spec).each do |pkg, method_names|
|
14
|
+
method_names.each do |name|
|
15
|
+
o.class.send(:define_method, name) do |*args|
|
16
|
+
pkg.method(name).call(*args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize
|
24
|
+
Rns::populate(self, use)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.using(use_spec, &blk)
|
28
|
+
blk[Rns::populate(Object.new, use_spec)]
|
29
|
+
end
|
30
|
+
end
|
data/rns.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rns/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rns"
|
7
|
+
s.version = Rns::VERSION
|
8
|
+
s.authors = ["Alan Dipert"]
|
9
|
+
s.email = ["alan@dipert.org"]
|
10
|
+
s.homepage = "http://github.com/alandipert/rns"
|
11
|
+
s.summary = %q{A library for namespacing pure functions.}
|
12
|
+
s.license = "BSD-new"
|
13
|
+
s.description = %q{rns is a namespace management library designed to ease functional programming in Ruby.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
# specify any dependencies here; for example:
|
21
|
+
s.add_development_dependency 'rspec'
|
22
|
+
s.add_development_dependency 'guard'
|
23
|
+
s.add_development_dependency 'guard-rspec'
|
24
|
+
s.add_development_dependency 'simplecov'
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), *%w[.. spec_helper.rb])
|
2
|
+
require 'rns'
|
3
|
+
|
4
|
+
describe Rns do
|
5
|
+
context 'adding methods to classes' do
|
6
|
+
it "works" do
|
7
|
+
(1 + 1).should == 2
|
8
|
+
end
|
9
|
+
end
|
10
|
+
context 'adding methods to blocks' do
|
11
|
+
it "works" do
|
12
|
+
(1 + 1).should == 2
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alan Dipert
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-05 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
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: guard
|
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
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: guard-rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: simplecov
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: rns is a namespace management library designed to ease functional programming
|
79
|
+
in Ruby.
|
80
|
+
email:
|
81
|
+
- alan@dipert.org
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- .document
|
87
|
+
- .gitignore
|
88
|
+
- .rvmrc
|
89
|
+
- .travis.yml
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- Guardfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/rns.rb
|
97
|
+
- lib/rns/version.rb
|
98
|
+
- rns.gemspec
|
99
|
+
- spec/rns/rns_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: http://github.com/alandipert/rns
|
102
|
+
licenses:
|
103
|
+
- BSD-new
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.8.24
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: A library for namespacing pure functions.
|
126
|
+
test_files: []
|