Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Add DeepStruct to imitate Sawyer::Resource
Browse files Browse the repository at this point in the history
OpenStruct doesn’t recursively provide method accessors.

See http://andreapavoni.com/blog/2013/4/create-recursive-openstruct-from-a-ruby-hash
  • Loading branch information
garethrees committed Sep 7, 2015
1 parent d19b75a commit c668ae4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
@@ -1,10 +1,12 @@
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'github-backup'
require 'minitest/autorun'
require 'fakefs/safe'
require File.join(File.dirname(__FILE__), 'support', 'deep_struct')

def sawyer_repo
OpenStruct.new(
DeepStruct.new(
{:id=>251112,
:name=>"github-backup",
:full_name=>"ddollar/github-backup",
Expand Down
22 changes: 22 additions & 0 deletions spec/support/deep_struct.rb
@@ -0,0 +1,22 @@
require 'ostruct'

class DeepStruct < OpenStruct
def initialize(hash=nil)
@table = {}
@hash_table = {}

if hash
hash.each do |k,v|
@table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
@hash_table[k.to_sym] = v

new_ostruct_member(k)
end
end
end

def to_h
@hash_table
end

end

0 comments on commit c668ae4

Please sign in to comment.