Skip to content

Commit

Permalink
Implement experimentally singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
yujideveloper committed Sep 14, 2023
1 parent 5fbd957 commit 96b8485
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/rekkyo/type.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "set"
require "singleton"
require "forwardable"

module Rekkyo
module Type
Expand All @@ -12,6 +14,32 @@ def included(klass)
klass.extend(DSLMethods)
klass.extend(EnumMethods)
end

def create(member_names)
::Class.new do
include ::Singleton
extend ::SingleForwardable

include EnumMethods
def_single_delegators :instance, :all, :members, :freeze

const_set(:Member, ::Class.new(Member))

define_method :initialize do
member_class = self.class.const_get(:Member, false)
@members = ::Set.new
member_names.each do |member_name|
member = member_class.new(member_name.upcase.to_sym, member_name).freeze
@members << member
self.class.const_set(member.key, member)
end
@members.freeze
end

instance # define a class and instantiate it
freeze
end
end
end
end
end
Expand Down

0 comments on commit 96b8485

Please sign in to comment.