diff --git a/lib/class_kit/class_methods.rb b/lib/class_kit/class_methods.rb index 860ce14..e292b1b 100644 --- a/lib/class_kit/class_methods.rb +++ b/lib/class_kit/class_methods.rb @@ -9,16 +9,17 @@ def attr_accessor_type(name, type: nil, collection_type: nil, allow_nil: true, d attributes = instance_variable_get(:@class_kit_attributes) attributes[name] = { name: name, type: type, collection_type: collection_type, allow_nil: allow_nil, - default: default, auto_init: auto_init, meta: meta } + default: default, auto_init: auto_init, meta: meta } class_eval do define_method name do - cka = self.class.instance_variable_get(:@class_kit_attributes)[name] + + cka = ClassKit::AttributeHelper.instance.get_attribute(klass: self.class, name: name) current_value = instance_variable_get(:"@#{name}") if current_value.nil? - if cka[:default] != nil + if !cka[:default].nil? current_value = instance_variable_set(:"@#{name}", cka[:default]) elsif cka[:auto_init] current_value = instance_variable_set(:"@#{name}", cka[:type].new) @@ -31,20 +32,20 @@ def attr_accessor_type(name, type: nil, collection_type: nil, allow_nil: true, d class_eval do define_method "#{name}=" do |value| - #get the attribute meta data + # get the attribute meta data cka = ClassKit::AttributeHelper.instance.get_attribute(klass: self.class, name: name) - #verify if the attribute is allowed to be set to nil + # verify if the attribute is allowed to be set to nil if value.nil? && cka[:allow_nil] == false - raise ClassKit::Exceptions::InvalidAttributeValueError.new("Attribute: #{name}, must not be nil.") + raise ClassKit::Exceptions::InvalidAttributeValueError, "Attribute: #{name}, must not be nil." end - #check if the value being set is not of the specified type and should attempt to parse the value + # check if the value being set is not of the specified type and should attempt to parse the value if !cka[:type].nil? && !value.nil? && (cka[:type] == :bool || !value.is_a?(cka[:type])) begin value = ClassKit::ValueHelper.instance.parse(type: cka[:type], value: value) rescue => e - raise ClassKit::Exceptions::InvalidAttributeValueError.new("Attribute: #{name}, must be of type: #{cka[:type]}. Error: #{e}") + raise ClassKit::Exceptions::InvalidAttributeValueError, "Attribute: #{name}, must be of type: #{cka[:type]}. Error: #{e}" end end diff --git a/spec/class_kit/class_methods_spec.rb b/spec/class_kit/class_methods_spec.rb index 07d7493..4fcda1a 100644 --- a/spec/class_kit/class_methods_spec.rb +++ b/spec/class_kit/class_methods_spec.rb @@ -9,12 +9,22 @@ TestParent.new end + let(:empty_child_entity) do + TestEmptyChild.new + end + context 'when setting the value of a base class attribute' do it 'should not error' do child_entity.base1 = 'hello world' end end + context 'when a child entity is empty' do + it 'class_kit_attributes are avaiable' do + expect{ empty_child_entity.base1 }.not_to raise_error + end + end + describe '#attr_accessor_type' do it 'should create the attribute' do expect(test_entity.respond_to?(:int)).to be true diff --git a/spec/class_kit/test_objects.rb b/spec/class_kit/test_objects.rb index b44aeeb..5e166cb 100644 --- a/spec/class_kit/test_objects.rb +++ b/spec/class_kit/test_objects.rb @@ -36,13 +36,20 @@ class TestParent attr_accessor_type :base2 end class TestChild < TestParent + extend ClassKit attr_accessor_type :child1, type: String attr_accessor_type :child2 end class TestChild2 < TestParent + extend ClassKit attr_accessor_type :text, type: String end class TestChild3 < TestParent + extend ClassKit attr_accessor_type :text1, type: String attr_accessor_type :text2, type: String -end \ No newline at end of file +end + +class TestEmptyChild < TestParent + extend ClassKit +end