123456789_123456789_123456789_123456789_123456789_

Class: YARD::OpenStruct

Relationships & Source Files
Inherits: Object
Defined in: lib/yard/open_struct.rb

Overview

An OpenStruct compatible struct class that allows for basic access of attributes via struct.attr_name and +struct.attr_name = value+.

Class Method Summary

Instance Method Summary

Constructor Details

.new(hash = {}) ⇒ OpenStruct

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 5

def initialize(hash = {})
  @table = hash.each_pair { |k, v| [k.to_sym, v] }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 10

def method_missing(name, *args)
  if name.to_s.end_with?('=')
    varname = name.to_s[0..-2].to_sym
    __cache_lookup__(varname)
    send(name, args.first)
  else
    __cache_lookup__(name)
    send(name)
  end
end

Instance Method Details

#==(other)

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 25

def ==(other)
  other.is_a?(self.class) && to_h == other.to_h
end

#[](key)

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 41

def [](key)
  @table[key.to_sym]
end

#[]=(key, value)

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 37

def []=(key, value)
  @table[key.to_sym] = value
end

#__cache_lookup__(name) (private)

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 59

def __cache_lookup__(name)
  key = name.to_sym.inspect
  instance_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}; @table[#{key}]; end
    def #{name.to_s.sub('?','_')}=(v); @table[#{key}] = v; end unless #{key}.to_s.include?('?')
  RUBY
end

#dig(*keys)

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 33

def dig(*keys)
  @table.dig(*keys)
end

#each_pair(&block)

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 45

def each_pair(&block)
  @table.each_pair(&block)
end

#hash

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 29

def hash
  @table.hash
end

#marshal_dump

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 49

def marshal_dump
  @table
end

#marshal_load(data)

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 53

def marshal_load(data)
  @table = data
end

#to_h

[ GitHub ]

  
# File 'lib/yard/open_struct.rb', line 21

def to_h
  @table.dup
end