Skip to content

Commit

Permalink
Fixed issue where subclass with no attr_accessor
Browse files Browse the repository at this point in the history
Subclasses with no attr_accessor_type could not retrieve fields set by
class kit in parent classes
  • Loading branch information
dephraser committed Jun 28, 2017
1 parent 2ae32fe commit eea9cf4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/class_kit/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
10 changes: 10 additions & 0 deletions spec/class_kit/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion spec/class_kit/test_objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end

class TestEmptyChild < TestParent
extend ClassKit
end

0 comments on commit eea9cf4

Please sign in to comment.