Class: YARD::OpenStruct

Inherits:
Object
  • Object
show all
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.

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ OpenStruct

Returns a new instance of OpenStruct.



5
6
7
# 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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/yard/open_struct.rb', line 15

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) ⇒ Object



30
31
32
# File 'lib/yard/open_struct.rb', line 30

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

#[](key) ⇒ Object



46
47
48
# File 'lib/yard/open_struct.rb', line 46

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

#[]=(key, value) ⇒ Object



42
43
44
# File 'lib/yard/open_struct.rb', line 42

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

#dig(*keys) ⇒ Object



38
39
40
# File 'lib/yard/open_struct.rb', line 38

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

#each_pair(&block) ⇒ Object



50
51
52
# File 'lib/yard/open_struct.rb', line 50

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

#hash ⇒ Object



34
35
36
# File 'lib/yard/open_struct.rb', line 34

def hash
  @table.hash
end

#initialize_copy(other) ⇒ Object



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

def initialize_copy(other)
  super
  @table = @table.dup
end

#marshal_dump ⇒ Object



54
55
56
# File 'lib/yard/open_struct.rb', line 54

def marshal_dump
  @table
end

#marshal_load(data) ⇒ Object



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

def marshal_load(data)
  @table = data
end

#to_h ⇒ Object



26
27
28
# File 'lib/yard/open_struct.rb', line 26

def to_h
  @table.dup
end