How to create hash with automatically generated ranges in rails - ruby-on-rails

I want to create a simple timer, that start from 0, has_many points and stop working when i push stop button.
Timer.start == 0
Timer.point = Time.now
Timer.fin = Time.now
I would like to create a timeline hash with ranges for every points in seconds.
Something looks like Timeline.new(timer.point.first[0..4], timer.point.second[5..11] ... timer.point.last [42..56])
I think I should use an iteration, but have no idea how setup point[from..till] arguments. Sorry, don't have much practice.
Any solutions, ideas?
TY4HLP

You can try to use while loop for your task

Related

Executing same function every monday at midnight

In Rails 5, I have this method in my "Aulas" ("Classes" in Portuguese) Controller:
def set_week_classes
classes = Aula.all.to_a
#this_week_classes = classes.shift(2)
end
Considering that "classes" is an array, I would like to have "#this_week_classes = classes.shift(2)" being executed every Monday, at midnight (Brazil's time), getting the next two items of the classes array to be shown on the view. And also, I would like that when it reached the end of the array, it simply started all over, making "#this_week_classes" become again the first two items of the classes array. How could I make this happen? Thank you!
You can use sidekiq with some kind of a scheduling gem (like sidekiq-scheduler or sidekiq-cron). Depending on your installation you could also copy use a rake task and run it periodically using cron. If you use cloud then your provider definitely have some kind of scheduler available.
BTW all of your source code should probably be in English. Mixing some Portuguese class names doesn't look great and can be confusing for other contributors.
But if your only goal is to show some what classes are listed this week it's probably better to do sth like this:
classes = Aula.all.to_a # not the best for the memory
shift = DateTime.current.weeks_since(CONSTANT_TIME) % classes.size
#this_week_classes = ([classes]+[classes])[shift..(shift+2)] # [classes]+[classes] make sure that we won't get too little classes if we reach the and of the `classes` array
It appears you ask two questions at the same time:
Run a job every week at a certain time.
Rails offers different ways to do this. To me the best fit seems to work with an active job library.
Possibilities are:
https://github.com/javan/whenever cron runner. Let's you set up jobs for every week to run.
Another open library for this type of task is the gem delayed_job. It's not very performant but easy to include into small projects.
Cycle through an array of items.
Here a possibility is instead of actually shifting the items out of your array that you store the job run in your database. Keep in mind there are other possibilities that do not need you to change your database. Following code is not tested but should be seen as pseudocode.
def run_job
last_aula_job = AulaJob.all.order(:created_at).last
classes = Aula.all.to_a
total = classes.count
p = last_aula_job.last_pointer % count
#this_week_classes = classes[p..p+1]
# do something with #this_week_classes
AulaJob.create(last_pointer: p + 2)
end

rails hash extract first value from list

I am trying to normalize some data in an ETL process because the data we get is not consistent.
Annoying but i am here to learn.
currently we do something like:
received = datum[:quantity_received] || datum[:received_cases] || datum[:received_quantity]
Curious if there is a more ruby/rails way of doing this?
considering:
received = datum.values_at(:quantity_received,:received_cases,:received_quantity).compact.first
I don't think there is a much better solution. I'd try to define some helper methods (I'm not a long lines' supporter)
def value_you_need(datum)
datum.values_at(*keys_of_interest).find(&:itself)
end
def keys_of_interest
%i(quantity_received received_cases received_quantity)
end
received = value_you_need(datum)
object#itself method is present from ruby 2.2.0. Otherwise, go for compact.first.
Note a detail: if false is one of the values you care about the solution with compact.first is the only one correct of our three. But I took for granted you don't or the first option would be wrong.

How to enqueue a job within a job dynamically in Rails?

I've a job, and when the job is run, at the very bottom of it, and I want to enqueue the same job again to run after 1 hour, but with different arguments.
What I've achieved so far:
class SimpleJob
#queue = :normal
def self.perform(start)
puts "Right now, start = #{start}"
start += 12
time = some_request_external_api
self.set(wait: time).perform_later(start)
end
end
I'm using resque gem, and running the job through QUEUE=* rake resque:work. Surely, it prints Right now, start = 12 in beginning, but after that, nothing happens. How exactly can I achieve this functionality?
Rather than enqueuing the job again within itself, you could either use a scheduler, such as clockwork.
Or, if what you are trying to accomplish is in response to some event on another service, maybe you could look into it's documentation and see if it provides webhook functionality.
These would send post requests to your desired action whenever any action occurs on their side.
Sounds like you are looking for ways to setup recurring job. If thats the case, take a look at this:
https://github.com/resque/resque-scheduler

cycle() an image_tag

I'm want to display different image version:
first article: big banner
second: small banner that float to right/left
so, first thing: use cycle() but dont work:
= cycle(image_tag(banner_big), image_tag(banner_small)
or
= image_tag(cycle(banner_big_path, banner_small_path))
Only first image is displayed
There's a proper way to make one like that ?
Your problem is that rails is expecting you to call cycle with the same set of strings each time. At the moment you're passing a different pair of strings to each call to cycle, so rails resets the cycle each time. New cycles always start with their first value, hence the result you describe.
Assuming your articles had methods called small_path, big_path, something like
article.send(cycle("big_path","small_path"))
Should return alternate image paths.
You can make use of the session facility to store indexes there and use those. For instance:
# application_helper.rb
def session_banner_index
session[:banner_index] || 0
end
def session_banner(*list)
list[session_banner_index % list.length]
end
# application_controller.rb
def increment_session_banner_index!
session[:banner_index] = (session[:banner_index] || 0) + 1
end
These helper methods approximate the interface you were asking for:
= image_tag(session_banner(banner_big, banner_small))

datetime_select with zeroed minutes

I know that I can use the component parts of the date helpers, rather than the full
datetime_select, but I'm not sure how it would work as far as combining the params.
A bit of background, I'm creating an app for traffic monitoring where people can log traffic counts in 1 hour blocks. I'm presenting the user with a datetime_select so they can specify the start of the block, then later I'm calculating the end.
So I don't want people to be able to submit minutes or seconds, well seconds aren't shown with the helper so that's a start.
I've tried to zero it before the record is created with something like:
params[:result]['start(5i)'] = 0
which is the key that the development log shows rails is using for minutes. Unfortunately I get:
undefined method `empty?' for 0:Fixnum
I guess I could do this with some javascript, hide the minutes select box and remove all but the "00" option. I'd rather find a nice, clean solution if I can though.
Grateful for some tips. Happy to provide more information but not sure what else might be of use at the moment.
params are Strings! try this:
params[:result]['start(5i)'] = '0'
or this:
params[:result]['start(5i)'] = ''

Resources