Class: FunctionalLightService::EnumBuilder::DataType

Inherits:
Object
  • Object
show all
Defined in:
lib/functional-light-service/functional/enum.rb

Defined Under Namespace

Modules: AnyEnum, Binary, Nullary

Class Method Summary collapse

Class Method Details

.create(parent, args) ⇒ Object

rubocop:disable Metrics/MethodLength



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/functional-light-service/functional/enum.rb', line 90

def self.create(parent, args)
  if args.include? :value
    raise ArgumentError, "#{args} may not contain the reserved name :value"
  end

  dt = Class.new(parent)

  dt.instance_eval do
    public_class_method :new
    include AnyEnum

    define_method(:args) { args }

    define_method(:parent) { parent }
    private :parent
  end

  case args.count
  when 0
    dt.instance_eval do
      include Nullary

      private :value
    end
  when 1
    dt.instance_eval do
      define_method(args[0].to_sym) { value }
    end
  else
    dt.instance_eval do
      include Binary

      args.each do |m|
        define_method(m) do
          @value[m]
        end
      end
    end
  end

  dt
end