Skip to content

Commit

Permalink
Extracting 1.9.3 backported methods and fixes into a module.
Browse files Browse the repository at this point in the history
This keeps the main class clean, and it makes it easy to know what was
backported for 1.9.3.

Closes #32
  • Loading branch information
aetherknight committed May 20, 2015
1 parent c0e912a commit 54cbd86
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
24 changes: 2 additions & 22 deletions lib/recursive_open_struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

require 'recursive_open_struct/debug_inspect'
require 'recursive_open_struct/deep_dup'
require 'recursive_open_struct/ruby_19_backport'

class RecursiveOpenStruct < OpenStruct
include Ruby19Backport if RUBY_VERSION =~ /\A1.9/
include DebugInspect

def initialize(hash={}, args={})
Expand All @@ -20,10 +22,6 @@ def initialize(hash={}, args={})
def initialize_copy(orig)
super

# Apply fix if necessary:
# https://github.com/ruby/ruby/commit/2d952c6d16ffe06a28bb1007e2cd1410c3db2d58
@table.each_key{|key| new_ostruct_member(key)} if RUBY_VERSION =~ /^1.9/

# deep copy the table to separate the two objects
@table = @deep_dup.call(orig.instance_variable_get(:@table))
# Forget any memoized sub-elements
Expand All @@ -40,10 +38,6 @@ def [](name)
send name
end

def []=(name, value)
modifiable[new_ostruct_member(name)] = value
end if RUBY_VERSION =~ /^1.9/

def new_ostruct_member(name)
key_name = _get_key_from_table_ name
unless self.respond_to?(name)
Expand Down Expand Up @@ -90,20 +84,6 @@ def delete_field(name)
@table.delete sym
end

def eql?(other)
return false unless other.kind_of?(OpenStruct)
@table.eql?(other.table)
end if RUBY_VERSION =~ /^1.9/

def hash
@table.hash
end if RUBY_VERSION =~ /^1.9/

def each_pair
return to_enum(:each_pair) { @table.size } unless block_given?
@table.each_pair{|p| yield p}
end if RUBY_VERSION =~ /^1.9/

private

def _get_key_from_table_(name)
Expand Down
27 changes: 27 additions & 0 deletions lib/recursive_open_struct/ruby_19_backport.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module RecursiveOpenStruct::Ruby19Backport
# Apply fix if necessary:
# https://github.com/ruby/ruby/commit/2d952c6d16ffe06a28bb1007e2cd1410c3db2d58
def initialize_copy(orig)
super
@table.each_key{|key| new_ostruct_member(key)}
end

def []=(name, value)
modifiable[new_ostruct_member(name)] = value
end

def eql?(other)
return false unless other.kind_of?(OpenStruct)
@table.eql?(other.table)
end

def hash
@table.hash
end

def each_pair
return to_enum(:each_pair) { @table.size } unless block_given?
@table.each_pair{|p| yield p}
end
end

0 comments on commit 54cbd86

Please sign in to comment.