Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ruby 1.8.7 compatibility #43

Merged
merged 17 commits into from
Jan 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--order random
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
before_install:
- gem update --system 2.1.11
- gem --version
bundler_args: --without development
language: ruby
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.0
- jruby
- jruby-head
- rbx
- ruby-head
matrix:
allow_failures:
- rvm: jruby-head
- rvm: ruby-head
fast_finish: true
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
Features:

- New "pebble" mode (Guilherme Carvalho)

15 changes: 12 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ gem 'rake'

group :development do
gem 'guard'
gem 'guard-rspec'
gem 'guard-bundler'
gem 'guard-rspec'
gem 'pry'
gem 'pry-rescue'
end

group :test do
gem "libnotify"
gem 'coveralls', require: false
gem 'coveralls', :require => false
gem 'libnotify'
gem 'mime-types', '~> 1.25', :platforms => [:jruby, :ruby_18]
gem 'rspec', '~> 2.14'
end

platforms :rbx do
gem 'json'
gem 'rubinius-coverage', '~> 2.0'
gem 'rubysl', '~> 2.0'
end
10 changes: 5 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ null_io = NullIO.new

null_io << "foo" # => nil
null_io.readline # => nil
null_io.foobar # =>
# ~> -:11:in `<main>': undefined method `foobar' for
null_io.foobar # =>
# ~> -:11:in `<main>': undefined method `foobar' for
# <null:IO>:NullIO (NoMethodError)
```

Expand Down Expand Up @@ -268,8 +268,8 @@ null = NullObject.instance

null.__id__ # => 17844080
NullObject.instance.__id__ # => 17844080
NullObject.new # =>
# ~> -:11:in `<main>': private method `new' called for
NullObject.new # =>
# ~> -:11:in `<main>': private method `new' called for
# NullObject:Class (NoMethodError)
```

Expand Down Expand Up @@ -303,7 +303,7 @@ NullObject = Naught.build do |config|
else
config.singleton
end
end
end
```

The only caveat is that when swapping between singleton and
Expand Down
13 changes: 13 additions & 0 deletions lib/naught/basic_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Naught
if defined? ::BasicObject
class BasicObject < ::BasicObject
end
else
class BasicObject #:nodoc:
keep = %w[! != == __id__ __send__ equal? instance_eval instance_exec method_missing singleton_method_added singleton_method_removed singleton_method_undefined]
instance_methods.each do |method_name|
undef_method(method_name) unless keep.include?(method_name)
end
end
end
end
57 changes: 57 additions & 0 deletions lib/naught/conversions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Naught
module Conversions

def self.included(null_class)
unless class_variable_defined?(:@@included) && @@included
@@null_class = null_class
@@null_equivs = null_class::NULL_EQUIVS
@@included = true
end
super
end

def Null(object=:nothing_passed)
case object
when NullObjectTag
object
when :nothing_passed, *@@null_equivs
@@null_class.get(:caller => caller(1))
else
raise ArgumentError, "#{object.inspect} is not null!"
end
end

def Maybe(object=nil)
object = yield if block_given?
case object
when NullObjectTag
object
when *@@null_equivs
@@null_class.get(:caller => caller(1))
else
object
end
end

def Just(object=nil)
object = yield if block_given?
case object
when NullObjectTag, *@@null_equivs
raise ArgumentError, "Null value: #{object.inspect}"
else
object
end
end

def Actual(object=nil)
object = yield if block_given?
case object
when NullObjectTag
nil
else
object
end
end

end
end
42 changes: 27 additions & 15 deletions lib/naught/null_class_builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'naught/null_class_builder/conversions_module'
require 'naught/basic_object'
require 'naught/conversions'

module Naught
class NullClassBuilder
Expand All @@ -10,8 +11,8 @@ module Commands

def initialize
@interface_defined = false
@base_class = BasicObject
@inspect_proc = ->{ "<null>" }
@base_class = Naught::BasicObject
@inspect_proc = lambda { "<null>" }
@stub_strategy = :stub_method_returning_nil
define_basic_methods
end
Expand All @@ -33,10 +34,6 @@ def null_equivalents
@null_equivalents ||= [nil]
end

def generate_conversions_module(null_class)
ConversionsModule.new(null_class, null_equivalents)
end

def generate_class
respond_to_any_message unless interface_defined?
generation_mod = Module.new
Expand All @@ -48,7 +45,13 @@ def generate_class
null_class = Class.new(@base_class) do
const_set :GeneratedMethods, generation_mod
const_set :Customizations, customization_mod
const_set :Conversions, builder.generate_conversions_module(self)
const_set :NULL_EQUIVS, builder.null_equivalents
include Conversions
remove_const :NULL_EQUIVS
Conversions.instance_methods.each do |instance_method|
undef_method(instance_method)
end
const_set :Conversions, Conversions

include NullObjectTag
include generation_mod
Expand All @@ -70,11 +73,20 @@ def method_missing(method_name, *args, &block)
end
end

def respond_to_missing?(method_name, include_private=false)
command_name = command_name_for_method(method_name)
Commands.const_defined?(command_name) || super
rescue NameError
super
if RUBY_VERSION >= '1.9'
def respond_to_missing?(method_name, include_private=false)
command_name = command_name_for_method(method_name)
Commands.const_defined?(command_name) || super
rescue NameError
super
end
else
def respond_to?(method_name, include_private=false)
command_name = command_name_for_method(method_name)
Commands.const_defined?(command_name) || super
rescue NameError
super
end
end

############################################################################
Expand All @@ -88,7 +100,7 @@ def black_hole
end

def respond_to_any_message
defer(prepend: true) do |subject|
defer(:prepend => true) do |subject|
subject.module_eval do
def respond_to?(*)
true
Expand Down Expand Up @@ -136,7 +148,7 @@ def initialize(*)
end

def define_basic_class_methods
defer(class: true) do |subject|
defer(:class => true) do |subject|
subject.module_eval do
class << self
alias get new
Expand Down
5 changes: 3 additions & 2 deletions lib/naught/null_class_builder/commands/mimic.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'naught/basic_object'
require 'naught/null_class_builder/command'

module Naught::NullClassBuilder::Commands
Expand All @@ -11,7 +12,7 @@ def initialize(builder, class_to_mimic, options={})
@include_super = options.fetch(:include_super) { true }

builder.base_class = root_class_of(class_to_mimic)
builder.inspect_proc = -> { "<null:#{class_to_mimic}>" }
builder.inspect_proc = lambda { "<null:#{class_to_mimic}>" }
builder.interface_defined = true
end

Expand All @@ -29,7 +30,7 @@ def root_class_of(klass)
if klass.ancestors.include?(Object)
Object
else
BasicObject
Naught::BasicObject
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/naught/null_class_builder/commands/singleton.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Naught::NullClassBuilder::Commands
class Singleton < Naught::NullClassBuilder::Command
def call
defer(class: true) do |subject|
defer(:class => true) do |subject|
require 'singleton'
subject.module_eval do
include ::Singleton
Expand Down
6 changes: 5 additions & 1 deletion lib/naught/null_class_builder/commands/traceable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def call
attr_reader :__file__, :__line__

def initialize(options={})
backtrace = options.fetch(:caller) { Kernel.caller(4) }
backtrace = if RUBY_VERSION.to_f == 1.9 && RUBY_PLATFORM != 'java'
options.fetch(:caller) { Kernel.caller(4) }
else
options.fetch(:caller) { Kernel.caller(3) }
end
@__file__, line, _ = backtrace[0].split(':')
@__line__ = line.to_i
end
Expand Down
57 changes: 0 additions & 57 deletions lib/naught/null_class_builder/conversions_module.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/base_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Naught.build do |b|
default_base_class = b.base_class
end
expect(default_base_class).to eq(BasicObject)
expect(default_base_class).to eq(Naught::BasicObject)
end

describe 'singleton null object' do
Expand Down
2 changes: 1 addition & 1 deletion spec/basic_null_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
expect(null_class.get.class).to be(null_class)
end

end
end
2 changes: 1 addition & 1 deletion spec/blackhole_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
expect(null.foobaz).to be(null)
expect(null << "bar").to be(null)
end
end
end
12 changes: 8 additions & 4 deletions spec/conversions_spec.rb → spec/explicit_conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
expect(null.to_a).to eq([])
expect(null.to_i).to eq(0)
expect(null.to_f).to eq(0.0)
expect(null.to_c).to eq(Complex(0))
expect(null.to_r).to eq(Rational(0))
expect(null.to_h).to eq({})
if RUBY_VERSION >= '1.9'
expect(null.to_c).to eq(Complex(0))
expect(null.to_r).to eq(Rational(0))
end
if RUBY_VERSION >= '2.0'
expect(null.to_h).to eq({})
end
end
end
end
2 changes: 1 addition & 1 deletion spec/functions/actual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
expect(Actual{ConvertableNull.new}).to be_nil
expect(Actual{"foo"}).to eq("foo")
end
end
end
Loading