Module: Micro::Struct::Factory::CreateStruct::ClassScope

Defined in:
lib/micro/struct/factory/create_struct.rb

Class Method Summary collapse

Class Method Details

.def_features(struct, features) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/micro/struct/factory/create_struct.rb', line 53

def self.def_features(struct, features)
  struct.class_eval("    class << self\n      attr_accessor :__features__\n\n      alias features __features__\n    end\n  RUBY\n\n  struct.__features__ = features\n\n  struct.send(:private_class_method, :__features__=)\nend\n", __FILE__, __LINE__ + 1)

.def_new(struct, members) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/micro/struct/factory/create_struct.rb', line 34

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("    class << self\n      undef_method :new\n\n      def new(\#{members.to_eval.keyword_args})                         # def new(a:, b:, c: nil) do\n        instance = allocate                                            #   instance = allocate\n        instance.send(:initialize, \#{members.to_eval.positional_args}) #   instance.send(:initialize, a, b, c)\n        instance                                                       #   instance\n      end                                                              # end\n\n      alias __new__ new\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1)

.def_private_writers(struct) ⇒ Object



75
76
77
78
# File 'lib/micro/struct/factory/create_struct.rb', line 75

def self.def_private_writers(struct)
  struct.send(:private, :[]=)
  struct.members.each { |member| struct.send(:private, "#{member}=") }
end

.def_to_proc(struct) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/micro/struct/factory/create_struct.rb', line 67

def self.def_to_proc(struct)
  struct.class_eval("    def self.to_proc\n      ->(hash) { new(**hash) }\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1)

.evaluate(struct, block) ⇒ Object



80
81
82
# File 'lib/micro/struct/factory/create_struct.rb', line 80

def self.evaluate(struct, block)
  struct.class_eval(&block) if block
end