How I can use copyDatabase function, through Mongoid?
# something like that
::Mongoid::Sessions.default.command('copyDatabase','production','staging')
Related
Let's say I have a spinnaker parameter like this:
parameters.region = "us-east1,us-east2,us-west1"
I want to update a Configmap using that parameter, but I'd like to transform that into a list in my yaml file instead of just the string. Is there a way to do that with SPeL?
For instance, here's what I would like it to look like:
data:
regions.yaml: |
regions:
- us-east1
- us-east2
- us-west1
I see that there is a split(,) method I can use, but I can't figure out how I could loop over that in my output.
Given:
class Thing
def initialize(object)
#object = object
end
end
items = [1,2,3]
I'd like to know of a more elegant way to convert each item to the Thing than this:
items.map{ |item| Thing.new item }
# => [<Thing #object=1>, <Thing #object=2>, <Thing #object=3>]
You can use the unary prefix & operator:
items.map(&Thing.method(:new))
I have suggested that Classes should behave as Factory Functions, which would allow you to write it like this:
items.map(&Thing)
However, there doesn't seem to be much interest in the proposal. You can monkey-patch it yourself, though, the implementation is trivial:
class Class
def to_proc
method(:new).to_proc
end
end
I would argue that your example is perfectly fine. But perhaps you like something like this:
# in item.rb
def to_thing
Thing.new(self)
end
That would allow you to write:
items.map(&:to_thing)
I am new to Rails and am currently learning strong parameters in Rails 4 and following the below example from the official documentation:
`class PeopleController < ActionController::Base
# Using "Person.create(params[:person])" would raise an
# ActiveModel::ForbiddenAttributes exception because it'd
# be using mass assignment without an explicit permit step.
# This is the recommended form:
def create
Person.create(person_params)
end
# This will pass with flying colors as long as there's a person key in the
# parameters, otherwise it'll raise an ActionController::MissingParameter
# exception, which will get caught by ActionController::Base and turned
# into a 400 Bad Request reply.
def update
redirect_to current_account.people.find(params[:id]).tap { |person|
person.update!(person_params)
}
end
private
# Using a private method to encapsulate the permissible parameters is
# just a good pattern since you'll be able to reuse the same permit
# list between create and update. Also, you can specialize this method
# with per-user checking of permissible attributes.
def person_params
params.require(:person).permit(:name, :age)
end
end`
Question 1:
What does current_account.people.find mean inside the update method?
Question 2:
Could someone please explain the person_params method. What is "params" inside the person_params method?
current_account is a most likely a private method that returns an Account instance. current_account.people.find(params[:id]) searches the people table for a person that belongs to the current_account and has an ID of params[:id]. Object#tap is a ruby method that yields a block with the current object, and then returns that object. In this case, the Person instance is updated inside the block and the returned from tap. Finally, redirect_to is a controller method that will redirect the request to a different path. redirect_to can take many different types of arguments, including an ActiveRecord model, a string, or a symbol. Passing it an ActiveRecord model will redirect the request to the model's resource path, which is defined in routes.rb. In this case, that path will most likely be /people/:id.
The params object is a hash containing parameter names and values. For example, the request /people?name=Joe&age=34 will result in the following params object: {name: 'Joe', age: '34'}.
I have a google-cloud-endpoints, in the docs, I did'nt find how to write a PATCH method.
My request:
curl -XPATCH localhost:8080/_ah/api/hellogreeting/1 -d '{"message": "Hi"}'
My method handler looks like this:
from models import Greeting
from messages import GreetingMessage
#endpoints.method(ID_RESOURCE, Greeting,`
path='hellogreeting/{id}', http_method='PATCH',
name='greetings.patch')
def greetings_patch(self, request):
request.message, request.username
greeting = Greeting.get_by_id(request.id)
greeting.message = request.message # It's ok, cuz message exists in request
greeting.username = request.username # request.username is None. Writing the IF conditions in each string(checking on empty), I think it not beatifully.
greeting.put()
return GreetingMessage(message=greeting.message, username=greeting.username)
So, now in Greeting.username field will be None. And it's wrong.
Writing the IF conditions in each string(checking on empty), I think it not beatifully.
So, what is the best way for model updating partially?
I do not think there is one in Cloud Endpoints, but you can code yours easily like the example below.
You will need to decide how you want your patch to behave, in particular when it comes to attributes that are objects : should you also apply the patch on the object attribute (in which case use recursion) or should you just replace the original object attribute with the new one like in my example.
def apply_patch(origin, patch):
for name in dir( patch ):
if not name.startswith( '__' ):
setattr(origin,name,getattr(patch,name))
I try to set an NSTabView delegate using MacRuby with XCode, but I can't figure how to write the delegate. I use:
def intialize
#tab_changed.delegate = self
end
def tabViewdidSelectTabViewItem(a_notification)
puts "tab has changed"
end
Then in the .xib, I hook the NSTab view element with the class, but nothing happen when I select some tabs.
Usually the delegate are very easy to use, but this one has a syntax like this :
tabView:didSelectTabViewItem:
and I don't know how to write this in MacRuby. Should I use tabViewdidSelectTabViewItem or tabView_didSelectTabViewItem (none of them works).
Thanks for your help.
Assuming this is being done in a ViewController, instead of using initialize, better to do things the Cocoa way and use a method like viewDidLoad.
def viewDidLoad
#tab_changed.delegate = self
end
The signature for the delegate method you want is -(void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem. In MacRuby, that would be represented like this:
def tabView(tabView, didSelectTabViewItem: tabViewItem)
puts "tab has changed"
end