Sha256: 9b7add4faf8d65d6bbc19f21e8efc432f7e6305a65ed135a7424753ecff968aa

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require_relative 'value_struct/version'

require_relative 'value_struct/immutable'
require_relative 'value_struct/dup_with_changes'
require_relative 'value_struct/strict_arguments'
require_relative 'value_struct/no_clone'
require_relative 'value_struct/freeze'

class ValueStruct < Struct
  class << self
    alias build new

    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

    def new(*args, &block)
      mixins = [ValueStruct::DupWithChanges]
      new_with_mixins(*args, mixins, &block)
    end
  end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
value_struct-0.8.1 lib/value_struct.rb
value_struct-0.8.0 lib/value_struct.rb