Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/collection props #3

Merged
merged 10 commits into from
Dec 21, 2023
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--format documentation
--color
--order random
--require spec_helper
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
## [Unreleased]
### Added
- Validator `#type_valid?` for checking the initial type of a model
- `.caching` option for generating the iVar and reader on the class

## [0.2.0] - 2023-12-20

### Changed
- Attempt to clean up all code

## [0.1.0] - 2023-12-19

- Gem created release
### Added
- Initial gem creation

[Unreleased]: https://github.com/site-prism/testingrecord/compare/v0.2...HEAD
[0.2.0]: https://github.com/site-prism/testingrecord/compare/0.1...v0.2
Expand Down
4 changes: 3 additions & 1 deletion lib/testing_record/dsl.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
require_relative 'dsl/validation'
# frozen_string_literal: true

require_relative 'dsl/validation'
2 changes: 2 additions & 0 deletions lib/testing_record/dsl/validation.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require_relative 'validation/input'
3 changes: 3 additions & 0 deletions lib/testing_record/dsl/validation/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ module Validation
# [TestingRecord::DSL::Validation::Input]
# Validations for direct inputs into creating models
module Input
# Check whether the type is valid
#
# @return [Boolean]
def type_valid?(input)
type_validations.include?(input)
end
Expand Down
40 changes: 40 additions & 0 deletions lib/testing_record/model.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
# frozen_string_literal: true

require 'automation_helpers/extensions/string'

module TestingRecord
# The top level Model. Most of the behaviours specified here are fairly rudimentary ones that will then
# include other behaviour(s), from the included modules
class Model
class << self
# Create a cache of the entities, named according to the classname
#
# @return [Symbol]
def caching(option)
return unless option == :enabled

instance_variable_set(ivar_name, [])
define_singleton_method(cache_name) { instance_variable_get(ivar_name) }
end

def create(attributes = {})
new(attributes).tap do |entity|
add_to_cache(entity) if respond_to?(cache_name)
end
end

# Set the type of model, this should be one of `:singular` or `:plural`
#
# @return [Symbol]
def type(type)
@type = type
end

private

def add_to_cache(entity)
# TODO: Cache entity as the current entity for model class
send(cache_name) << entity
# TODO: Add log message (Requires adding logger)
end

def cache_name
:"#{to_s.snake_case}s"
end

def ivar_name
"@#{to_s.snake_case}s"
end
end

attr_reader :attributes

def initialize(attributes)
@attributes = attributes
end
end
end
8 changes: 0 additions & 8 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,5 @@
require 'testing_record'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'

# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!

config.expect_with :rspec do |c|
c.syntax = :expect
end
end
68 changes: 56 additions & 12 deletions spec/testing_record/model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,71 @@
# frozen_string_literal: true

RSpec.describe TestingRecord::Model do
context 'when classified as a singular model' do
let(:model) do
Class.new(TestingRecord::Model) do
type :singular
describe '.type' do
context 'when classified as a singular model' do
let(:model) do
Class.new(TestingRecord::Model) do
type :singular
end
end

it 'sets the singular model properties' do
expect(model.instance_variable_get(:@type)).to eq(:singular)
end
end

it 'sets the singular model properties' do
expect(model.instance_variable_get(:@type)).to eq(:singular)
context 'when classified as a plural model' do
let(:model) do
Class.new(TestingRecord::Model) do
type :plural
end
end

it 'sets the plural model properties' do
expect(model.instance_variable_get(:@type)).to eq(:plural)
end
end
end

context 'when classified as a plural model' do
let(:model) do
Class.new(TestingRecord::Model) do
type :plural
describe '.caching' do
before do
stub_const('Foo', Class.new(described_class))
Foo.caching :enabled
end

it 'generates a new reader class method for accessing the raw data' do
expect(Foo).to respond_to(:foos)
end

it 'automatically creates the cache as an empty array' do
expect(Foo.foos).to eq([])
end
end

describe '.create' do
context 'with caching enabled' do
before do
stub_const('Foo', Class.new(described_class))
Foo.caching :enabled
end

it 'generates a new instance of the model entity' do
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)
end
end

it 'sets the singular model properties' do
expect(model.instance_variable_get(:@type)).to eq(:plural)
context 'without caching enabled' do
before do
stub_const('Foo', Class.new(described_class))
end

it 'generates a new instance of the model entity' do
expect(Foo.create({})).to be_a Foo
end
end
end
end
2 changes: 2 additions & 0 deletions testingrecord.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Gem::Specification.new do |spec|
spec.files = Dir.glob('lib/**/*') + %w[LICENSE.md README.md]
spec.require_paths = ['lib']

spec.add_dependency 'automation_helpers', '~> 5.0'

spec.add_development_dependency 'rspec', '~> 3.12'
spec.add_development_dependency 'rubocop', '~> 1.59.0'
spec.add_development_dependency 'rubocop-performance', '~> 1.20.0'
Expand Down