Module: Cube::Interface

Defined in:
lib/cube/interfaces.rb

Defined Under Namespace

Classes: InterfaceMatchError, MethodArityError, MethodMissing, PrivateVisibleMethodMissing, PublicVisibleMethodMissing, TypeMismatchError

Instance Method Summary collapse

Instance Method Details

#impotentObject



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/cube/interfaces.rb', line 194

def impotent
  cl = Cube.interface {}.extend(self)
  cl.module_exec do
    def extend_object(mod)
      super
    end
    def append_features(mod)
      super
    end
  end
  cl
end

#proto(meth, *args) ⇒ Object



179
180
181
182
183
184
# File 'lib/cube/interfaces.rb', line 179

def proto(meth, *args)
  out_spec = yield if block_given?
  validate_spec(args)
  validate_spec(out_spec) if out_spec
  @ids.merge!({ meth.to_sym => { in: args, out: out_spec } })
end

#public_visible(*ids) ⇒ Object



186
187
188
189
190
191
192
# File 'lib/cube/interfaces.rb', line 186

def public_visible(*ids)
  unless ids.all? { |id| id.is_a?(Symbol) || id.is_a?(String) }
    raise ArgumentError, "Arguments should be strings or symbols"
  end
  spec = map_spec(ids)
  @ids.merge!(spec)
end

#required_public_methodsObject

Accepts an array of method names that define the interface. When this module is included/implemented, those method names must have already been defined.



166
167
168
# File 'lib/cube/interfaces.rb', line 166

def required_public_methods
  @ids.keys
end

#shellObject

creates a shell object for testing



208
209
210
211
212
213
214
215
216
217
# File 'lib/cube/interfaces.rb', line 208

def shell
  ids = @ids
  unreq = @unreq
  cls = Class.new(Object) do
    (ids.keys - unreq).each do |m|
      define_method(m) { |*args| }
    end
  end
  Cube[cls].send(:shell_implements, self)
end

#to_specObject



170
171
172
173
174
175
176
177
# File 'lib/cube/interfaces.rb', line 170

def to_spec
  inherited = (self.ancestors-[self]).select{ |x| Interface === x }
  inherited_ids = inherited.map{ |x| x.instance_variable_get('@ids') }

  # Store required method ids
  inherited_specs = map_spec(inherited_ids.flatten)
  @ids.merge(inherited_specs)
end