Skip to content

Commit

Permalink
Add support for inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Feb 22, 2023
1 parent 5b17caf commit 2ff1c58
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

## [0.1.1] - 2023-02-22

- Add support for inheritance

## [0.1.0] - 2023-02-21

- Initial release
21 changes: 19 additions & 2 deletions lib/shale/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,21 @@ module Shale
#
module Builder
class << self
# Gets called after including this module.
# Gets called after including this module in a module or class.
#
# @param mod [Module]
# @param mod [Module, Class]
# @return [void]
def included(mod)
mod.extend ClassMethods
Builder.prepare_mod(mod)
end

# Prepares the received module or class
# for dynamic method definition.
#
# @param mod [Module, Class]
# @return [void]
def prepare_mod(mod)
builder_methods_module = ::Module.new
mod.instance_variable_set :@builder_methods_module, builder_methods_module
mod.include builder_methods_module
Expand All @@ -51,6 +61,13 @@ def included(mod)

# Class methods provided by `Shale::Builder`
module ClassMethods
# @param subclass [Class]
# @return [void]
def inherited(subclass)
super
Builder.prepare_mod(subclass)
end

# Contains overridden getter methods for attributes
# with complex types (so that they accept a block for building)
#
Expand Down
1 change: 1 addition & 0 deletions shale-builder.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Gem::Specification.new do |spec|

spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['changelog_uri'] = 'https://github.com/Verseth/ruby-shale-builder/blob/main/CHANGELOG.md'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand Down
55 changes: 54 additions & 1 deletion test/shale/builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,61 @@ class TestTransactionType < ::Shale::Mapper
attribute :amount, TestAmountType
end

class TestClientDataType < ::Shale::Mapper
include ::Shale::Builder

attribute :first_name, ::Shale::Type::String
attribute :last_name, ::Shale::Type::String
attribute :email, ::Shale::Type::String
end

class TestEnhancedTransactionType < TestTransactionType
attribute :client_data, TestClientDataType
end

context 'inheritance' do
should 'correctly set up a class after inheriting' do
mod_parent = TestTransactionType.builder_methods_module
mod_child = TestEnhancedTransactionType.builder_methods_module
assert mod_parent.is_a?(::Module)
assert mod_child.is_a?(::Module)
assert !mod_child.equal?(mod_parent)
assert TestTransactionType.include?(mod_parent)
assert !TestTransactionType.include?(mod_child)
assert TestEnhancedTransactionType.include?(mod_child)
assert TestEnhancedTransactionType.include?(mod_parent)
assert_equal %i[amount], mod_parent.instance_methods
assert_equal %i[client_data], mod_child.instance_methods
end

should 'correctly build an instance of a subclass' do
obj = TestEnhancedTransactionType.build do |t|
t.cvv_code = '321'
t.amount do |a|
a.value = 45.0
a.currency = 'USD'
end
t.client_data do |c|
c.first_name = 'Dupa'
c.last_name = 'Kret'
c.email = '[email protected]'
end
end

assert obj.is_a?(TestEnhancedTransactionType)
assert_equal '321', obj.cvv_code
assert obj.amount.is_a?(TestAmountType)
assert_equal 45.0, obj.amount.value
assert_equal 'USD', obj.amount.currency
assert obj.client_data.is_a?(TestClientDataType)
assert_equal 'Dupa', obj.client_data.first_name
assert_equal 'Kret', obj.client_data.last_name
assert_equal '[email protected]', obj.client_data.email
end
end


should 'correctly set up a class after inheriting' do
should 'correctly set up a class after including' do
mod = TestTransactionType.builder_methods_module
assert mod.is_a?(::Module)
assert TestTransactionType.include?(mod)
Expand Down

0 comments on commit 2ff1c58

Please sign in to comment.