Convierta la asociación de rieles a la sintaxis de shouldamatcher usando expresiones regulares

I am working on a ruby on rails project. I am trying to convert

'has_many :comments,:as=>:entity,:order=>"id asc", :dependent => :destroy'

verse así

it { should have_many(:comments).as(:entity).order("id asc").dependent(:destroy)}

I am almost successful in doing so . The only problem I face is in a part of it. The regex I am using to find the key value pairs inside the string is

'has_many :comments,:as=>:entity,:order=>"id asc", :dependent => :destroy'.scan(/:\w+\s*=>\s*.\w+/) que regresa

#=> [":as=>:entity", ":order=>\"id", ":dependent => :destroy"]

Its not able to return the full string inside any of the values. It just takes the first word which is quite evident from the regex.

I want a result like this :

#=> [":as=>:entity", ":order=> "id asc", ":dependent => :destroy"]

preguntado el 12 de febrero de 14 a las 07:02

1 Respuestas

Prueba este:

'has_many :comments,:as=>:entity,:order=>"id asc", :dependent => :destroy'.scan(/:\w+\s*=>\s*.[\w \"\']+/)

Respondido 12 Feb 14, 09:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.