-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from site-prism/feature/add_basic_collection_pr…
…operties feature/collection props
- Loading branch information
Showing
9 changed files
with
113 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--format documentation | ||
--color | ||
--order random | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'validation/input' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters