Class: ValueStruct

Inherits:
Struct
  • Object
show all
Defined in:
lib/value_struct.rb,
lib/value_struct/version.rb

Defined Under Namespace

Modules: DupWithChanges, Freeze, Immutable, NoClone, StrictArguments, ToH

Constant Summary collapse

VERSION =
'0.7.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.buildObject



12
# File 'lib/value_struct.rb', line 12

alias build new

.new(*args, &block) ⇒ Object



30
31
32
33
34
# File 'lib/value_struct.rb', line 30

def new(*args, &block)
  mixins = [ValueStruct::DupWithChanges]
  mixins.unshift(ValueStruct::ToH) if RUBY_VERSION < "2.0"
  new_with_mixins(*args, mixins, &block)
end

.new_with_mixins(*args, mixins, &block) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/value_struct.rb', line 14

def new_with_mixins(*args, mixins, &block)
  raise ArgumentError, 'mixin list (last paramater) must be an array' unless mixins.is_a? Array

  struct_class = build(*args, &block)
  struct_class.send(:include, ValueStruct::Immutable)

  mixins.each{ |mixin|
    if mixin.is_a?(Symbol) # convenient including bundled mixins ala :dup_with_changes
      mixin = ValueStruct.const_get(mixin.to_s.gsub(/(?:^|_)([a-z])/){ $1.upcase })
    end
    struct_class.send(:include, mixin)
  }

  struct_class
end

Instance Method Details

#inspectObject



37
38
39
# File 'lib/value_struct.rb', line 37

def inspect
  super.to_s.sub('struct', 'ValueStruct')
end