Module: Micro::Struct::Factory::CreateStruct::ClassScope
- Defined in:
- lib/micro/struct/factory/create_struct.rb
Class Method Summary collapse
- .def_features(struct, features) ⇒ Object
- .def_new(struct, members) ⇒ Object
- .def_private_writers(struct) ⇒ Object
- .def_to_proc(struct) ⇒ Object
- .evaluate(struct, block) ⇒ Object
Class Method Details
.def_features(struct, features) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/micro/struct/factory/create_struct.rb', line 46 def self.def_features(struct, features) struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1) class << self attr_accessor :__features__ alias features __features__ end RUBY struct.__features__ = features struct.send(:private_class_method, :__features__=) end |
.def_new(struct, members) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/micro/struct/factory/create_struct.rb', line 27 def self.def_new(struct, members) # The .new() method will require all required keyword arguments. # We are doing this because the Struct constructor keyword init option treats everything as optional. # struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1) class << self undef_method :new def new(#{members.to_eval.keyword_args}) # def new(a:, b:, c: nil) do instance = allocate # instance = allocate instance.send(:initialize, #{members.to_eval.positional_args}) # instance.send(:initialize, a, b, c) instance # instance end # end alias __new__ new end RUBY end |
.def_private_writers(struct) ⇒ Object
68 69 70 71 |
# File 'lib/micro/struct/factory/create_struct.rb', line 68 def self.def_private_writers(struct) struct.send(:private, :[]=) struct.send(:private, *struct.members.map { |member| "#{member}=" }) end |
.def_to_proc(struct) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/micro/struct/factory/create_struct.rb', line 60 def self.def_to_proc(struct) struct.class_eval(<<-RUBY, __FILE__, __LINE__ + 1) def self.to_proc ->(hash) { new(**hash) } end RUBY end |
.evaluate(struct, block) ⇒ Object
73 74 75 |
# File 'lib/micro/struct/factory/create_struct.rb', line 73 def self.evaluate(struct, block) struct.class_eval(&block) if block end |