Module: Ks

Defined in:
lib/ks.rb

Overview

"Ks" - as in "kiss" - a generator of keyworded Structs.

Constant Summary collapse

VERSION =
'0.0.2'
@@caching_mutex =
Mutex.new
@@predefined_structs =
{}

Class Method Summary collapse

Class Method Details

.allowing_unknown(*members) ⇒ Object

Returns a class that is a descendant of Struct, with a keyword initializer that permits unknown keywords to be passed in. Those keywords will be dropped. The keywords that are known at definition time will be checked for presence. This allows you to use structs for API responses and payloads that might get additional properties as the API evolves, without breaking (your) consuming code. Imagine at time of designing your structures you specify a Shipment:

Shipment = Ks.allowing_unknown(:sku, :weight)

The API you are using, however, later adds a "shipping_company_id" property. If you had used strict your struct would fail to initialize, since it does not know about the shipping_company_id attribute.

Shipment.new(JSON.parse(payload)) #=> ArgumentError...

but as you have used allowing_unknown the "shipping_company_id" property will be silently dropped instead.

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

Parameters:

  • members (Array<Symbol>)

    the names of members to create. Those members will be required, just like with Ks.strict



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ks.rb', line 73

def self.allowing_unknown(*members)
  k = 'with_optionals:' + 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 "        def initialize(\#{members.map { |a| \"\#{a}:\" }.join(', ')}, **) # def initialize(bar:, baz:, **)\n          super(\#{members.join(', ')})                                #   super(bar, baz)\n        end                                                           # end\n      METHOD\n    end\n    @@predefined_structs[k] = predefined\n    predefined\n  end\nend\n", __FILE__, __LINE__ + 1

.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

Parameters:

  • members (Array<Symbol>)

    the names of members to create



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ks.rb', line 28

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 "        def initialize(\#{members.map { |a| \"\#{a}:\" }.join(', ')}) # def initialize(bar:, baz:)\n          super(\#{members.join(', ')})                            #   super(bar, baz)\n        end                                                       # end\n      METHOD\n    end\n    @@predefined_structs[k] = predefined\n    predefined\n  end\nend\n", __FILE__, __LINE__ + 1