Skip to content

Commit

Permalink
Feature/add builder logic (#7)
Browse files Browse the repository at this point in the history
* Add tdd variant of #add_any_helper

* Add changelog
  • Loading branch information
luke-hill committed Jan 17, 2024
1 parent efe3807 commit 8de3d39
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/testing_record/dsl.rb
@@ -1,3 +1,4 @@
# frozen_string_literal: true

require_relative 'dsl/builder'
require_relative 'dsl/validation'
3 changes: 3 additions & 0 deletions lib/testing_record/dsl/builder.rb
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative 'builder/helpers'
32 changes: 32 additions & 0 deletions 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
31 changes: 31 additions & 0 deletions 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
4 changes: 2 additions & 2 deletions spec/testing_record/dsl/validation/input_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8de3d39

Please sign in to comment.