class Plan9::ImprovedStruct
@!visibility private
Public Class Methods
new(*attrs, &block)
click to toggle source
Re-organized slightly, this code is reused from ‘ImmutableStruct’ by Theo Hultberg. See github.com/iconara/immutable_struct Copyright notice mentioned in the LICENSE file.
@!visibility private
# File lib/plan9/structures.rb, line 11 def self.new(*attrs, &block) init_new(Struct.new(*attrs, &block)) end
new(*attrs)
click to toggle source
# File lib/plan9/structures.rb, line 27 def initialize(*attrs) if is_hash_case?(*attrs) struct_initialize(*members.map { |m| attrs.first[m.to_sym] }) else struct_initialize(*attrs) end end
Protected Class Methods
init_new(struct)
click to toggle source
# File lib/plan9/structures.rb, line 16 def self.init_new(struct) optionalize_constructor!(struct) extend_dup!(struct) struct end
Private Class Methods
extend_dup!(struct)
click to toggle source
# File lib/plan9/structures.rb, line 43 def self.extend_dup!(struct) struct.class_eval do def dup(overrides={}) self.class.new(to_h.merge(overrides)) end end end
optionalize_constructor!(struct)
click to toggle source
# File lib/plan9/structures.rb, line 23 def self.optionalize_constructor!(struct) struct.class_eval do alias_method :struct_initialize, :initialize def initialize(*attrs) if is_hash_case?(*attrs) struct_initialize(*members.map { |m| attrs.first[m.to_sym] }) else struct_initialize(*attrs) end end protected def is_hash_case?(*a) # @return (bool) true if attrs are Hash, false otherwise members.size > 1 && a && a.size == 1 && a.first.instance_of?(Hash) end end end
Private Instance Methods
dup(overrides={})
click to toggle source
# File lib/plan9/structures.rb, line 45 def dup(overrides={}) self.class.new(to_h.merge(overrides)) end
is_hash_case?(*a)
click to toggle source
# File lib/plan9/structures.rb, line 36 def is_hash_case?(*a) # @return (bool) true if attrs are Hash, false otherwise members.size > 1 && a && a.size == 1 && a.first.instance_of?(Hash) end