From 8de3d39ef1206440692ade4bf163bfa849c06339 Mon Sep 17 00:00:00 2001 From: Luke Hill <20105237+luke-hill@users.noreply.github.com> Date: Wed, 17 Jan 2024 16:00:47 +0000 Subject: [PATCH] Feature/add builder logic (#7) * Add tdd variant of #add_any_helper * Add changelog --- CHANGELOG.md | 2 ++ lib/testing_record/dsl.rb | 1 + lib/testing_record/dsl/builder.rb | 3 ++ lib/testing_record/dsl/builder/helpers.rb | 32 +++++++++++++++++++ .../dsl/builder/helpers_spec.rb | 31 ++++++++++++++++++ .../dsl/validation/input_spec.rb | 4 +-- 6 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 lib/testing_record/dsl/builder.rb create mode 100644 lib/testing_record/dsl/builder/helpers.rb create mode 100644 spec/testing_record/dsl/builder/helpers_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 00f90ab..74278a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Added - `.caching` option for generating the iVar and reader on the class - Validators `#type_valid?` and `#caching_valid?` are enabled for checking the inputs on the model +- Properties can now be stored on the Model (All properties can be queried also) +- First helper added to builder logic - the `#any?` helper that will detect if anything is present ## [0.2.0] - 2023-12-20 ### Changed diff --git a/lib/testing_record/dsl.rb b/lib/testing_record/dsl.rb index f5fc8c2..6279b95 100644 --- a/lib/testing_record/dsl.rb +++ b/lib/testing_record/dsl.rb @@ -1,3 +1,4 @@ # frozen_string_literal: true +require_relative 'dsl/builder' require_relative 'dsl/validation' diff --git a/lib/testing_record/dsl/builder.rb b/lib/testing_record/dsl/builder.rb new file mode 100644 index 0000000..f057d84 --- /dev/null +++ b/lib/testing_record/dsl/builder.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +require_relative 'builder/helpers' diff --git a/lib/testing_record/dsl/builder/helpers.rb b/lib/testing_record/dsl/builder/helpers.rb new file mode 100644 index 0000000..22a00f5 --- /dev/null +++ b/lib/testing_record/dsl/builder/helpers.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module TestingRecord + module DSL + module Builder + # [TestingRecord::DSL::Builder::Helpers] + # Ways in which we can build in extra helper methods from building requests + module Helpers + # Add the boolean helper which will perform the `#any?` check on your instance + # + # @return [TestingRecord::Model] + def add_any_helper(name) + define_method(:"#{name}?") do + instance_variable_get(:"@#{name}").any? + end + end + + # Check whether the type setting is valid + # + # @return [Boolean] + def type_valid?(input) + type_validations.include?(input) + end + + private + + def caching_validations = %i[enabled disabled] + def type_validations = %i[singular plural] + end + end + end +end diff --git a/spec/testing_record/dsl/builder/helpers_spec.rb b/spec/testing_record/dsl/builder/helpers_spec.rb new file mode 100644 index 0000000..f91b891 --- /dev/null +++ b/spec/testing_record/dsl/builder/helpers_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +RSpec.describe TestingRecord::DSL::Builder::Helpers do + subject(:klazz) do + Class.new do + extend TestingRecord::DSL::Builder::Helpers + end + end + + describe '.add_any_helper' do + subject(:instance) { klazz.new } + + before { klazz.add_any_helper(:foo) } + + it 'creates a boolean helper with the desired name' do + expect(instance).to respond_to(:foo?) + end + + it 'is `true` when the iVar corresponding to the name is not empty' do + instance.instance_variable_set(:@foo, [1]) + + expect(instance.foo?).to be true + end + + it 'is `false` when the iVar corresponding to the name is empty' do + instance.instance_variable_set(:@foo, []) + + expect(instance.foo?).to be false + end + end +end diff --git a/spec/testing_record/dsl/validation/input_spec.rb b/spec/testing_record/dsl/validation/input_spec.rb index 8478b65..26a421f 100644 --- a/spec/testing_record/dsl/validation/input_spec.rb +++ b/spec/testing_record/dsl/validation/input_spec.rb @@ -7,7 +7,7 @@ end end - describe '#caching_valid?' do + describe '.caching_valid?' do it 'is `true` when the type is enabled' do expect(klazz.caching_valid?(:enabled)).to be true end @@ -21,7 +21,7 @@ end end - describe '#type_valid?' do + describe '.type_valid?' do it 'is `true` when the type is singular' do expect(klazz.type_valid?(:singular)).to be true end