Class: Steep::Interface::Shape::Methods

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/steep/interface/shape.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(substs:, methods:) ⇒ Methods

Returns a new instance of Methods.



113
114
115
116
117
# File 'lib/steep/interface/shape.rb', line 113

def initialize(substs:, methods:)
  @substs = substs
  @methods = methods
  @resolved_methods = {}
end

Instance Attribute Details

#methodsObject (readonly)

Returns the value of attribute methods.



109
110
111
# File 'lib/steep/interface/shape.rb', line 109

def methods
  @methods
end

#resolved_methodsObject (readonly)

Returns the value of attribute resolved_methods.



109
110
111
# File 'lib/steep/interface/shape.rb', line 109

def resolved_methods
  @resolved_methods
end

#substsObject (readonly)

Returns the value of attribute substs.



109
110
111
# File 'lib/steep/interface/shape.rb', line 109

def substs
  @substs
end

Instance Method Details

#[](name) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/steep/interface/shape.rb', line 132

def [](name)
  return nil unless key?(name)

  resolved_methods[name] ||= begin
    entry = methods.fetch(name)
    Entry.new(
      method_name: name,
      overloads: entry.overloads.map do |overload|
        overload.subst(subst)
      end,
      private_method: entry.private_method?
    )
  end
end

#[]=(name, entry) ⇒ Object



127
128
129
130
# File 'lib/steep/interface/shape.rb', line 127

def []=(name, entry)
  resolved_methods[name] = nil
  methods[name] = entry
end

#each(&block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/steep/interface/shape.rb', line 147

def each(&block)
  if block
    methods.each_key do |name|
      entry = self[name] or next
      yield [name, entry]
    end
  else
    enum_for :each
  end
end

#each_name(&block) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/steep/interface/shape.rb', line 158

def each_name(&block)
  if block
    each do |name, _|
      yield name
    end
  else
    enum_for :each_name
  end
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
124
125
# File 'lib/steep/interface/shape.rb', line 119

def key?(name)
  if entry = methods.fetch(name, nil)
    entry.has_method_type?
  else
    false
  end
end

#merge!(other, &block) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/steep/interface/shape.rb', line 180

def merge!(other, &block)
  other.each do |name, entry|
    if block && (old_entry = methods[name])
      methods[name] = yield(name, old_entry, entry)
    else
      methods[name] = entry
    end
  end
end

#public_methodsObject



190
191
192
193
194
195
# File 'lib/steep/interface/shape.rb', line 190

def public_methods
  Methods.new(
    substs: substs,
    methods: methods.reject {|_, entry| entry.private_method? }
  )
end

#push_substitution(subst) ⇒ Object



176
177
178
# File 'lib/steep/interface/shape.rb', line 176

def push_substitution(subst)
  Methods.new(substs: [*substs, subst], methods: methods)
end

#substObject



168
169
170
171
172
173
174
# File 'lib/steep/interface/shape.rb', line 168

def subst
  @subst ||= begin
    substs.each_with_object(Substitution.empty) do |s, ss|
      ss.merge!(s, overwrite: true)
    end
  end
end