This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
require 'ostruct'
|
||||
|
||||
class OpenStruct
|
||||
|
||||
# Iterate over key-value pairs.
|
||||
#
|
||||
def each(&blk)
|
||||
@table.each(&blk)
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,48 @@
|
||||
require 'ostruct'
|
||||
|
||||
class OpenStruct
|
||||
|
||||
# Allows the initialization of an OpenStruct with a setter block:
|
||||
#
|
||||
# person = OpenStruct.new do |o|
|
||||
# o.name = 'John Smith'
|
||||
# o.gender = :M
|
||||
# o.age = 71
|
||||
# end
|
||||
#
|
||||
# You can still provide a hash for initialization purposes, and even combine
|
||||
# the two approaches if you wish.
|
||||
#
|
||||
# person = OpenStruct.new(:name => 'John Smith', :age => 31) do |p|
|
||||
# p.gender = :M
|
||||
# end
|
||||
#
|
||||
# Alternatively you can provide a default block:
|
||||
#
|
||||
# stuff = OpenStruct.new{ |o,k| o[k] = [] }
|
||||
# stuff.place << :a
|
||||
# stuff.place << :b
|
||||
# stuff.place #=> [:a, :b]
|
||||
#
|
||||
# A setter block versus a defualt block is determined by the arity of
|
||||
# the block. You cannot provide both at the same time.
|
||||
#
|
||||
# CREDIT: Noah Gibbs, Gavin Sinclair
|
||||
def initialize(hash=nil, &block)
|
||||
if block && block.arity==2
|
||||
@table = Hash.new(&block)
|
||||
else
|
||||
@table = {}
|
||||
end
|
||||
if hash
|
||||
for k,v in hash
|
||||
@table[k.to_sym] = v
|
||||
new_ostruct_member(k)
|
||||
end
|
||||
end
|
||||
if block && block.arity==1
|
||||
yield self
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,21 @@
|
||||
require 'ostruct'
|
||||
|
||||
class OpenStruct
|
||||
|
||||
# Insert/update hash data on the fly.
|
||||
#
|
||||
# o = OpenStruct.new
|
||||
# o.merge!(:a => 2)
|
||||
# o.a #=> 2
|
||||
#
|
||||
def merge!(other)
|
||||
raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
|
||||
##other = other.to_hash #to_h?
|
||||
for k,v in other
|
||||
@table[k.to_sym] = v
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
require 'ostruct'
|
||||
|
||||
class OpenStruct
|
||||
|
||||
# Access a value in the OpenStruct by key, like a Hash.
|
||||
# This increases OpenStruct's "duckiness".
|
||||
#
|
||||
# o = OpenStruct.new
|
||||
# o.t = 4
|
||||
# o['t'] #=> 4
|
||||
#
|
||||
def [](key)
|
||||
key = key.to_sym unless key.is_a?(Symbol)
|
||||
@table[key]
|
||||
end
|
||||
|
||||
# Set a value in the OpenStruct by key, like a Hash.
|
||||
#
|
||||
# o = OpenStruct.new
|
||||
# o['t'] = 4
|
||||
# o.t #=> 4
|
||||
#
|
||||
def []=(key,val)
|
||||
raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
|
||||
key = key.to_sym unless key.is_a?(Symbol)
|
||||
@table[key]=val
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1 @@
|
||||
require 'facets/ostruct/op_fetch'
|
||||
@@ -0,0 +1,10 @@
|
||||
require 'ostruct'
|
||||
|
||||
class OpenStruct
|
||||
|
||||
#
|
||||
def to_h
|
||||
@table.dup
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,65 @@
|
||||
require 'ostruct'
|
||||
|
||||
class OpenStruct
|
||||
#
|
||||
def to_ostruct
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
class Hash
|
||||
# Turns a hash into a generic object using an OpenStruct.
|
||||
#
|
||||
# o = {'a' => 1}.to_ostruct
|
||||
# o.a #=> 1
|
||||
#
|
||||
def to_ostruct
|
||||
OpenStruct.new(self)
|
||||
end
|
||||
|
||||
## Recursively converts a Hash object to an OpenStruct object.
|
||||
##
|
||||
## Returns a copy of the hash as an OpenStruct.
|
||||
#def to_ostruct()
|
||||
# struct = OpenStruct.new(self.clone())
|
||||
# each do |k, v|
|
||||
# struct.__send__("#{k}=", v.to_ostruct()) if v.is_a?(Hash)
|
||||
# struct.__send__(k).map! {|x| x.is_a?(Hash) ? x.to_ostruct() : x} if v.is_a?(Array)
|
||||
# end
|
||||
# return struct
|
||||
#end
|
||||
|
||||
# Like `#to_ostruct` but recusively objectifies all hash elements as well.
|
||||
#
|
||||
# o = {'a' => { 'b' => 1 }}.to_ostruct_recurse
|
||||
# o.a.b #=> 1
|
||||
#
|
||||
# The `exclude` parameter is used internally to prevent infinite
|
||||
# recursion and is not intended to be utilized by the end-user.
|
||||
# But for more advance use, if there is a particular subhash you
|
||||
# would like to prevent from being converted to an OpoenStruct
|
||||
# then include it in the `exclude` hash referencing itself. e.g.
|
||||
#
|
||||
# h = { 'a' => { 'b' => 1 } }
|
||||
# o = h.to_ostruct_recurse( { h['a'] => h['a'] } )
|
||||
# o.a['b'] #=> 1
|
||||
#
|
||||
# CREDIT: Alison Rowland, Jamie Macey, Mat Schaffer
|
||||
def to_ostruct_recurse(exclude={})
|
||||
return exclude[self] if exclude.key?( self )
|
||||
o = exclude[self] = OpenStruct.new
|
||||
h = self.dup
|
||||
each_pair do |k,v|
|
||||
h[k] = v.to_ostruct_recurse( exclude ) if v.respond_to?(:to_ostruct_recurse)
|
||||
end
|
||||
o.merge!(h)
|
||||
end
|
||||
end
|
||||
|
||||
class NilClass
|
||||
# Create an empty OpenStruct object.
|
||||
def to_ostruct
|
||||
OpenStruct.new
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user