verstehen 0.1.0

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,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Corban Brook
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.
data/README ADDED
File without changes
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "verstehen"
8
+ gem.summary = %Q{Ruby List Comprehension}
9
+ gem.description = %Q{Ruby list comprehension all over your tee tahs}
10
+ gem.email = "corbanbrook@gmail.com"
11
+ gem.homepage = "http://github.com/corbanbrook/verstehen"
12
+ gem.authors = ["Corban Brook"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "verstehen #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,11 @@
1
+ require 'verstehen/list'
2
+
3
+ module Kernel
4
+ def list &block; Verstehen::List.new &block; end
5
+ end
6
+
7
+ module Verstehen
8
+ Dimension = Struct.new(:for, :in, :if)
9
+
10
+ class Context; end
11
+ end
@@ -0,0 +1,52 @@
1
+ module Verstehen
2
+ class List
3
+ def initialize &expression
4
+ @expression = expression
5
+ @result = []
6
+ @dimensions = []
7
+ @context = Context.new
8
+ end
9
+
10
+ def for *symbols
11
+ @dimensions << Dimension.new(symbols)
12
+ symbols.each { |sym| Context.send(:attr_accessor, sym) }
13
+ self
14
+ end
15
+
16
+ def in &range
17
+ @dimensions.last.in = range
18
+ self
19
+ end
20
+
21
+ def if &condition
22
+ @dimensions.last.if = condition
23
+ self
24
+ end
25
+
26
+ def comprehend
27
+ generated = ""
28
+ @dimensions.each_with_index do |dimension, i|
29
+ block_params = dimension.for.collect {|sym| sym.to_s}.join(", ")
30
+ generated << "for #{block_params} in @context.instance_eval(&@dimensions[#{i}].in)\n"
31
+ generated << dimension.for.map {|sym| "@context.#{sym} = #{sym}\n" }.join
32
+ if dimension == @dimensions.last
33
+ if @dimensions[i].if
34
+ generated << "if @context.instance_eval(&@dimensions[#{i}].if)\n"
35
+ end
36
+ generated << "@result << @context.instance_eval(&@expression)\n"
37
+ if @dimensions[i].if
38
+ generated << "end\n"
39
+ end
40
+ end
41
+ end
42
+ @dimensions.each_with_index do |dimension, i|
43
+ generated << "end\n"
44
+ end
45
+
46
+ #puts generated
47
+ eval generated
48
+
49
+ @result
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'verstehen'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,47 @@
1
+ require 'verstehen'
2
+ require 'test/unit'
3
+
4
+ class TestVerstehen < Test::Unit::TestCase
5
+ def test_simple
6
+ assert_equal(
7
+ [0, 1, 2, 3, 4],
8
+ list { x }.for(:x).in{ 0..4 }.comprehend)
9
+ end
10
+
11
+ def test_simple_expression
12
+ assert_equal(
13
+ [0, 5, 10, 15, 20],
14
+ list { x * 5 }.for(:x).in{ 0..4 }.comprehend)
15
+ end
16
+
17
+ def test_simple_condition
18
+ assert_equal(
19
+ [0, 64, 128, 192, 256],
20
+ list { x * 16 }.for(:x).in{ 0..16 }.if{ x % 4 == 0 }.comprehend)
21
+ end
22
+
23
+ def test_string
24
+ word = "spam"
25
+ assert_equal(
26
+ [["", "spam"], ["s", "pam"], ["sp", "am"], ["spa", "m"], ["spam", ""]],
27
+ list { [word[0, i], word[i, word.length]] }.for(:i).in{ 0..word.length }.comprehend)
28
+ end
29
+
30
+ def test_2d_range
31
+ assert_equal(
32
+ [1, 2, 2, 4],
33
+ list { x * y }.for(:x).in{ 0...3 }.for(:y).in{ 0...3 }.if{ x > 0 and y > 0 }.comprehend)
34
+ end
35
+
36
+ def test_3d_range
37
+ assert_equal(
38
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 2, 4, 0, 0, 0, 0, 2, 4, 0, 4, 8],
39
+ list { x * y * z }.for(:x).in{ 0...3 }.for(:y).in{ 0...3 }.for(:z).in{0...3}.comprehend)
40
+ end
41
+
42
+ def test_reference_outer
43
+ assert_equal(
44
+ [:a, :b, :c, :d, :e, :f, :g, :h, :i],
45
+ list { l2 }.for(:l1).in{ [[:a, :b, :c], [:d, :e, :f], [:g, :h, :i]] }.for(:l2).in{ l1 }.comprehend)
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestVerstehen < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: verstehen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Corban Brook
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-28 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Ruby list comprehension all over your tee tahs
26
+ email: corbanbrook@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/verstehen.rb
42
+ - lib/verstehen/list.rb
43
+ - test/helper.rb
44
+ - test/test.rb
45
+ - test/test_verstehen.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/corbanbrook/verstehen
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Ruby List Comprehension
74
+ test_files:
75
+ - test/helper.rb
76
+ - test/test.rb
77
+ - test/test_verstehen.rb