Skip to content

Commit

Permalink
Separate into required and optional attributes (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
afuno authored Jun 6, 2024
1 parent 3c32bb8 commit c9d615e
Show file tree
Hide file tree
Showing 21 changed files with 1,436 additions and 140 deletions.
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ GEM
remote: https://rubygems.org/
specs:
abbrev (0.1.2)
activesupport (7.1.3.2)
activesupport (7.1.3.3)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
Expand All @@ -28,16 +28,16 @@ GEM
thor (>= 0.14.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.7)
bigdecimal (3.1.8)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
diff-lcs (1.5.1)
drb (2.2.1)
i18n (1.14.4)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
minitest (5.22.3)
minitest (5.23.0)
mutex_m (0.2.0)
parallel (1.24.0)
parser (3.3.0.5)
Expand Down Expand Up @@ -93,7 +93,7 @@ GEM
rubocop-rspec_rails (2.28.3)
rubocop (~> 1.40)
ruby-progressbar (1.13.0)
servactory (2.6.0)
servactory (2.6.2)
activesupport (>= 5.1, < 7.2)
i18n (~> 1.14)
zeitwerk (~> 2.6)
Expand All @@ -103,7 +103,7 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
zeitwerk (2.6.13)
zeitwerk (2.6.14)

PLATFORMS
ruby
Expand Down
4 changes: 2 additions & 2 deletions examples/usual/example1/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Usual
module Example1
class Country < Datory::Base
string :name
string :iso2 # rubocop:disable Naming/VariableNumber
string! :name
string! :iso2 # rubocop:disable Naming/VariableNumber
end
end
end
4 changes: 2 additions & 2 deletions examples/usual/example1/genre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Usual
module Example1
class Genre < Datory::Base
string :name
string :code
string! :name
string! :code
end
end
end
4 changes: 2 additions & 2 deletions examples/usual/example1/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Usual
module Example1
class Image < Datory::Base
string :url
boolean :default
string! :url
boolean! :default
end
end
end
6 changes: 4 additions & 2 deletions examples/usual/example1/rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
module Usual
module Example1
class Rating < Datory::Base
float :value
integer :quantity
float! :value
integer! :quantity

string? :linkUrl, to: :link_url
end
end
end
2 changes: 1 addition & 1 deletion examples/usual/example1/ratings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Usual
module Example1
class Ratings < Datory::Base
one :imdb, include: Rating
one! :imdb, include: Rating
end
end
end
11 changes: 6 additions & 5 deletions examples/usual/example1/season.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
module Usual
module Example1
class Season < Datory::Base
uuid :id
# uuid :serialId, to: :serial_id
uuid! :id
# uuid! :serialId, to: :serial_id

integer :number
integer! :number

# many :episodes, include: Episode
# many! :episodes, include: Episode

date :premieredOn, to: :premiered_on
date! :premieredOn, to: :premiered_on
date? :endedOn, to: :ended_on
end
end
end
18 changes: 9 additions & 9 deletions examples/usual/example1/serial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
module Usual
module Example1
class Serial < Datory::Base
uuid :id
uuid! :id

string :status
string :title
string! :status
string! :title

one :poster, include: Image
one! :poster, include: Image

one :ratings, include: Ratings
one! :ratings, include: Ratings

many :countries, include: Country
many :genres, include: Genre
many :seasons, include: Season
many! :countries, include: Country
many! :genres, include: Genre
many! :seasons, include: Season

date :premieredOn, to: :premiered_on
date! :premieredOn, to: :premiered_on
end
end
end
11 changes: 7 additions & 4 deletions examples/usual/example2/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
module Usual
module Example2
class Product < Datory::Base
uuid :id
uuid! :id

string :title
string! :title

money :price
money! :price
money? :discount

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

duration? :installmentDuration, to: :installment_duration
end
end
end
37 changes: 37 additions & 0 deletions examples/usual/example3/language.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Usual
module Example3
class Language < Datory::Base
uuid! :id

string! :name

# TODO: Need to prepare this example:
# {
# versions: {
# current: {},
# maintenance: {
# normal: {
# last: {},
# all: []
# },
# security: {
# last: {},
# all: []
# }
# },
# eol: {
# last: {},
# all: []
# }
# }
# }

one! :currentVersion, to: :current, include: Version
one? :lastEOLVersion, to: :last_eol, include: Version

many? :previousVersions, to: :previous, include: Version
end
end
end
13 changes: 13 additions & 0 deletions examples/usual/example3/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Usual
module Example3
class Version < Datory::Base
string! :name

# NOTE: Must be empty within examples.
date? :releasedAt, to: :released_at
date? :endedAt, to: :ended_at
end
end
end
17 changes: 17 additions & 0 deletions examples/usual/example4/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Usual
module Example4
class Comment < Datory::Base
uuid! :id

uuid? :parentId, to: :parent_id

string! :content

time? :editedAt, to: :edited_at

datetime? :publishedAt, to: :published_at
end
end
end
11 changes: 8 additions & 3 deletions lib/datory/attributes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Attributes
class Attribute
attr_reader :from, :to

def initialize(name, **options) # rubocop:disable Metrics/MethodLength
def initialize(name, **options) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
@from = Options::From.new(
name: name,
type: options.fetch(:from),
Expand All @@ -20,6 +20,7 @@ def initialize(name, **options) # rubocop:disable Metrics/MethodLength
type: options.fetch(:as, @from.type),
# TODO: It is necessary to implement NilClass support for optional
required: options.fetch(:required, true),
default: options.fetch(:default, nil),
consists_of: @from.consists_of,
min: @from.min,
max: @from.max,
Expand All @@ -30,11 +31,12 @@ def initialize(name, **options) # rubocop:disable Metrics/MethodLength

##########################################################################

def input_serialization_options # rubocop:disable Metrics/AbcSize
def input_serialization_options # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
hash = {
as: to.name,
type: to.type,
required: to.required,
default: to.default,
consists_of: to.consists_of
}

Expand Down Expand Up @@ -64,18 +66,21 @@ def output_serialization_options # rubocop:disable Metrics/AbcSize

##########################################################################

def input_deserialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def input_deserialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
hash = {
as: to.name,
type: from.type,
required: to.required,
default: to.default,
consists_of: from.consists_of,
prepare: (lambda do |value:|
return value unless to.include_class.present?

if [Set, Array].include?(from.type)
value.map { |item| to.include_class.deserialize(**item) }
else
return nil if value.nil? # NOTE: When `one` is optional and not passed

to.include_class.deserialize(**value)
end
end)
Expand Down
Loading

0 comments on commit c9d615e

Please sign in to comment.