Module: FinerStruct::Named

Defined in:
lib/finer_struct/named.rb

Class Method Summary collapse

Class Method Details

.build_class(attribute_names, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/finer_struct/named.rb', line 4

def self.build_class(attribute_names, &block)
  Class.new do
    define_method(:attribute_names, -> { attribute_names })

    def initialize(attributes = {})
      attributes.each_pair do |name, value|
        unless attribute_names.include?(name)
          raise(ArgumentError, "no such attribute: #{name}")
        end
        instance_variable_set("@#{name}", value)
      end
    end

    instance_eval(&block)
  end
end