Skip to content

Commit

Permalink
Feature/add property (#6)
Browse files Browse the repository at this point in the history
* Add TDD form for property

* Add working code to add a property to the model

* Add model spec and lib for property
  • Loading branch information
luke-hill committed Jan 17, 2024
1 parent b4b3c38 commit efe3807
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
22 changes: 22 additions & 0 deletions lib/testing_record/model.rb
Expand Up @@ -20,12 +20,34 @@ def caching(option)
define_singleton_method(cache_name) { instance_variable_get(ivar_name) }
end

# Creates an instance of the model, adding it to the cache if caching is enabled
#
# @return [TestingRecord::Model]
def create(attributes = {})
new(attributes).tap do |entity|
add_to_cache(entity) if respond_to?(cache_name)
end
end

# Sets a property on the model, this should have a name and an optional type (Defaults to `:singular`)
#
# @return [Array<Hash>]
def property(name, type: :singular)
raise Error, 'Invalid type option, must be :singular or :plural' unless type_valid?(type)

if type == :plural
attr_reader :"#{name}s"
else
attr_reader name
end

properties << { name:, type: }
end

def properties
@properties ||= []
end

# Set the type of model, this should be one of `:singular` or `:plural`
#
# @return [Symbol]
Expand Down
66 changes: 63 additions & 3 deletions spec/testing_record/model_spec.rb
Expand Up @@ -60,11 +60,11 @@
end

it 'generates a new instance of the model entity' do
expect(Foo.create({})).to be_a Foo
expect(Foo.create).to be_a Foo
end

it 'will add the entity to the cache' do
expect { Foo.create({}) }.to change(Foo.foos, :length).by(1)
expect { Foo.create }.to change(Foo.foos, :length).by(1)
end
end

Expand All @@ -75,7 +75,7 @@
end

it 'generates a new instance of the model entity' do
expect(Foo.create({})).to be_a Foo
expect(Foo.create).to be_a Foo
end

it 'does not generate a cache add the entity to the cache' do
Expand All @@ -93,4 +93,64 @@
end
end
end

describe '.property' do
context 'when not classified' do
let(:model) do
Class.new(TestingRecord::Model) do
property :bar
end
end

it 'sets the property as a method on the model instance' do
expect(model.create).to respond_to(:bar)
end

it 'stores the property as a singular type on the model' do
expect(model.properties).to include({ name: :bar, type: :singular })
end
end

context 'when classified as singular' do
let(:model) do
Class.new(TestingRecord::Model) do
property :bar, type: :singular
end
end

it 'sets the property as a method on the model instance' do
expect(model.create).to respond_to(:bar)
end

it 'stores the property as a singular type on the model' do
expect(model.properties).to include({ name: :bar, type: :singular })
end
end

context 'when classified as plural' do
let(:model) do
Class.new(TestingRecord::Model) do
property :bar, type: :plural
end
end

it 'sets the property as a method on the model instance' do
expect(model.create).to respond_to(:bars)
end

it 'stores the property as a singular type on the model' do
expect(model.properties).to include({ name: :bar, type: :plural })
end
end

context 'with an invalid type setting' do
before do
stub_const('Foo', Class.new(described_class))
end

it 'cannot be configured on the model' do
expect { Foo.property :bar, type: :invalid }.to raise_error(TestingRecord::Error)
end
end
end
end

0 comments on commit efe3807

Please sign in to comment.