Class: DataClass::Factory
- Inherits:
-
Object
- Object
- DataClass::Factory
- Defined in:
- lib/data_class/factory.rb
Overview
An internal class for providing implementation of ‘Data.define`.
Instance Method Summary collapse
- #create(parent_class:, &block) ⇒ Class<Data>
-
#initialize(attribute_names) ⇒ Factory
constructor
A new instance of Factory.
Constructor Details
#initialize(attribute_names) ⇒ Factory
Returns a new instance of Factory.
7 8 9 |
# File 'lib/data_class/factory.rb', line 7 def initialize(attribute_names) @definition = Definition.new(attribute_names) end |
Instance Method Details
#create(parent_class:, &block) ⇒ Class<Data>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/data_class/factory.rb', line 13 def create(parent_class:, &block) attribute_names = @definition.attribute_names # defines a subclass of Data. Class.new(parent_class) do public_class_method :new private_class_method :define attribute_names.each { |key| define_method(key) { @data[key] } } define_singleton_method(:members) { attribute_names } class_eval(&block) unless block.nil? end end |