Class: OpenStruct

Inherits:
Object show all
Defined in:
opal/stdlib/ostruct.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ OpenStruct

Returns a new instance of OpenStruct.

[View source]

2
3
4
5
6
7
8
9
10
# File 'opal/stdlib/ostruct.rb', line 2

def initialize(hash = nil)
  @table = {}

  if hash
    hash.each_pair do |key, value|
      @table[new_ostruct_member(key)] = value
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

[View source]

20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'opal/stdlib/ostruct.rb', line 20

def method_missing(name, *args)
  if args.length > 2
    raise NoMethodError.new("undefined method `#{name}' for #<OpenStruct>", name)
  end
  if name.end_with? '='
    if args.length != 1
      raise ArgumentError, 'wrong number of arguments (0 for 1)'
    end
    @table[new_ostruct_member(name[0..-2])] = args[0]
  else
    @table[name.to_sym]
  end
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.


77
78
79
# File 'opal/stdlib/ostruct.rb', line 77

def table
  @table
end

Instance Method Details

#==(other) ⇒ Object

[View source]

47
48
49
50
51
# File 'opal/stdlib/ostruct.rb', line 47

def ==(other)
  return false unless other.is_a?(OpenStruct)

  @table == other.instance_variable_get(:@table)
end

#===(other) ⇒ Object

[View source]

53
54
55
56
57
# File 'opal/stdlib/ostruct.rb', line 53

def ===(other)
  return false unless other.is_a?(OpenStruct)

  @table === other.instance_variable_get(:@table)
end

#[](name) ⇒ Object

[View source]

12
13
14
# File 'opal/stdlib/ostruct.rb', line 12

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

#[]=(name, value) ⇒ Object

[View source]

16
17
18
# File 'opal/stdlib/ostruct.rb', line 16

def []=(name, value)
  @table[new_ostruct_member(name)] = value
end

#delete_field(name) ⇒ Object

[View source]

79
80
81
82
83
84
85
86
# File 'opal/stdlib/ostruct.rb', line 79

def delete_field(name)
  sym = name.to_sym
  begin
    singleton_class.__send__(:remove_method, sym, "#{sym}=")
  rescue NameError
  end
  @table.delete sym
end

#each_pairObject

[View source]

39
40
41
42
43
44
45
# File 'opal/stdlib/ostruct.rb', line 39

def each_pair
  return enum_for :each_pair unless block_given?

  @table.each_pair do |pair|
    yield pair
  end
end

#eql?(other) ⇒ Boolean

Returns:

[View source]

59
60
61
62
63
# File 'opal/stdlib/ostruct.rb', line 59

def eql?(other)
  return false unless other.is_a?(OpenStruct)

  @table.eql? other.instance_variable_get(:@table)
end

#hashObject

[View source]

73
74
75
# File 'opal/stdlib/ostruct.rb', line 73

def hash
  @table.hash
end

#inspectObject Also known as: to_s

[View source]

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'opal/stdlib/ostruct.rb', line 99

def inspect
  %x{
    var top = (ostruct_ids === undefined),
        ostruct_id = #{__id__};
  }
  begin
    result = "#<#{self.class}"
    %x{
      if (top) {
        ostruct_ids = {};
      }
      if (ostruct_ids.hasOwnProperty(ostruct_id)) {
        return result + ' ...>';
      }
      ostruct_ids[ostruct_id] = true;
    }

    result += ' ' if @table.any?

    result += each_pair.map do |name, value|
      "#{name}=#{value.inspect}"
    end.join ', '

    result += '>'

    result
  ensure
    %x{
      if (top) {
        ostruct_ids = undefined;
      }
    }
  end
end

#new_ostruct_member(name) ⇒ Object

[View source]

88
89
90
91
92
93
94
95
# File 'opal/stdlib/ostruct.rb', line 88

def new_ostruct_member(name)
  name = name.to_sym
  unless respond_to?(name)
    define_singleton_method(name) { @table[name] }
    define_singleton_method("#{name}=") { |x| @table[name] = x }
  end
  name
end

#respond_to_missing?(mid, include_private = false) ⇒ Boolean

:nodoc:

Returns:

[View source]

34
35
36
37
# File 'opal/stdlib/ostruct.rb', line 34

def respond_to_missing?(mid, include_private = false) # :nodoc:
  mname = mid.to_s.chomp('=').to_sym
  @table&.key?(mname) || super
end

#to_hObject

[View source]

65
66
67
# File 'opal/stdlib/ostruct.rb', line 65

def to_h
  @table.dup
end

#to_nObject

[View source]

69
70
71
# File 'opal/stdlib/ostruct.rb', line 69

def to_n
  @table.to_n
end