Skip to content

Commit

Permalink
Add getter and setter DSL to Datory
Browse files Browse the repository at this point in the history
Added getter and setter Domain Specific Language (DSL) as features to the Datory framework. This change includes creation of new files for getter, setter, and their collection classes. Moreover, adjustments were made in the test files and examples to demonstrate the usage of these newly added features.
  • Loading branch information
afuno committed Jun 6, 2024
1 parent 061ea82 commit 795b27a
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 10 deletions.
9 changes: 9 additions & 0 deletions examples/usual/example2/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ class Product < Datory::Base
uuid! :id

string! :title
string! :formatted_title

money! :price
money? :discount

integer! :quantity, min: 1, max: 10

duration? :installmentDuration, to: :installment_duration

getter :formatted_title do |attributes:|
"The New #{attributes.fetch(:title)} (from getter)"
end

setter :formatted_title do |attributes:|
"The New #{attributes.fetch(:title)} (from setter)"
end
end
end
end
8 changes: 8 additions & 0 deletions lib/datory/attributes/serialization/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def build(attributes = {}) # rubocop:disable Metrics/MethodLength
self
end

def add(key, value)
self.class.send(:attr_accessor, key)

instance_variable_set(:"@#{key}", value)

self
end

def parse(data) # rubocop:disable Metrics/MethodLength
data.instance_variables.to_h do |key|
value = data.instance_variable_get(key)
Expand Down
4 changes: 2 additions & 2 deletions lib/datory/attributes/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Attributes
module Workspace
private

def serialize(model:, collection_of_attributes:)
def serialize(model:, collection_of_attributes:, **)
super

return nil if model.nil? # NOTE: When `one` is optional and not passed
Expand All @@ -18,7 +18,7 @@ def serialize(model:, collection_of_attributes:)
)
end

def deserialize(incoming_attributes:, collection_of_attributes:)
def deserialize(incoming_attributes:, collection_of_attributes:, **)
super

Deserialization::ServiceBuilder.build!(self, incoming_attributes, collection_of_attributes)
Expand Down
2 changes: 2 additions & 0 deletions lib/datory/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Datory
class Base
include Info::DSL
include Context::DSL
include Getters::DSL
include Setters::DSL
include Attributes::DSL
end
end
6 changes: 4 additions & 2 deletions lib/datory/context/callable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,17 @@ def _serialize(context, model)
context.send(
:_serialize,
model: model,
collection_of_attributes: collection_of_attributes
collection_of_attributes: collection_of_attributes,
collection_of_setters: collection_of_setters
)
end

def _deserialize(context, **attributes)
context.send(
:_deserialize,
incoming_attributes: attributes.symbolize_keys,
collection_of_attributes: collection_of_attributes
collection_of_attributes: collection_of_attributes,
collection_of_getters: collection_of_getters
)
end

Expand Down
16 changes: 10 additions & 6 deletions lib/datory/context/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
module Datory
module Context
module Workspace
def _serialize(model:, collection_of_attributes:)
def _serialize(model:, collection_of_attributes:, collection_of_setters:)
serialize(
model: model,
collection_of_attributes: collection_of_attributes
collection_of_attributes: collection_of_attributes,
collection_of_setters: collection_of_setters
)
end

def _deserialize(incoming_attributes:, collection_of_attributes:)
def _deserialize(incoming_attributes:, collection_of_attributes:, collection_of_getters:)
deserialize(
incoming_attributes: incoming_attributes,
collection_of_attributes: collection_of_attributes
collection_of_attributes: collection_of_attributes,
collection_of_getters: collection_of_getters
)
end

Expand All @@ -23,14 +25,16 @@ def _to_model(attributes:)
)
end

def serialize(model:, collection_of_attributes:, **)
def serialize(model:, collection_of_attributes:, collection_of_setters:, **)
@model = model
@collection_of_attributes = collection_of_attributes
@collection_of_setters = collection_of_setters
end

def deserialize(incoming_attributes:, collection_of_attributes:, **)
def deserialize(incoming_attributes:, collection_of_attributes:, collection_of_getters:, **)
@incoming_attributes = incoming_attributes
@collection_of_attributes = collection_of_attributes
@collection_of_getters = collection_of_getters
end

def to_model(attributes:)
Expand Down
18 changes: 18 additions & 0 deletions lib/datory/getters/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Datory
module Getters
class Collection
extend Forwardable
def_delegators :@collection, :<<, :each, :merge

def initialize(collection = Set.new)
@collection = collection
end

# def find_by(name:)
# find { |getter| getter.name == name }
# end
end
end
end
30 changes: 30 additions & 0 deletions lib/datory/getters/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Datory
module Getters
module DSL
def self.included(base)
base.extend(ClassMethods)
base.include(Workspace)
end

module ClassMethods
def inherited(child)
super

child.send(:collection_of_getters).merge(collection_of_getters)
end

private

def getter(name)
collection_of_getters << Getter.new(name, ->(attributes:) { yield(attributes: attributes) })
end

def collection_of_getters
@collection_of_getters ||= Collection.new
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/datory/getters/getter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Datory
module Getters
class Getter
attr_reader :name, :block

def initialize(name, block)
@name = name
@block = block
end
end
end
end
17 changes: 17 additions & 0 deletions lib/datory/getters/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Datory
module Getters
module Workspace
private

def deserialize(incoming_attributes:, collection_of_attributes:, collection_of_getters:)
super

collection_of_getters.each do |getter|
incoming_attributes.merge!(getter.name => getter.block.call(attributes: incoming_attributes))
end
end
end
end
end
18 changes: 18 additions & 0 deletions lib/datory/setters/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

module Datory
module Setters
class Collection
extend Forwardable
def_delegators :@collection, :<<, :each, :merge

def initialize(collection = Set.new)
@collection = collection
end

# def find_by(name:)
# find { |getter| getter.name == name }
# end
end
end
end
30 changes: 30 additions & 0 deletions lib/datory/setters/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Datory
module Setters
module DSL
def self.included(base)
base.extend(ClassMethods)
base.include(Workspace)
end

module ClassMethods
def inherited(child)
super

child.send(:collection_of_setters).merge(collection_of_setters)
end

private

def setter(name)
collection_of_setters << Setter.new(name, ->(attributes:) { yield(attributes: attributes) })
end

def collection_of_setters
@collection_of_setters ||= Collection.new
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/datory/setters/setter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module Datory
module Setters
class Setter
attr_reader :name, :block

def initialize(name, block)
@name = name
@block = block
end
end
end
end
19 changes: 19 additions & 0 deletions lib/datory/setters/workspace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Datory
module Setters
module Workspace
private

def serialize(model:, collection_of_attributes:, collection_of_setters:)
super

collection_of_setters.each do |setter|
hash = Datory::Attributes::Serialization::Model.to_hash(model)

model.add(setter.name, setter.block.call(attributes: hash))
end
end
end
end
end
27 changes: 27 additions & 0 deletions spec/examples/usual/example2/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
{
id: "55363a14-aa9a-4eba-9276-7f7cec432123",
title: "iPhone 15 Pro",
formatted_title: "The New iPhone 15 Pro (from setter)",
price_cents: 999_00,
price_currency: "USD",
discount_cents: nil,
Expand All @@ -158,6 +159,7 @@
{
id: "55363a14-aa9a-4eba-9276-7f7cec432123",
title: "iPhone 15 Pro",
formatted_title: "The New iPhone 15 Pro (from setter)",
price_cents: 999_00,
price_currency: "USD",
discount_cents: nil,
Expand Down Expand Up @@ -211,6 +213,7 @@
{
id: "55363a14-aa9a-4eba-9276-7f7cec432123",
title: "iPhone 15 Pro",
formatted_title: "The New iPhone 15 Pro (from setter)",
price_cents: 999_00,
price_currency: "USD",
quantity: 5
Expand All @@ -236,6 +239,7 @@
have_attributes(
id: "55363a14-aa9a-4eba-9276-7f7cec432123",
title: "iPhone 15 Pro",
formatted_title: "The New iPhone 15 Pro (from getter)",
price_cents: 999_00,
price_currency: "USD",
quantity: 5
Expand Down Expand Up @@ -350,6 +354,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| id | String | id | String |
| title | String | title | String |
| formatted_title | String | formatted_title | String |
| price_cents | Integer | price_cents | Integer |
| price_currency | String | price_currency | String |
| discount_cents | [Integer, NilClass] | discount_cents | [Integer, NilClass] |
Expand Down Expand Up @@ -379,6 +384,7 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| id | String | id | String |
| title | String | title | String |
| formatted_title | String | formatted_title | String |
| price_cents | Integer | price_cents | Integer |
| price_currency | String | price_currency | String |
| discount_cents | [Integer, NilClass] | discount_cents | [Integer, NilClass] |
Expand Down Expand Up @@ -444,6 +450,27 @@
include: nil
}
},
formatted_title: {
from: {
name: :formatted_title,
type: String,
min: nil,
max: nil,
consists_of: false,
format: nil
},
to: {
name: :formatted_title,
type: String,
required: true,
default: nil,
min: nil,
max: nil,
consists_of: false,
format: nil,
include: nil
}
},
price_cents: {
from: {
name: :price_cents,
Expand Down

0 comments on commit 795b27a

Please sign in to comment.