Skip to content

Commit 4142827

Browse files
author
Michael Crismali
committed
refactored ClassBuilder.generate_class into private methods
1 parent f8f1957 commit 4142827

File tree

1 file changed

+61
-35
lines changed

1 file changed

+61
-35
lines changed

lib/jsonb_accessor/class_builder.rb

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,44 @@ module ClassBuilder
66
class << self
77
def generate_class(namespace, new_class_name, attribute_definitions)
88
fields_map = JsonbAccessor::FieldsMap.new([], attribute_definitions)
9-
10-
klass = Class.new(NestedBase)
11-
new_class_name_camelized = "#{CLASS_PREFIX}#{new_class_name.to_s.camelize}"
12-
namespace.const_set(new_class_name_camelized, klass)
9+
klass = generate_new_class(new_class_name, fields_map, namespace)
1310
nested_classes = generate_nested_classes(klass, fields_map.nested_fields)
1411

15-
klass.class_eval do
16-
singleton_class.send(:define_method, :nested_classes) { nested_classes }
17-
singleton_class.send(:define_method, :attribute_on_parent_name) { new_class_name }
12+
define_class_methods(klass, nested_classes, new_class_name)
13+
define_attributes_and_data_types(klass, fields_map)
14+
define_typed_accessors(klass, fields_map)
15+
define_nested_accessors(klass, fields_map)
1816

19-
define_method(:attributes_and_data_types) do
20-
@attributes_and_data_types ||= fields_map.typed_fields.each_with_object({}) do |(name, type), attrs_and_data_types|
21-
attrs_and_data_types[name] = TypeHelper.fetch(type)
22-
end
23-
end
17+
klass
18+
end
2419

25-
fields_map.typed_fields.keys.each do |attribute_name|
26-
define_method(attribute_name) { attributes[attribute_name] }
20+
def generate_nested_classes(klass, nested_attributes)
21+
nested_attributes.each_with_object({}) do |(attribute_name, nested_attrs), nested_classes|
22+
nested_classes[attribute_name] = generate_class(klass, attribute_name, nested_attrs)
23+
end
24+
end
2725

28-
define_method("#{attribute_name}=") do |value|
29-
cast_value = attributes_and_data_types[attribute_name].type_cast_from_user(value)
30-
attributes[attribute_name] = cast_value
31-
update_parent
32-
end
33-
end
26+
def generate_class_namespace(name)
27+
class_name = CLASS_PREFIX + name.gsub(CONSTANT_SEPARATOR, "")
28+
if JsonbAccessor.constants.any? { |c| c.to_s == class_name }
29+
class_namespace = JsonbAccessor.const_get(class_name)
30+
else
31+
class_namespace = Module.new
32+
JsonbAccessor.const_set(class_name, class_namespace)
33+
end
34+
class_namespace
35+
end
36+
37+
def generate_attribute_namespace(attribute_name, class_namespace)
38+
attribute_namespace = Module.new
39+
name = generate_constant_name(attribute_name)
40+
class_namespace.const_set(name, attribute_namespace)
41+
end
42+
43+
private
3444

45+
def define_nested_accessors(klass, fields_map)
46+
klass.class_eval do
3547
fields_map.nested_fields.keys.each do |attribute_name|
3648
attr_reader attribute_name
3749

@@ -45,29 +57,43 @@ def generate_class(namespace, new_class_name, attribute_definitions)
4557
end
4658
end
4759
end
48-
klass
4960
end
5061

51-
def generate_nested_classes(klass, nested_attributes)
52-
nested_attributes.each_with_object({}) do |(attribute_name, nested_attrs), nested_classes|
53-
nested_classes[attribute_name] = generate_class(klass, attribute_name, nested_attrs)
62+
def define_typed_accessors(klass, fields_map)
63+
klass.class_eval do
64+
fields_map.typed_fields.keys.each do |attribute_name|
65+
define_method(attribute_name) { attributes[attribute_name] }
66+
67+
define_method("#{attribute_name}=") do |value|
68+
cast_value = attributes_and_data_types[attribute_name].type_cast_from_user(value)
69+
attributes[attribute_name] = cast_value
70+
update_parent
71+
end
72+
end
5473
end
5574
end
5675

57-
def generate_class_namespace(name)
58-
class_name = CLASS_PREFIX + name.gsub(CONSTANT_SEPARATOR, "")
59-
if JsonbAccessor.constants.any? { |c| c.to_s == class_name }
60-
class_namespace = JsonbAccessor.const_get(class_name)
61-
else
62-
class_namespace = Module.new
63-
JsonbAccessor.const_set(class_name, class_namespace)
76+
def define_attributes_and_data_types(klass, fields_map)
77+
klass.send(:define_method, :attributes_and_data_types) do
78+
@attributes_and_data_types ||= fields_map.typed_fields.each_with_object({}) do |(name, type), attrs_and_data_types|
79+
attrs_and_data_types[name] = TypeHelper.fetch(type)
80+
end
6481
end
65-
class_namespace
6682
end
6783

68-
def generate_attribute_namespace(attribute_name, class_namespace)
69-
attribute_namespace = Module.new
70-
class_namespace.const_set("#{CLASS_PREFIX}#{attribute_name.to_s.camelize}", attribute_namespace)
84+
def define_class_methods(klass, nested_classes, attribute_name)
85+
klass.singleton_class.send(:define_method, :nested_classes) { nested_classes }
86+
klass.singleton_class.send(:define_method, :attribute_on_parent_name) { attribute_name }
87+
end
88+
89+
def generate_new_class(new_class_name, fields_map, namespace)
90+
klass = Class.new(NestedBase)
91+
new_class_name_camelized = generate_constant_name(new_class_name)
92+
namespace.const_set(new_class_name_camelized, klass)
93+
end
94+
95+
def generate_constant_name(attribute_name)
96+
"#{CLASS_PREFIX}#{attribute_name.to_s.camelize}"
7197
end
7298
end
7399
end

0 commit comments

Comments
 (0)