Sha256: 9fcee1af69b58aedfe40509f9692fb5d6cb0e92dc5620d5f5b3941c74df7b83a

Contents?: true

Size: 1.26 KB

Versions: 68

Compression:

Stored size: 1.26 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 = {}
    if hash
      hash.each_pair do |k, v|
        k = k.to_sym
        @table[k] = v
        new_ostruct_member(k)
      end
    end
  end

  def modifiable
    begin
      @modifiable = true
    rescue
      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

68 entries across 68 versions & 1 rubygems

Version Path
trusty-cms-4.1.2 lib/ostruct.rb
trusty-cms-4.1.1 lib/ostruct.rb
trusty-cms-4.1.0 lib/ostruct.rb
trusty-cms-4.0.2 lib/ostruct.rb
trusty-cms-3.9.7 lib/ostruct.rb
trusty-cms-3.9.6 lib/ostruct.rb
trusty-cms-3.9.5 lib/ostruct.rb
trusty-cms-4.0.1 lib/ostruct.rb
trusty-cms-3.9.4 lib/ostruct.rb
trusty-cms-3.9.3 lib/ostruct.rb
trusty-cms-3.9.2 lib/ostruct.rb
trusty-cms-4.0.0 lib/ostruct.rb
trusty-cms-3.9.1 lib/ostruct.rb
trusty-cms-3.9.0 lib/ostruct.rb
trusty-cms-3.8.4 lib/ostruct.rb
trusty-cms-3.8.3 lib/ostruct.rb
trusty-cms-3.8.2 lib/ostruct.rb
trusty-cms-3.8.1 lib/ostruct.rb
trusty-cms-3.8.0 lib/ostruct.rb
trusty-cms-3.7.1 lib/ostruct.rb