Questions tagged [virtus]

Virtus allows you to define attributes on classes, modules or class instances with optional information about types, reader/writer method visibility and coercion behavior. It supports a lot of coercions and advanced mapping of embedded objects and collections.

Taken from the docs:

Virtus allows you to define attributes on classes, modules or class instances with optional information about types, reader/writer method visibility and coercion behavior. It supports a lot of coercions and advanced mapping of embedded objects and collections.

.../...

You can use it in many different contexts like:

Input parameter sanitization and coercion in web applications Mapping JSON to domain objects Encapsulating data-access in Value Objects Domain model prototyping

23 questions
9
votes
2 answers

Dynamically extend Virtus instance attributes

Let's say we have a Virtus model User class User include Virtus.model attribute :name, String, default: 'John', lazy: true end Then we create an instance of this model and extend from Virtus.model to add another attribute on the fly: user =…
potashin
  • 44,205
  • 11
  • 83
  • 107
9
votes
2 answers

How to store a string identifier to a model attribute

I'm using Virtus to create models that represent Salesforce objects. I'm trying to create attributes that have friendly names that are used to access the value and method that I can use to retrieve a identifier "String" for that…
Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79
7
votes
1 answer

Use boolean attribute helper methods when extending from Virtus.model on the fly

Let's say I have a Virtus model User with a boolean attribute active: class User include Virtus.model attribute :active, Boolean, default: false, lazy: true end Then I could user a helper method active?: User.new.active? # =>…
potashin
  • 44,205
  • 11
  • 83
  • 107
5
votes
1 answer

Ruby's Virtus gem vs attr_accessor

I am looking at the Virtus gem used in a few tutorials about Service object in Ruby. In the github page, https://github.com/solnic/virtus, it gives the following example. Using Virtus with Classes You can create classes extended with Virtus and…
frank
  • 1,283
  • 1
  • 19
  • 39
4
votes
2 answers

default values for Virtus in rails

I am using virtus(1.0.5) with rails (5.0.2). I am using Virtus for a model since it has validations based on the page being accessed. My Organization model is as class Organization < ApplicationRecord validates :name, presence: true end and form…
Prasad Surase
  • 6,486
  • 6
  • 39
  • 58
4
votes
3 answers

Rails Form Object with Virtus: has_many association

I am having a tough time figuring out how to make a form_object that creates multiple associated objects for a has_many association with the virtus gem. Below is a contrived example where a form object might be overkill, but it does show the issue I…
Neil
  • 4,578
  • 14
  • 70
  • 155
4
votes
1 answer

How to implement `dry-validation` gem in a Rails form object?

I'm trying to substitute ActiveRecord validations with Dry-validations, but I've been unable to find any in-app implementation examples to follow. Dry-validation docs: http://dry-rb.org/gems/dry-validation/ I've added below to the form object, but I…
tim_xyz
  • 11,573
  • 17
  • 52
  • 97
2
votes
1 answer

what a method 'books=(books)' is doing?

I joined Rails team and maintain the codes. Some of the objects are controlled by Gem virtus, but I really don't understand like below code is doing. I understand the result that the attribute 'latest_book' can collect latest book from Books but why…
d.y
  • 105
  • 1
  • 6
2
votes
3 answers

Coercion of comma separated values string into array using dry-types

I wish to coerce the form input "1,3,5" into: [1,3,5] I am using dry-types gem for other coercions and constraints. I need to know: Is this possible via any built-in mechanism in rails or dry-types ? If not, how do I define a custom coercion for…
Zuhaib Ali
  • 3,344
  • 3
  • 20
  • 32
2
votes
1 answer

undefined method `persisted?' with Reform and Virtus model

I'm currently developing an Rails app (rails v5.1.1 and ruby v2.3.4) and I'm getting an error when trying to use a reform form object at one of my routes (/bookings/new): undefined method `persisted?' for # I'm using a…
Igor_Marques
  • 1,742
  • 2
  • 16
  • 24
2
votes
1 answer

Use Storext (or just Virtus) with nested array or hash objects

I have a postgres DB backing my Rails app with a class with a jsonb column class Product < AR::B include Storext.model(data: {}) store_attributes :data do thing_one String thing_two Boolean # Not actually showing up in the `data` hash …
MCB
  • 2,021
  • 1
  • 18
  • 32
2
votes
0 answers

Does Virtus support attributes that are Hash[SomeKey => Array[CustomModel]]?

For example: class Nested include Virtus.model attribute :name, String end class Base # Does not raise an exception even though the array failed to coerce! include Virtus.model(:strict => true) attribute :name, String attribute…
qix
  • 7,228
  • 1
  • 55
  • 65
2
votes
1 answer

Virtus and booleans conversion

I'm using Virtus gem and trying to autocast Strings to Booleans, but without success... If you can see what's wrong with this code... Virtus.coercer do |config| config.string.boolean_map = { 'true' => true, 'false' => false } end class…
Nicolas Blanco
  • 11,164
  • 7
  • 38
  • 49
1
vote
0 answers

fields_for and POROs (Virtus) in Rails

I have a couple of virtus objects: class Calculation include Virtus.model include ActiveModel::Validations include ActiveModel::Model attribute :age_ranges, Array[AgeRange], default: [{from: 16, to: 22},{from: 24, to: 30}] end class…
sekmo
  • 1,517
  • 18
  • 27
1
vote
1 answer

Custom wrap JSON to Virtus Model?

I have a JSON object that looks like the following: { "id":"10103", "key":"PROD", "name":"Product", "projectCategory":{ "id":"10000", "name":"design", "description":"" } } and a Virtus model that looks like the…
beckah
  • 1,543
  • 6
  • 28
  • 61
1
2