iain-not 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = Changelog Not
2
+
3
+ == 0.0.1
4
+
5
+ * Initial version
6
+ * RSpec
7
+ * Usable as Rails plugin
data/README.rdoc ADDED
@@ -0,0 +1,26 @@
1
+ = Not
2
+
3
+
4
+ This little gem/plugin adds a little syntactic sugar to your code. You can append not
5
+ in the middle of your code to improve readability.
6
+
7
+ = Examples
8
+
9
+ @foo.not.nil?
10
+ @user.not.active?
11
+ "something".not.tainted?
12
+ User.not.new(:name => 'iain') # not sure why you'd want to do this though
13
+ @foo.not.not.not.not.send(:not).not.nil? # sorry, couldn't resist
14
+
15
+ = Installation
16
+
17
+ To install as a gem:
18
+
19
+ gem sources add http://gems.github.com
20
+ gem install iain-not
21
+
22
+ To install as a Rails plugin:
23
+
24
+ ruby script/plugin install git://github.com/iain/not
25
+
26
+ Copyright (c) 2008 Iain Hecker, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+ require 'spec/rake/spectask'
5
+
6
+ desc 'Default: run specs'
7
+ task :default => :spec
8
+
9
+ desc 'Run the specs'
10
+ Spec::Rake::SpecTask.new(:spec) do |t|
11
+ t.spec_opts = ['--colour --format specdoc --loadby mtime --reverse']
12
+ t.spec_files = FileList['spec/**/*_spec.rb']
13
+ end
14
+
15
+ Echoe.new('not', '0.0.1') do |p|
16
+ p.description = "Syntactic sugar for negating any results: @foo.not.nil?"
17
+ p.url = "http://github.com/iain/not"
18
+ p.author = "Iain Hecker"
19
+ p.email = "iain@iain.nl"
20
+ p.ignore_pattern = [ "tmp/*", "script/*" ]
21
+ p.development_dependencies = []
22
+ end
23
+
24
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'not'
data/lib/not.rb ADDED
@@ -0,0 +1,50 @@
1
+ module Not
2
+
3
+ # Negates any command that comes after it.
4
+ #
5
+ # @foo.nil? # => true
6
+ # @foo.not.nil? # => false
7
+ def not
8
+ @_not_class ||= NotClass.new(self)
9
+ end
10
+
11
+ # NotClass is the proxy object to perform other commands from.
12
+ class NotClass
13
+
14
+ private
15
+
16
+ # The not method needs to pass self to work so the Notclass
17
+ def initialize(base)
18
+ @base = base
19
+
20
+ # Make the NotClass a proxy for whatever object it is called from
21
+ if self.class.respond_to?(:delegate_methods!)
22
+ self.class.delegate_methods!
23
+ end
24
+ end
25
+
26
+ # The not-class is a proxy, but with negation. Send any method to it's base object
27
+ def method_missing(method, *args, &block)
28
+ not @base.send(method, *args, &block)
29
+ end
30
+
31
+ # Also override any methods this method has so that you can use any method,
32
+ # including nil? and other basic methods.
33
+ def self.delegate_methods!
34
+ override = instance_methods.reject do |m|
35
+ m == 'method_missing' or m[0,1] == '_' or m == 'not'
36
+ end
37
+ override.each do |m|
38
+ class_eval(%Q{
39
+ def #{m}(*args, &block)
40
+ method_missing(:#{m}, *args, &block)
41
+ end
42
+ })
43
+ end
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ # Apply the magic
50
+ Object.send(:include, Not)
data/not.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{not}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Iain Hecker"]
9
+ s.date = %q{2008-12-24}
10
+ s.description = %q{Syntactic sugar for negating any results: @foo.not.nil?}
11
+ s.email = %q{iain@iain.nl}
12
+ s.extra_rdoc_files = ["lib/not.rb", "CHANGELOG.rdoc", "README.rdoc"]
13
+ s.files = ["Rakefile", "lib/not.rb", "init.rb", "CHANGELOG.rdoc", "spec/not_spec.rb", "README.rdoc", "Manifest", "not.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/iain/not}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Not", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{not}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{Syntactic sugar for negating any results: @foo.not.nil?}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
data/spec/not_spec.rb ADDED
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/../lib/not'
2
+
3
+ class Duck
4
+
5
+ def quack?
6
+ true
7
+ end
8
+
9
+ def bark?
10
+ nil
11
+ end
12
+
13
+ end
14
+
15
+ class NotSpec < Spec::ExampleGroup
16
+
17
+ describe "Using the not method" do
18
+
19
+ it "should negate nil?" do
20
+ @foo.should be_nil
21
+ @foo.not.nil?.should be_false
22
+ end
23
+
24
+ it "should chain" do
25
+ @foo.not.not.nil?.should be_true
26
+ end
27
+
28
+ describe "a duck" do
29
+
30
+ before do
31
+ @duck = Duck.new
32
+ end
33
+
34
+ it "should quack" do
35
+ @duck.quack?.should be_true
36
+ @duck.not.quack?.should be_false
37
+ end
38
+
39
+ it "should not bark" do
40
+ @duck.bark?.should be_nil
41
+ @duck.not.bark?.should be_true
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iain-not
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Iain Hecker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-24 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "Syntactic sugar for negating any results: @foo.not.nil?"
17
+ email: iain@iain.nl
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/not.rb
24
+ - CHANGELOG.rdoc
25
+ - README.rdoc
26
+ files:
27
+ - Rakefile
28
+ - lib/not.rb
29
+ - init.rb
30
+ - CHANGELOG.rdoc
31
+ - spec/not_spec.rb
32
+ - README.rdoc
33
+ - Manifest
34
+ - not.gemspec
35
+ has_rdoc: true
36
+ homepage: http://github.com/iain/not
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --line-numbers
40
+ - --inline-source
41
+ - --title
42
+ - Not
43
+ - --main
44
+ - README.rdoc
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "1.2"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: not
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: "Syntactic sugar for negating any results: @foo.not.nil?"
66
+ test_files: []
67
+