Class: YARD::OpenStruct
- Inherits:
-
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
10
11
12
13
14
15
16
17
18
19
|
# 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) ⇒ Object
25
26
27
|
# File 'lib/yard/open_struct.rb', line 25
def ==(other)
other.is_a?(self.class) && to_h == other.to_h
end
|
#[](key) ⇒ Object
41
42
43
|
# File 'lib/yard/open_struct.rb', line 41
def [](key)
@table[key.to_sym]
end
|
#[]=(key, value) ⇒ Object
37
38
39
|
# File 'lib/yard/open_struct.rb', line 37
def []=(key, value)
@table[key.to_sym] = value
end
|
#dig(*keys) ⇒ Object
33
34
35
|
# File 'lib/yard/open_struct.rb', line 33
def dig(*keys)
@table.dig(*keys)
end
|
#each_pair(&block) ⇒ Object
45
46
47
|
# File 'lib/yard/open_struct.rb', line 45
def each_pair(&block)
@table.each_pair(&block)
end
|
#hash ⇒ Object
29
30
31
|
# File 'lib/yard/open_struct.rb', line 29
def hash
@table.hash
end
|
#marshal_dump ⇒ Object
49
50
51
|
# File 'lib/yard/open_struct.rb', line 49
def marshal_dump
@table
end
|
#marshal_load(data) ⇒ Object
53
54
55
|
# File 'lib/yard/open_struct.rb', line 53
def marshal_load(data)
@table = data
end
|
#to_h ⇒ Object
21
22
23
|
# File 'lib/yard/open_struct.rb', line 21
def to_h
@table.dup
end
|