Sketchup Get Entity By component name - ruby

To work on an element, I use the following for the definition.
ref = Sketchup.active_model.entities[0]
refdef = ref.definition
is there a way in which I can get the entitiy by its name (component name) instead of entities[0] etc

Sketchup.active_model.definitions returns a DefinitionList. Its [] method can be used to access definitions by index, GUID or name.
componame="MyLovelyComponent"
compo = Sketchup.active_model.definitions[componame]
if compo
puts "#{componame} found: doing something else..."
### do something with 'compo' definition
else
puts "#{componame} does NOT exist!"
return
end

Related

PostgreSQL "ERROR: could not determine data type of parameter" Ruby exec_params

I am trying to execute a query that selects recipes that match a search term from user input stored in the query variable. This is the portion of relevant code:
class DatabasePersistence
def initialize(logger)
#db = if Sinatra::Base.production?
PG.connect(ENV['DATABASE_URL'])
else
PG.connect(dbname: "recipes")
end
#logger = logger
end
def search_recipes(query)
p "Query parameter is:"
p query
p query.class
sql = <<~SQL
SELECT * FROM recipes
WHERE labels ILIKE '%$1::text%'
SQL
results = query(sql, query)
# ... more code
end
def query(statement, *params)
#logger.info "#{statement}: #{params}"
#db.exec_params(statement, params)
end
end
The following error is raised on when this line results = query(sql, query) is executed.
PG::IndeterminateDatatype at /search
ERROR: could not determine data type of parameter $1
Another post suggested adding an explicit type cast which is why I added the type cast. I could be doing it incorrectly. I also tried it like the following:
WHERE labels ILIKE '%text($1)%'
WHERE labels ILIKE '%cast($1 as text)%'
WHERE labels ~ '$1::text'
WHERE labels ~ 'cast($1 as text'
In all of the above cases it returned the same error "could not determine the datatype of parameter. I added some #p method calls to make sure the query variable is referencing a real value for debuggin. I have confirmed that this error occurs when the query references a string object with value oats.
What is causing this error to still occur if I am casting the datatype and it is not nil? Am I passing the parameters incorrectly? Am I casting the parameters incorrectly? Is it possible there is a way to pass datatypes as arguments to the #exec_params method? Is there another way to safely pass parameters to be executed by the instance of the PG.connect class?
Simply:
WHERE labels ILIKE $1::text
I assume labels is a plain character type like text, too.

Know type of entity by eyetrace

How can a know an entity before me by GetEyeTrace. I want to check this and if entity == Weapon then do anything...
Whatever like below:
if ply:GetEyeTrace().Entity.IsWeapon() then
print("+")
end
But IsWeapon() function have no.
It should be :IsWeapon()
You should also really be checking if the entity is valid.
local ent = ply:GetEyeTrace().Entity
if(!IsValid(ent)) then return end
if(ent:IsWeapon()) then
print("This is a weapon.")
end

How to use polymorphism to remove a switch statement which compares strings?

I am new to Ruby, so let me describe the context of my problem first:
I have a json as input which has the following key / value pair:
{
"service": "update"
}
The value has many different values for example: insert,delete etc.
Next there is a method x which handles the different requests:
def x(input)
case input[:service]
services = GenericService.new
when "update"
result = services.service(UpdateService.new,input)
when "insert"
result = services.service(InsertService.new,input)
when "delete"
result = services.service(DeleteService.new,input)
....
....
else
raise "Unknown service"
end
puts JSON.pretty_generate(result)
end
What is bothering me is that I still need to use a switch statement to check the String values (reminds me of 'instance of' ugh..). Is there a cleaner way (not need to use a switch)?
Finally I tried to search for an answer to my question and did not succeed, if however I missed it feel free to comment the related question.
Update: I was thinking to maybe cast the string to the related class name as follows: How do I create a class instance from a string name in ruby? and then call result = services.services(x.constantize.new,input) , then the class names ofcourse needs to match the input of the json.
You can try something like:
def x(input)
service_class_name = "#{input[:service].capitalize}Service"
service_class = Kernel.const_get(service_class_name)
service_class.new(input).process
end
In addition you might want to check if this is a valid Service class name at all.
I don't understand why you want to pass the service to GenericService this seems strange. let the service do it's job.
If you're trying to instatiate a class by it's name you're actually speaking about Reflection rather than Polymorphism.
In Ruby you can achieve this in this way:
byName = Object.const_get('YourClassName')
or if you are in a Rails app
byName= 'YourClassName'.constantize
Hope this helps
Just first thoughts, but you can do:
eval(services.service("#{input[:service].capitalize}Service.new, #{input})") if valid_service? input[:service]
def valid_service?
w%(delete update insert).include? input[:service]
end
As folks will no doubt shout, eval needs to be used with alot of care

How to use Resolv::DNS::Resource::Generic

I would like to better understand how Resolv::DNS handles records that are not directly supported. These records are represented by the Resolv::DNS::Resource::Generic class, but I could not find documentation about how to get the data out of this record.
Specifically, my zone will contain SSHFP and TLSA records, and I need a way to get to that data.
Through reverse engineering, I found the answer - documenting it here for others to see.
Please note that this involves undocumented features of the Resolv::DNS module, and the implementation may change over time.
Resource Records that the Resolv::DNS module does not understand are represented not through the Generic class, but rather through a subclass whose name represents the type and class of the DNS response - for instance, an SSHFP record (type 44) will be represented as Resolv::DNS::Resource::Generic::Type44_Class1
The object will contain a method "data" that gives you access to the RDATA of the record, in plain binary format.
Thus, to access an SSHFP record, here is how to get it:
def handle_sshfp(rr) do
# the RDATA is a string but contains binary data
data = rr.data.bytes
algo = data[0].to_s
fptype = data[1].to_s
fp = data[2..-1].to_s
hex = fp.map{|b| b.to_s(16).rr.rjust(2,'0') }.join(':')
puts "The SSHFP record is: #{fptype} #{algo} #{hex}"
end
Resolv::DNS.open do |dns|
all_records = dns.getresources('myfqdn.example.com', Resolv::DNS::Resource::IN::ANY ) rescue nil
all_records.each do |rr|
if rr.is_a? Resolv::DNS::Resource::Generic then
classname = rr.class.name.split('::').last
handle_sshfp(rr) if classname == "Type44_Class1"
end
end
end

Get the type of object that called a registered function in QTP

I'm trying to develop a generic registered function that verifies the existence of a given object, then adds a line to the report to say whether it was found or not.
Here's the function:
'#Description Reports in the test results whether the given object exists.
Public Function verifyExistence(ByRef test_object)
If test_object.Exist(1) Then
Reporter.ReportEvent micPass, "Verify that the page exists.", "Page exists."
Else
Reporter.ReportEvent micFail, "Verify that the page exists.", "Page does not exist."
End If
End Function
RegisterUserFunc "Page", "verifyExistence", "verifyExistence"
This works fine for Page objects, but how can I 'genericize' this so it can be used with any object type? I realize I'll need to add a RegisterUserFunc line for each object type.
Ideally, I'd have a line that looks like this:
typename = getType(test_object)
if typeName = "Page" Then
objName = test_object.GetROProperty("title")
... 'and so on.
end if
Try using test_object.GetTOProperty("micclass") for the test object's name.
Some dynamic objects (e.g. those returned from ChildObjects) may not have this property set on QTP's side in which case you should do test_object.GetROProperty("micclass").
The RO method should always work but its a bit slower than the TO method which should almost always work.
The TypeName() function should return (nearly) the type name of the object, e.g. "IRegExp2" for a RegExp.

Resources