Module: Ks
- Defined in:
- lib/ks.rb
Overview
“Ks” - as in “kiss” - a generator of keyworded Structs.
Constant Summary collapse
- VERSION =
'0.0.1'
- @@caching_mutex =
Mutex.new
- @@predefined_structs =
{}
Class Method Summary collapse
-
.strict(*members) ⇒ Object
Returns a class that is a descendant of Struct, with a strict keyword initializer.
Class Method Details
.strict(*members) ⇒ Object
Returns a class that is a descendant of Struct, with a strict keyword initializer.
Info = Ks.strict(:item_count, :weight)
data = Info.new(item_count: 1, weight: 2)
Note that all the keyword arguments defined for the class (all the members) are going to be required keyword arguments for the initializer.
The created classes (Struct descendants) are cached to make reloading easier, since when reloading a usual Struct descendant it will receive a different parent class. This is mitigated by caching the created subclasses using their member lists
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ks.rb', line 26 def self.strict(*members) k = members.sort.join(':') @@caching_mutex.synchronize do return @@predefined_structs[k] if @@predefined_structs[k] struct_ancestor = Struct.new(*members) predefined = Class.new(struct_ancestor) do class_eval <<-METHOD, __FILE__, __LINE__ + 1 def initialize(#{members.map { |a| "#{a}:" }.join(', ')}) # def initialize(bar:, baz:) super(#{members.join(', ')}) # super(bar, baz) end # end METHOD end @@predefined_structs[k] = predefined predefined end end |