Module: Cube::Interface
- Defined in:
- lib/cube/interfaces.rb
Defined Under Namespace
Classes: MethodArityError, MethodMissing, PrivateVisibleMethodMissing, PublicVisibleMethodMissing, TypeMismatchError
Constant Summary collapse
- VERSION =
The version of the interface library.
'0.2.0'
Instance Method Summary collapse
- #private_visible(*ids) ⇒ Object
- #proto(meth, *args) ⇒ Object
- #public_visible(*ids) ⇒ Object
-
#required_public_methods ⇒ Object
Accepts an array of method names that define the interface.
- #shell ⇒ Object
-
#unrequired_methods(*ids) ⇒ Object
Accepts an array of method names that are removed as a requirement for implementation.
Instance Method Details
#private_visible(*ids) ⇒ Object
179 180 181 182 183 184 185 |
# File 'lib/cube/interfaces.rb', line 179 def private_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) @private_ids.merge!(spec) end |
#proto(meth, *args) ⇒ Object
164 165 166 167 168 169 |
# File 'lib/cube/interfaces.rb', line 164 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
171 172 173 174 175 176 177 |
# File 'lib/cube/interfaces.rb', line 171 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_methods ⇒ Object
Accepts an array of method names that define the interface. When this module is included/implemented, those method names must have already been defined.
160 161 162 |
# File 'lib/cube/interfaces.rb', line 160 def required_public_methods @ids.keys end |
#shell ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/cube/interfaces.rb', line 195 def shell ids = @ids unreq = @unreq cls = Class.new(Object) do (ids.keys - unreq).each do |m| define_method(m) { |*args| } end end cls.send(:shell_implements, self) end |
#unrequired_methods(*ids) ⇒ Object
Accepts an array of method names that are removed as a requirement for implementation. Presumably you would use this in a sub-interface where you only wanted a partial implementation of an existing interface.
190 191 192 193 |
# File 'lib/cube/interfaces.rb', line 190 def unrequired_methods(*ids) @unreq ||= [] @unreq += ids end |