Sha256: ff494cab63b9178f309ddedaaa1cd8f30cdd8a27e809150c98e664ece6638b73

Contents?: true

Size: 910 Bytes

Versions: 1

Compression:

Stored size: 910 Bytes

Contents

# Courtesy https://gist.github.com/FluffyJack/6ecd13fda8ff9c6be879
module Motion
  class OpenStruct < BasicObject
    def initialize(hash)
      @struct = ::Struct.new(*hash.keys.map { |k| k.to_sym })
      @struct = @struct.new(*hash.values)
    end

    def ==(other)
      to_h == other.to_h
    end

    def method_missing(method_name, *args, &block)
      if method_name.to_s.index('=') && !@struct.respond_to?(method_name)
        vals = @struct.to_h.values
        getter_name = method_name.to_s.gsub('=', '')
        getters = ((@struct.to_h.keys.map { |k| k.to_sym }) + [getter_name.to_sym])
        @struct = ::Struct.new(*getters)
        @struct = @struct.new(*vals)
      end
      @struct.send(method_name, *args, &block)
    end

    def respond_to_missing?(method_name, include_private = false)
      return true if @struct.respond_to?(method_name)
      return true if super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
motion-ostruct-0.0.1 lib/motion-ostruct/open_struct.rb