Module: Tins::BlankSlate
- Defined in:
- lib/tins/dslkit.rb
Overview
A module that provides blank slate class creation functionality.
This module enables the creation of anonymous classes with restricted method sets, allowing for precise control over object interfaces. Blank slates are useful for security, DSL construction, testing, and creating clean API surfaces.
Class Method Summary collapse
-
.with(*ids) ⇒ Class
Creates an anonymous blank slate class with restricted method set.
Class Method Details
.with(*ids) ⇒ Class
Creates an anonymous blank slate class with restricted method set.
This method generates a new class that inherits from the specified superclass (or Object by default) and removes all methods except those explicitly allowed. The allowed methods can be specified as symbols, strings, or regular expressions.
593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/tins/dslkit.rb', line 593 def self.with(*ids) opts = Hash === ids.last ? ids.pop : {} ids = ids.map { |id| Regexp === id ? id : id.to_s } klass = opts[:superclass] ? Class.new(opts[:superclass]) : Class.new klass.instance_eval do instance_methods.each do |m| m = m.to_s undef_method m unless m =~ /^(__|object_id)/ or ids.any? { |i| i === m } end end klass end |