Is there a possibility in Rails/Ruby to get the list of model's methods.
fe. ModelName.methods
I want to get all names of methods that belongs to Mailer model.
Sorting methods out is always a problem in ruby, because you can't simply say : "give me methods specific to that class".
Getting methods for a specific class
You have to use array substraction like mentionned by #Monk_Code and even then, you can't separate methods from a base implementation and from its monkey patches.
To exhaustively remove all included modules and all parents methods :
> MyClass.instance_methods - ( MyClass.ancestors - [ MyClass ] ).map( &:instance_methods ).flatten
Replace #instance_methods with #methods if you want class methods.
Note that methods dynamically created with #define_method in parents like model callbacks will still appear, as they are defined directly on the child class.
Getting methods for a specific file
Often, isolating methods from a class is not enough.
I wrote an helper that helps me isolate methods from files. It allows to do that :
> MyModel.new.located_methods
+------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| Name | Location |
+------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------+
| ! | |
| <=> | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/core.rb line 324 |
| unloadable | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb line 245 |
| == | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/core.rb line 296 |
| validates_format_of | /home/user/.gem/ruby/2.0.0/gems/activemodel-4.0.0/lib/active_model/validations/format.rb line 110 |
| and so on ... |
A first parameter allows to grep method names :
> MyModel.new.located_methods /validate/
+----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------+
| Name | Location |
+----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------+
| _validate_callbacks | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 107 |
| _validate_callbacks= | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 117 |
| _validate_callbacks? | /home/user/.gem/ruby/2.0.0/gems/activesupport-4.0.0/lib/active_support/core_ext/class/attribute.rb line 114 |
| validate_associated_records_for_amenities | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/autosave_association.rb line 147 |
And a second one allows to grep per source file :
> MyModel.new.located_methods /validate/, /autosave/
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| Name | Location |
+----------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| validate_associated_records_for_amenities | /home/user/.gem/ruby/2.0.0/gems/activerecord-4.0.0/lib/active_record/autosave_association.rb line 147 |
the code
class Object
# Display an object methods list with their source location.
#
# List can optionally be filtered by method pattern and source file
# pattern.
#
# Mainly useful for debugging.
#
# #param [Regexp] method_pattern grep method name
# #param [Regexp] file_pattern grep file name
def located_methods( method_pattern = nil, file_pattern = nil )
list = ( method_pattern ? methods.grep( method_pattern ) : methods ).sort.map do |name|
location = method( name ).source_location
location = "#{location.first} line #{location.second}" if location
[ name.to_s.colorize( :yellow ), location ]
end
list = list.select { |meth| meth[1] =~ file_pattern } if file_pattern
puts ( [[ 'Name'.colorize( :yellow ), 'Location' ]] + list ).to_table( first_row_is_head: true )
true
end
end
This version depends on colorize and text-table, but you can easily modify it to use your preferred way of formatting.
class Object
def show_methods
(methods - self.class.superclass.instance_methods).sort
end
end
Mailer.show_methods
for example
I would suggest use the
show-method
from the pry gem.
You can also navigate within objects like in a unix console via
cd
ls
etc...
Check out this small example
and this: http://pryrepl.org/
Related
i want to retrieve an ec2 instance's ID from it's name which I have set as the value of it's tag. I have a VM with key as 'Name' and value as 'testvm1'. Is there something like
(get-ec2 instance id) | where-object {$_.(key.value) - eq "testvm1"}
this worked
$ec2Name = New-Object Amazon.EC2.Model.Filter -Property #{Name = "tag:Name"; Values = "testvm1"}
$instances = #(Get-EC2Tag -Filters $ec2Name)
$instances | Select-Object -ExpandProperty resourceid
You can further reduce this by passing a hashtable as your filter. This will map your input to a Amazon.EC2.Model.Filter[] type.
Example with one filter on tag:Name mapping multiple tag names:
(Get-EC2Tag -Filter #{Name="tag:Name";Values="Name1","Name2"}).ResourceId
Example output:
i-abcd1234
i-edfg5678
In order to nest regular URL mappings within a resource mapping, according to the grails documentation in the url mapping section you can do the following
"/books"(resources: "book") {
"/publisher"(controller:"book", action:'publisher', method: "GET")
}
This will produce the following list of urls
Controller: book
| GET | /books/create | Action: create |
| GET | /books/${id}/edit | Action: edit |
| GET | /books/${bookId}/publisher | Action: publisher |
| POST | /books | Action: save |
| GET | /books | Action: index |
| DELETE | /books/${id} | Action: delete |
| PATCH | /books/${id} | Action: patch |
| PUT | /books/${id} | Action: update |
| GET | /books/${id} | Action: show |
As you can see the url generated by "/publisher"(controller:"book", action:'publisher', method: "GET") use the variable bookId instead of id like the other urls.
/books/${bookId}/publisher
My question is whether it is possible to change the name of this variable and name it for example id like the others
Thanks in advance
I have not used that mapping style personally but I believe the intent of the regular URL mapping within the resource mapping is to do something 'special' with that particular path, in this case /publisher (for example you could map it to a different controller altogether, etc)
It sounds like you want it to behave like another standard GET method, so you could add a resource entry like
get "/books/$id/publisher"(controller:"book", action:"publisher")
I've checked code and examples from identical questions on this site, but I'm baffled, because I'm utilizing nearly the exact same code from Get-ADUser with display name as a value and it just pulls empty results for me. I don't know how to continue a conversation on an existing question, so I figured I had to make my own.
Here is what I have:
import-module activedirectory
$csv = Get-Content users.csv
foreach ($user in $csv) {
$user = $user.trim()
write-host $user #A line for testing to show me what name is being analyzed
$samName = Get-ADUser -Filter{displayName -like "$user*"} | select samaccountname
write-host $samName
}
cmd /c pause | out-null
The output I get is:
John Doe
Jane Doe
Billy Ray
But if I change
$samName = Get-ADUser -Filter{displayName -like "$user*"} | select samaccountname
to:
$samName = Get-ADUser -Filter{displayName -like "John Doe*"} | select samaccountname
It will correctly return John Doe's samaccountname.
I'm trying to get FitNesse (slim tests running via fitSharp) to process tables stored in a variable. Both approach A & B below render the same on the page, but only approach B will run.
Approach A
!define test (
| Table:myTest | someValue |
)
${test}
Approach B
| Table:myTest | someValue |
This example is rather superficial, but in my tests I'm looking to vary some parameters and reexecute the same test (without a lot of copy and paste).
Adding additional details requested;
Approach A renders this to page when saving;
<br><span class="meta">variable defined: test=
| Table:myTest | someValue |
</span>
<br><br><table>
<tbody><tr class="slimRowTitle">
<td>Table:myTest</td>
<td>someValue</td>
</tr>
</tbody></table>
<br>
...but when running the test the page doesn't seem to process the table and shows just the variable definition
<br><span class="meta">variable defined: test=
| Table:myTest | someValue |
</span>
<br><br><br></div>
Try creating a separate page with the test table.
In your real test page you can include this page multiple times, after assigning values to the variables.
Ok, the example from plugin birt helped me tons, but one question is bothering me, how can I pass params to the birt to select from id?
question | description
----------------------
1 | blabla
2 | xoxoxo
3 | tititi
4 | buhbuh
Example: I have this table above... From my grails application, I choose what question I want, so if I choose 1,3,4... The Birt report shows me selected only.
Basically, I have to change my dataset too, because my query is static and needed to be dynamic:.
...(query) and a1.question_id = 1 and a2.question_id = 2 and and a3.question_id = 3 (query)...
But in grails how I will pass the params to dataset?
the Birt report receives the parameters with params, you can use this part of code
def options = birtReportService.getRenderOption(request, 'html')
def result=birtReportService.runAndRender(reportName, params, options)
render result
. Remember don't forget place your rptdesing in a folder on (web-app)