how to define a two dimensional array using domain models library in ruby?

210 views Asked by At

In order to define a field with one dimension which contains Integers, used to define in the below format using domain models library.

class Sample
 include DomainModel

 field :numbers, :type => Integer, :collection => true

end

In the similar way how can I define a two dimensional array using Domain model?

1

There are 1 answers

2
engineersmnky On

Okay I have found a solution to your question in domain_model.

When you include the DomainModel Module into your class it adds a class method called validate. This method allows you to specify custom validation in the context of the instance so to validate that an Array contains only numbers at any depth this would be you code:

require 'domain_model'

class Sample
 include DomainModel


 field :numbers, :collection => true
 validate :numbers do |n|
   n.add("must contain only numbers") if self.numbers.flatten.any? {|value| !value.is_a?(Integer)}
 end
end

To validate that it contains only numbers in a 2 dimensional Array then

require 'domain_model'

class Sample
 include DomainModel


 field :numbers, :collection => true
 validate :numbers do |n|
   n.add("must contain only numbers") if self.numbers.flatten(1).any? {|value| !value.is_a?(Integer)}
 end
end