Sha256: e105afb609de194989984e8be73ea284c5bb0d1a9dfe26eddca7305042f84199

Contents?: true

Size: 1.24 KB

Versions: 92

Compression:

Stored size: 1.24 KB

Contents

# patching OpenStruct due to Ruby 2.3.1 upgrade
# See more here: http://stackoverflow.com/questions/39278864/openstruct-issue-with-ruby-2-3-1/39280374#39280374

class OpenStruct
  def initialize(hash = nil)
    @table = {}
    hash&.each_pair do |k, v|
      k = k.to_sym
      @table[k] = v
      new_ostruct_member(k)
    end
  end

  def modifiable
    begin
      @modifiable = true
    rescue StandardError
      raise RuntimeError, "can't modify frozen #{self.class}", caller(3)
    end
    @table
  end
  protected :modifiable

  def new_ostruct_member(name)
    name = name.to_sym
    unless respond_to?(name)
      define_singleton_method(name) { @table[name] }
      define_singleton_method("#{name}=") { |x| modifiable[name] = x }
    end
    name
  end
  protected :new_ostruct_member

  def method_missing(mid, *args) # :nodoc:
    mname = mid.id2name
    len = args.length
    if mname.chomp!('=')
      if len != 1
        raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
      end

      modifiable[new_ostruct_member(mname)] = args[0]
    elsif len == 0
      @table[mid]
    else
      err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args
      err.set_backtrace caller(1)
      raise err
    end
  end
end

Version data entries

92 entries across 92 versions & 1 rubygems

Version Path
trusty-cms-7.0.22 lib/ostruct.rb
trusty-cms-7.0.21 lib/ostruct.rb
trusty-cms-7.0.20 lib/ostruct.rb
trusty-cms-7.0.19 lib/ostruct.rb
trusty-cms-7.0.18 lib/ostruct.rb
trusty-cms-7.0.17 lib/ostruct.rb
trusty-cms-7.0.16 lib/ostruct.rb
trusty-cms-7.0.14 lib/ostruct.rb
trusty-cms-7.0.13 lib/ostruct.rb
trusty-cms-7.0.12 lib/ostruct.rb
trusty-cms-7.0.15 lib/ostruct.rb
trusty-cms-7.0.9.1 lib/ostruct.rb
trusty-cms-7.0.11 lib/ostruct.rb
trusty-cms-7.0.10 lib/ostruct.rb
trusty-cms-7.0.9 lib/ostruct.rb
trusty-cms-7.0.8 lib/ostruct.rb
trusty-cms-7.0.7 lib/ostruct.rb
trusty-cms-7.0.6 lib/ostruct.rb
trusty-cms-7.0.5 lib/ostruct.rb
trusty-cms-7.0.4 lib/ostruct.rb