11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/kweerie/result_class_generator.rb', line 11
def self.generate(parent_class, attribute_names)
Class.new(parent_class) do
include Comparable
include ResultClassComponents::Accessors
include ResultClassComponents::Comparison
include ResultClassComponents::KeyTransformation
include ResultClassComponents::Serialization
include ResultClassComponents::TypeCasting
attribute_names.each { |name| attr_reader name }
define_method :initialize do |attrs|
cast_definitions = parent_class.cast_definitions
@_raw_original_attributes = attrs.dup
@_original_attributes = attrs.each_with_object({}) do |(key, value), hash|
type_definition = cast_definitions[key.to_sym]
casted_value = type_cast_value(value, type_definition)
hash[key.to_s] = casted_value
instance_variable_set("@#{key}", casted_value)
end
super() if defined?(super)
end
define_method :inspect do
attrs = attribute_names.map do |name|
"#{name}=#{instance_variable_get("@#{name}").inspect}"
end.join(" ")
"#<#{self.class.superclass.name} #{attrs}>"
end
define_method(:attribute_names) { attribute_names }
end
end
|