Skip to content

Commit

Permalink
fixed attrs overwriting methods
Browse files Browse the repository at this point in the history
  • Loading branch information
realtradam committed Jul 9, 2021
1 parent 7f38d21 commit a90b071
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 59 deletions.
5 changes: 0 additions & 5 deletions CHANGELOG.md

This file was deleted.

18 changes: 18 additions & 0 deletions CHANGELOG.mdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ChangeLog

###### [major.minor.bugfix] - year-month-day

![Fixed](https://img.shields.io/badge/-Fixed-blue)
![Added](https://img.shields.io/badge/-Added-brightgreen)
![Changed](https://img.shields.io/badge/-Changed-yellow)
![Deprecated](https://img.shields.io/badge/-Deprecated-orange)
![Removed](https://img.shields.io/badge/-Removed-red)

## [1.0.1](https://github.com/realtradam/FelFlame/releases/tag/1.0.1) - 2021-07-09
![Fixed](https://img.shields.io/badge/-Fixed-blue)
- Defining attributes in components are no longer allowed to overwrite methods

## [1.0.0](https://github.com/realtradam/FelFlame/releases/tag/1.0.0) - 2021-07-09

![Added](https://img.shields.io/badge/-Added-brightgreen)
- Initial release
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
felflame (1.0.0)
felflame (1.0.1)

GEM
remote: https://rubygems.org/
Expand Down
21 changes: 0 additions & 21 deletions LICENSE.txt

This file was deleted.

4 changes: 4 additions & 0 deletions lib/felflame/component_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ def new(component_name, *attrs, **attrs_with_defaults)


const_set(component_name, Class.new(FelFlame::ComponentManager) {})

attrs.each do |attr|
if FelFlame::Components.const_get(component_name).method_defined?("#{attr}") || FelFlame::Components.const_get(component_name).method_defined?("#{attr}=")
raise NameError.new "The attribute name \"#{attr}\" is already a method"
end
FelFlame::Components.const_get(component_name).attr_accessor attr
end
attrs_with_defaults.each do |attr, _default|
Expand Down
2 changes: 1 addition & 1 deletion lib/felflame/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Felflame
VERSION = "1.0.0"
VERSION = "1.0.1"
end
11 changes: 10 additions & 1 deletion spec/component_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@
end

it 'cant overwrite exiting component managers' do
expect { FelFlame::Components.new('TestComponents') }.to raise_error(NameError)
FelFlame::Components.new('TestComponent1')
expect { FelFlame::Components.new('TestComponent1') }.to raise_error(NameError)
end

it 'can\'t create an attribute when its name is an existing method' do
expect { FelFlame::Components.new('TestComponent2', :id) }.to raise_error(NameError)
expect { FelFlame::Components.new('TestComponent2', :addition_triggers) }.to raise_error(NameError)
expect { FelFlame::Components.new('TestComponent2', :removal_triggers) }.to raise_error(NameError)
expect { FelFlame::Components.new('TestComponent2', :attr_triggers) }.to raise_error(NameError)
expect { FelFlame::Components.new('TestComponent3', :same, :same) }.to raise_error(NameError)
end
end
60 changes: 30 additions & 30 deletions spec/system_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe 'Components' do

before :all do
@component_manager ||= FelFlame::Components.new('TestSystems', health: 10, name: 'imp', mana: 10)
@component_manager ||= FelFlame::Components.new('TestSystems', health: 10, whatever: 'imp', mana: 10)
@@testitr = 999
end

Expand Down Expand Up @@ -156,17 +156,17 @@
it 'can trigger when a single Component\'s attribute is changed' do
@cmp0 = @component_manager.new
@cmp1 = @component_manager.new health: 20
@system.trigger_when_is_changed @cmp0, :name
@system.trigger_when_is_changed @cmp0, :whatever
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@entity0 = FelFlame::Entities.new
@entity1 = FelFlame::Entities.new @cmp0
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.name = 'different'
@cmp0.whatever = 'different'
expect(@cmp0.health).to eq(15)
expect(@cmp1.health).to eq(25)
@cmp1.name = 'different'
@cmp1.whatever = 'different'
expect(@cmp0.health).to eq(15)
expect(@cmp1.health).to eq(25)
end
Expand All @@ -179,15 +179,15 @@
@system.trigger_when_added @component_manager
@system.trigger_when_removed @cmp0
@system.trigger_when_removed @component_manager
@system.trigger_when_is_changed @cmp0, :name
@system.trigger_when_is_changed @component_manager, :name
@system.trigger_when_is_changed @cmp0, :whatever
@system.trigger_when_is_changed @component_manager, :whatever
@system.clear_triggers
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@entity1.add @cmp0, @cmp1
@entity1.remove @cmp0, @cmp1
@cmp0.name = 'something'
@cmp1.name = 'different'
@cmp0.whatever = 'something'
@cmp1.whatever = 'different'
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
end
Expand All @@ -196,15 +196,15 @@
@cmp0 = @component_manager.new health: 10, mana: 10
@cmp1 = @component_manager.new health: 20, mana: 20
@entity1 = FelFlame::Entities.new
@system.trigger_when_is_changed @cmp0, :name
@system.trigger_when_is_changed @component_manager, :name
@system.trigger_when_is_changed @cmp0, :whatever
@system.trigger_when_is_changed @component_manager, :whatever
@system.trigger_when_is_changed @cmp0, :mana
@system.trigger_when_is_changed @component_manager, :mana
@system.clear_triggers :attr_triggers, :name
@system.clear_triggers :attr_triggers, :whatever
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.name = 'something'
@cmp1.name = 'different'
@cmp0.whatever = 'something'
@cmp1.whatever = 'different'
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.mana = 15
Expand All @@ -217,17 +217,17 @@
@cmp0 = @component_manager.new health: 10, mana: 10
@cmp1 = @component_manager.new health: 20, mana: 20
@entity1 = FelFlame::Entities.new
@system.trigger_when_is_changed @cmp0, :name
@system.trigger_when_is_changed @cmp0, :whatever
@system.trigger_when_is_changed @cmp0, :mana
#expect(@system.attr_triggers).to eq({@cmp0 => [:name, :mana]})
#expect(@cmp0.attr_triggers).to eq({:name => [@system], :mana => [@system]})
@system.clear_triggers :attr_triggers, :name, component_or_manager: @cmp0
@system.clear_triggers :attr_triggers, :whatever, component_or_manager: @cmp0
#expect(@system.attr_triggers).to eq({@cmp0 => [:mana]})
#expect(@cmp0.attr_triggers).to eq({:name => [], :mana => [@system]})
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.name = 'something'
@cmp1.name = 'different'
@cmp0.whatever = 'something'
@cmp1.whatever = 'different'
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.mana = 15
Expand All @@ -240,13 +240,13 @@
@cmp0 = @component_manager.new health: 10, mana: 10
@cmp1 = @component_manager.new health: 20, mana: 20
@entity1 = FelFlame::Entities.new
@system.trigger_when_is_changed @component_manager, :name
@system.trigger_when_is_changed @component_manager, :whatever
@system.trigger_when_is_changed @component_manager, :mana
@system.clear_triggers :attr_triggers, :name, component_or_manager: @component_manager
@system.clear_triggers :attr_triggers, :whatever, component_or_manager: @component_manager
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.name = 'something'
@cmp1.name = 'different'
@cmp0.whatever = 'something'
@cmp1.whatever = 'different'
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.mana = 15
Expand All @@ -259,13 +259,13 @@
@cmp0 = @component_manager.new health: 10, mana: 10
@cmp1 = @component_manager.new health: 20, mana: 20
@entity1 = FelFlame::Entities.new
@system.trigger_when_is_changed @component_manager, :name
@system.trigger_when_is_changed @component_manager, :whatever
@system.trigger_when_is_changed @cmp1, :mana
@system.clear_triggers :attr_triggers
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.name = 'something'
@cmp1.name = 'different'
@cmp0.whatever = 'something'
@cmp1.whatever = 'different'
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.mana = 15
Expand All @@ -278,13 +278,13 @@
@cmp0 = @component_manager.new health: 10, mana: 10
@cmp1 = @component_manager.new health: 20, mana: 20
@entity1 = FelFlame::Entities.new
@system.trigger_when_is_changed @component_manager, :name
@system.trigger_when_is_changed @component_manager, :whatever
@system.trigger_when_is_changed @cmp1, :mana
@system.clear_triggers :attr_triggers, component_or_manager: @cmp1
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.name = 'something'
@cmp1.name = 'different'
@cmp0.whatever = 'something'
@cmp1.whatever = 'different'
expect(@cmp0.health).to eq(20)
expect(@cmp1.health).to eq(30)
@cmp0.mana = 15
Expand All @@ -297,13 +297,13 @@
@cmp0 = @component_manager.new health: 10, mana: 10
@cmp1 = @component_manager.new health: 20, mana: 20
@entity1 = FelFlame::Entities.new
@system.trigger_when_is_changed @component_manager, :name
@system.trigger_when_is_changed @component_manager, :whatever
@system.trigger_when_is_changed @cmp1, :mana
@system.clear_triggers :attr_triggers, component_or_manager: @component_manager
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.name = 'something'
@cmp1.name = 'different'
@cmp0.whatever = 'something'
@cmp1.whatever = 'different'
expect(@cmp0.health).to eq(10)
expect(@cmp1.health).to eq(20)
@cmp0.mana = 15
Expand Down

0 comments on commit a90b071

Please sign in to comment.