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
1
vote
0 answers

initialize data in edit using rails form object

I cant initialize the data for class attributes with the use virtus gem. Here's my example code: class ShipmentForm include Virtus.mode attr_reader :shipment attr_reader :shipment_detail attribute :shipment_type, String attribute …
Ryan Angelo
  • 93
  • 1
  • 13
1
vote
1 answer

How to cull attributes before creating a virtus Object?

I have more attributes in my hash than Virtus has defined in it's class. I want to cull those attributes inside the virtus model before instantiating it. test_hash = {:x="stuff" , :y ="stuff2", :z="stuff3"} def myObject include Virtus.model …
Dr. Chocolate
  • 2,043
  • 4
  • 25
  • 45
0
votes
2 answers

Initialize with String or Hash in Virtus Ruby object

I need to be able to create a Virtus based object that takes in a Hash or a String. If its a Hash, then normal behavior is perfect. If it's a plain String, then i need to convert it to {'id' => "STRING"}. At this time, i'm not sure how / where to…
Jaison Brooks
  • 5,816
  • 7
  • 43
  • 79
0
votes
1 answer

Render fields for array attribute in Rails form object

Right now Im having a hard time to render form fields for the array attribute inside may form object. Im using virtus gem for setting up attributes. My Code: class JournalForm include Virtus.model extend ActiveModel::Naming include…
Ryan Angelo
  • 93
  • 1
  • 13
0
votes
0 answers

How would one monkey patch this: `include Virtus.model'?

I have a few method I would like to add to that but am not 100% sure what's going on there. Is it including some kind of factory method?
Dr. Chocolate
  • 2,043
  • 4
  • 25
  • 45
0
votes
1 answer

How to get a list of all of one type of Virtus attribute?

How to get a list of all of one type? Like for example, a list of all of the String attributes? Is there an easy Virtus solution or do I have to roll my own? def my_model include Virtus.model attribute :a, Integer attribute :b, Integer …
Dr. Chocolate
  • 2,043
  • 4
  • 25
  • 45
0
votes
1 answer

Why use virtus with rails form objects?

When I use the form object pattern in rails, I include ActiveModel and I tend to delegate attributes to an actual model. Why would I want to use the virtus gem? Seems I'd be redefining attributes that are already defined elsewhere (i.e. a model)
Nathan
  • 7,627
  • 11
  • 46
  • 80
-1
votes
1 answer

Defining attributes at runtime based on data from related object

I'm building an application where users are part of an Organisation. An organisation has many Lists, which in turn have many ListItems. Now, I would like for admin users to be able to specify which attributes are available on list items, based on…
bumcode
  • 362
  • 4
  • 13
1
2