### Install Datory Gem Source: https://servactory.com/datory/getting-started Add the Datory gem to your Ruby project's Gemfile and install dependencies using Bundler. ```Ruby gem "datory" ``` ```Shell bundle install ``` -------------------------------- ### Define Base Form Class for Datory Source: https://servactory.com/datory/getting-started Establish a base class for form objects by inheriting from Datory::Base. This provides a common foundation for handling form data within your application. ```Ruby module ApplicationForm class Base < Datory::Base end end ``` -------------------------------- ### Define Base DTO Class for Datory Source: https://servactory.com/datory/getting-started Create a base class for Data Transfer Objects (DTOs) by inheriting from Datory::Base. This class serves as a foundation for all DTOs in your application. ```Ruby module ApplicationDTO class Base < Datory::Base end end ``` -------------------------------- ### Initialize Serialization Form with SerialDto Source: https://servactory.com/datory/guide/usage/serialization Demonstrates how to initialize a serialization form using the `SerialDto.form` method, passing the data to be serialized. This method allows for handling serialization errors gracefully. ```ruby form = SerialDto.form(serial) ``` -------------------------------- ### Perform Final Serialization Source: https://servactory.com/datory/guide/usage/serialization Demonstrates the final step of serialization by calling the `serialize` method on the form object. This converts the prepared data into its final serialized format. ```ruby form.serialize # => { ... } ``` -------------------------------- ### Access Target and Model Information Source: https://servactory.com/datory/guide/usage/serialization Illustrates how to retrieve information about the target object and the underlying model using the `target` and `model` methods of the form object. This provides insight into the serialized data structure. ```ruby form.target # => SerialDto form.model # => { ... } ``` -------------------------------- ### Instantiate SerialDto from Attributes Source: https://servactory.com/datory/guide/usage/serialization Demonstrates how to create a new instance of `SerialDto` by passing a hash of attributes to its `.new` method. This is the primary method for initializing a DTO with raw input data for serialization or validation. ```ruby serial = SerialDto.new(attributes) ``` -------------------------------- ### Direct Serialization from DTO Class Source: https://servactory.com/datory/guide/usage/serialization Shows an alternative method for serialization directly from the DTO class without using the `form` method. This approach throws a `Datory::Exceptions::SerializationError` on problems. ```ruby SerialDto.serialize(serial) # => { ... } ``` -------------------------------- ### Define SeasonDto with Datory::Base Source: https://servactory.com/datory/guide/usage/serialization Defines the `SeasonDto` class using `Datory::Base`, illustrating the definition of `uuid!`, `integer!`, `many!`, and `date!` attributes. It includes an example of an optional date field (`date?`) and attribute renaming. ```ruby class SeasonDto < Datory::Base uuid! :id uuid! :serialId, to: :serial_id integer! :number many! :episodes, include: EpisodeDto date! :premieredOn, to: :premiered_on date? :endedOn, to: :ended_on end ``` -------------------------------- ### Load SerialDto from ActiveRecord Model Source: https://servactory.com/datory/guide/usage/serialization Illustrates how to populate a `SerialDto` instance from an existing ActiveRecord model. This snippet shows integration with an ORM, where `Serial.find(id)` retrieves a record that can then be used to initialize the DTO. ```ruby serial = Serial.find(id) ``` -------------------------------- ### SerialDto Attribute Mapping Table Overview Source: https://servactory.com/datory/guide/usage/serialization A textual representation summarizing the attribute mappings for `SerialDto`. It details the 'From' (source type), 'To' (mapped attribute name), 'As' (resulting type), and 'Include' (nested DTO) for each defined attribute, providing a clear schema overview. ```text ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | SerialDto | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Attribute | From | To | As | Include | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | id | String | id | String | | | status | String | status | String | | | title | String | title | String | | | poster | [ImageDto, Hash] | poster | [ImageDto, Hash] | ImageDto | | ratings | [RatingsDto, Hash] | ratings | [RatingsDto, Hash] | RatingsDto | | countries | Array | countries | Array | CountryDto | | genres | Array | genres | Array | GenreDto | | seasons | Array | seasons | Array | SeasonDto | | premieredOn | String | premiered_on | Date | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` -------------------------------- ### Define a Float Attribute with 'float!' and 'float?' Helpers Source: https://servactory.com/datory/guide/data/attributes Provides examples for defining required and optional float attributes using 'float!' and 'float?', suitable for decimal numbers. ```ruby float! :rating ``` ```ruby float? :rating ``` -------------------------------- ### Validate Data with `valid?` and `invalid?` Source: https://servactory.com/datory/guide/usage/serialization Shows how to check the validity of serialized data using the `valid?` and `invalid?` methods on the initialized form object. These methods return boolean values indicating the validation status. ```ruby form.valid? # => true form.invalid? # => false ``` -------------------------------- ### SerialDto Datory Internal Schema Representation Source: https://servactory.com/datory/guide/usage/serialization Shows the detailed internal `Datory::Info::Result` object for `SerialDto`. This output provides a comprehensive breakdown of each attribute's 'from' and 'to' specifications, including types, formats, constraints, and included DTOs, reflecting the parsed schema. ```text # {:from=>{:name=>:id, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>:uuid}, :to=>{:name=>:id, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>:uuid, :required=>true, :default=>nil, :include=>nil}}, :status=> {:from=>{:name=>:status, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:status, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>nil}}, :title=> {:from=>{:name=>:title, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:title, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>nil}}, :poster=> {:from=>{:name=>:poster, :type=>[ImageDto, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:poster, :type=>[ImageDto, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>ImageDto}}, :ratings=> {:from=>{:name=>:ratings, :type=>[RatingsDto, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:ratings, :type=>[RatingsDto, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>RatingsDto}}, :countries=> {:from=>{:name=>:countries, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[CountryDto, Hash], :format=>nil}, :to=>{:name=>:countries, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[CountryDto, Hash], :format=>nil, :required=>true, :default=>nil, :include=>CountryDto}}, :genres=> {:from=>{:name=>:genres, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[GenreDto, Hash], :format=>nil}, :to=>{:name=>:genres, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[GenreDto, Hash], :format=>nil, :required=>true, :default=>nil, :include=>GenreDto}}, :seasons=> {:from=>{:name=>:seasons, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[SeasonDto, Hash], :format=>nil}, :to=>{:name=>:seasons, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[SeasonDto, Hash], :format=>nil, :required=>true, :default=>nil, :include=>SeasonDto}}, :premieredOn=> {:from=>{:name=>:premieredOn, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>:date}, :to=>{:name=>:premiered_on, :type=>Date, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>nil}}}> ``` -------------------------------- ### Define a Duration Attribute with 'duration!' and 'duration?' Helpers Source: https://servactory.com/datory/guide/data/attributes Provides examples of 'duration!' and 'duration?' helpers for defining required and optional duration attributes, showing their expansion into a generic 'attribute' definition with specific formatting and type casting to ActiveSupport::Duration. ```ruby duration! :episode_duration ``` ```ruby attribute :episode_duration, from: String, as: ActiveSupport::Duration, format: { from: :duration } ``` ```ruby duration? :episode_duration ``` ```ruby attribute :episode_duration, from: [String, NilClass], as: [ActiveSupport::Duration, NilClass], format: { from: :duration }, required: false ``` -------------------------------- ### Update Model Values in Serialization Form Source: https://servactory.com/datory/guide/usage/serialization Explains how to update values within the serialized model. It shows two methods: `update` for single objects and `update_by` for collections, specifying the index and new values. ```ruby form.update(title: "New title") form.update_by(0, title: "New title") # For collection ``` -------------------------------- ### Define SerialDto with Datory::Base Source: https://servactory.com/datory/guide/usage/serialization Defines the `SerialDto` class using `Datory::Base` to specify attributes like `id`, `status`, `title`, and relationships (`one!`, `many!`) to other DTOs such as `ImageDto`, `RatingsDto`, `CountryDto`, `GenreDto`, and `SeasonDto`. It also demonstrates attribute renaming with `to:`. ```ruby class SerialDto < Datory::Base uuid! :id string! :status string! :title one! :poster, include: ImageDto one! :ratings, include: RatingsDto many! :countries, include: CountryDto many! :genres, include: GenreDto many! :seasons, include: SeasonDto date! :premieredOn, to: :premiered_on end ``` -------------------------------- ### Define a Basic Attribute with 'attribute' Method Source: https://servactory.com/datory/guide/data/attributes Demonstrates how to define a data attribute using the generic 'attribute' method. It shows both required and optional configurations, specifying source and target types, and an optional format. ```ruby attribute :uuid, from: String, to: :id, as: String, format: :uuid ``` ```ruby attribute :uuid, from: [String, NilClass], to: :id, as: [String, NilClass], format: :uuid, required: false ``` -------------------------------- ### Retrieve Datory Object Schema Information using Ruby's info method Source: https://servactory.com/datory/guide/info Demonstrates how to use the `info` method on a Datory object (e.g., `SerialDto`) to obtain a comprehensive schema definition. The output details each attribute's source (`from`) and target (`to`) properties, including name, type, format, required status, and included DTOs for nested objects. ```ruby SerialDto.info ``` ```APIDOC Datory::Info::Result: attributes: id: from: name: id type: String min: null max: null consists_of: false format: uuid to: name: id type: String min: null max: null consists_of: false format: uuid required: true default: null include: null status: from: name: status type: String min: null max: null consists_of: false format: null to: name: status type: String min: null max: null consists_of: false format: null required: true default: null include: null title: from: name: title type: String min: null max: null consists_of: false format: null to: name: title type: String min: null max: null consists_of: false format: null required: true default: null include: null poster: from: name: poster type: [ImageDto, Hash] min: null max: null consists_of: false format: null to: name: poster type: [ImageDto, Hash] min: null max: null consists_of: false format: null required: true default: null include: ImageDto ratings: from: name: ratings type: [RatingsDto, Hash] min: null max: null consists_of: false format: null to: name: ratings type: [RatingsDto, Hash] min: null max: null consists_of: false format: null required: true default: null include: RatingsDto countries: from: name: countries type: Array min: null max: null consists_of: [CountryDto, Hash] format: null to: name: countries type: Array min: null max: null consists_of: [CountryDto, Hash] format: null required: true default: null include: CountryDto genres: from: name: genres type: Array min: null max: null consists_of: [GenreDto, Hash] format: null to: name: genres type: Array min: null max: null consists_of: [GenreDto, Hash] format: null required: true default: null include: GenreDto seasons: from: name: seasons type: Array min: null max: null consists_of: [SeasonDto, Hash] format: null to: name: seasons type: Array min: null max: null consists_of: [SeasonDto, Hash] format: null required: true default: null include: SeasonDto premieredOn: from: name: premieredOn type: String min: null max: null consists_of: false format: date to: name: premiered_on type: Date min: null max: null consists_of: false format: null required: true default: null include: null ``` -------------------------------- ### Ruby: Describe SerialDto Schema and Attributes Source: https://servactory.com/datory/guide/info This snippet shows how to invoke the `describe` method on `SerialDto` in Ruby. Following the Ruby call, the detailed schema of the `SerialDto` is presented, outlining each attribute's type, mapping, and any nested DTOs, providing a clear understanding of the data structure. ```ruby SerialDto.describe ``` ```APIDOC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | SerialDto | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Attribute | From | To | As | Include | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | id | String | id | String | | | status | String | status | String | | | title | String | title | String | | | poster | [ImageDto, Hash] | poster | [ImageDto, Hash] | ImageDto | | | ratings | [RatingsDto, Hash] | ratings | [RatingsDto, Hash] | RatingsDto | | | countries | Array | countries | Array | CountryDto | | genres | Array | genres | Array | GenreDto | | seasons | Array | seasons | Array | SeasonDto | | premieredOn | String | premiered_on | Date | | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` -------------------------------- ### Define a String Attribute with 'string!' and 'string?' Helpers Source: https://servactory.com/datory/guide/data/attributes Illustrates the use of 'string!' for required string attributes and 'string?' for optional string attributes, simplifying the definition of string-based data fields. ```ruby string! :uuid, to: :id ``` ```ruby string? :uuid, to: :id ``` -------------------------------- ### Define a UUID Attribute with 'uuid!' and 'uuid?' Helpers Source: https://servactory.com/datory/guide/data/attributes Shows the convenient 'uuid!' and 'uuid?' helpers for defining required and optional UUID attributes, along with their equivalent 'string!' definitions with a 'format: :uuid' option. ```ruby uuid! :id ``` ```ruby string! :id, format: :uuid ``` ```ruby uuid? :id ``` ```ruby string? :id, format: :uuid ``` -------------------------------- ### Define a Date Attribute with 'date!' and 'date?' Helpers Source: https://servactory.com/datory/guide/data/attributes Demonstrates the 'date!' and 'date?' helpers for defining required and optional date attributes, and their equivalent 'attribute' definitions with type casting to Date. ```ruby date! :premiered_on ``` ```ruby attribute :premiered_on, from: String, as: Date, format: { from: :date } ``` ```ruby date? :premiered_on ``` ```ruby attribute :premiered_on, from: [String, NilClass], as: [Date, NilClass], format: { from: :date }, required: false ``` -------------------------------- ### Define a Datetime Attribute with 'datetime!' and 'datetime?' Helpers Source: https://servactory.com/datory/guide/data/attributes Illustrates the 'datetime!' and 'datetime?' helpers for defining required and optional datetime attributes, and their equivalent 'attribute' definitions with type casting to DateTime. ```ruby datetime! :premiered_at ``` ```ruby attribute :premiered_at, from: String, as: DateTime, format: { from: :datetime } ``` ```ruby datetime? :premiered_at ``` ```ruby attribute :premiered_at, from: [String, NilClass], as: [DateTime, NilClass], format: { from: :datetime }, required: false ``` -------------------------------- ### Define an Integer Attribute with 'integer!' and 'integer?' Helpers Source: https://servactory.com/datory/guide/data/attributes Shows how to define required and optional integer attributes using 'integer!' and 'integer?', including options for specifying minimum and maximum values. ```ruby integer! :rating, min: 1, max: 10 ``` ```ruby integer? :rating, min: 1, max: 10 ``` -------------------------------- ### Define a Time Attribute with 'time!' and 'time?' Helpers Source: https://servactory.com/datory/guide/data/attributes Shows the 'time!' and 'time?' helpers for defining required and optional time attributes, and their equivalent 'attribute' definitions with type casting to Time. ```ruby time! :premiered_at ``` ```ruby attribute :premiered_at, from: String, as: Time, format: { from: :time } ``` ```ruby time? :premiered_at ``` ```ruby attribute :premiered_at, from: [String, NilClass], as: [Time, NilClass], format: { from: :time }, required: false ``` -------------------------------- ### Define a Money Attribute with 'money!' and 'money?' Helpers Source: https://servactory.com/datory/guide/data/attributes Illustrates the 'money!' and 'money?' helpers for defining required and optional money attributes, which internally expand into separate integer (cents) and string (currency) attributes. ```ruby money! :box_office ``` ```ruby integer! :box_office_cents string! :box_office_currency ``` ```ruby money? :box_office ``` ```ruby integer? :box_office_cents string? :box_office_currency ``` -------------------------------- ### Define a Boolean Attribute with 'boolean!' Helper Source: https://servactory.com/datory/guide/data/attributes Demonstrates how to define a required boolean attribute using 'boolean!'. Note that an explicit 'boolean?' helper for optional booleans is not directly supported in the same manner. ```ruby boolean! :published ``` ```ruby # not supported ``` -------------------------------- ### Deserialize JSON to Datory Result in Ruby Source: https://servactory.com/datory/guide/usage/deserialization This Ruby code snippet demonstrates how to deserialize a JSON string or Hash into a `Datory::Result` object. The `SerialDto.deserialize` method is used, which takes the data (e.g., `json`) as an argument and returns an instance of `Datory::Result`. ```ruby SerialDto.deserialize(json) # => Datory::Result ``` -------------------------------- ### Define Optional Single Nested Data in Ruby Source: https://servactory.com/datory/guide/data/nesting This snippet demonstrates how to define an optional single nested data object using the `one?` method in Ruby. It specifies that the `poster` attribute can optionally include an `ImageDto`. ```ruby one? :poster, include: ImageDto ``` -------------------------------- ### Define Required Single Nested Data in Ruby Source: https://servactory.com/datory/guide/data/nesting This snippet demonstrates how to define a required single nested data object using the `one!` method in Ruby. It specifies that the `poster` attribute must include an `ImageDto`. ```ruby one! :poster, include: ImageDto ``` -------------------------------- ### Define Optional Multiple Nested Data in Ruby Source: https://servactory.com/datory/guide/data/nesting This snippet demonstrates how to define optional multiple nested data objects using the `many?` method in Ruby. It specifies that the `seasons` attribute can optionally include a collection of `SeasonDto` objects. ```ruby many? :seasons, include: SeasonDto ``` -------------------------------- ### Define Required Multiple Nested Data in Ruby Source: https://servactory.com/datory/guide/data/nesting This snippet demonstrates how to define required multiple nested data objects using the `many!` method in Ruby. It specifies that the `seasons` attribute must include a collection of `SeasonDto` objects. ```ruby many! :seasons, include: SeasonDto ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.