Ruby add dynamic events using AASM - ruby

I've got a class in a program which is handling game states. I'm actually handling it with AASM so to create an event I have to use something like aasm_event :name ... inside the class.
I need to be able to load other files who dynamically have to add events and states to the class.
How is it possible ?
Thank you in advance.

The following should work, unless aasm_state and aasm_event are protected or private:
# game.rb
class Game
include AASM
aasm_initial_state :start
end
# add the require after the class definition, else it will complain of a missing constant
require "other_file"
# other_file.rb
Game.aasm_state :another_state
Game.aasm_event do
transitions :to => :another_state, :from => [:start]
end

Related

How to pass a parameter to new/init in AASM gem

I'm trying to make a finite state machine chain with AASM gem. I want to check if a string is unique (not existing in the database).
I wrote:
require 'rubygems'
require 'aasm'
class Term
include AASM
aasm do
state :Beginning, :initial => true
state :CheckUniqueness
def initialize(term)
print term
end
event :UniquenessChecking do
print "Check uniqueness"
transitions :from => :Beginning, :to => :CheckUniqueness
end
end
end
term = Term.new("textstring")
term.CheckUniqueness
But when I use Term.new("textstring"), it doesn't allow me to pass a parameter I think, because I get a error:
`initialize': wrong number of arguments (1 for 0) (ArgumentError)
from try.rb:24:in `new'
from try.rb:24:in `<main>'
Is it possible to pass a parameter with init in AASM? I would like to know how I can do that?
You defined initialize inside of the aasm block, just move it out of that block:
require 'rubygems'
require 'aasm'
class Term
include AASM
def initialize(term)
print term
end
aasm do
# ...
end
end

How can I cause the state_machine block to use the default accessor instead of my custom accessor?

I'm implementing pluginaweek's state_machine gem. I narrowed the code down to just this so I can understand the problem more easily. Let's assume I only have one status for now:
class Event < ActiveRecord::Base
state_machine :status, :initial => :active_offer do
end
def status
'Active Offer'
end
end
The error I'm getting when creating new Event objects either via seeding or via the browser is {:status=>["is invalid"]}.
The plan is to include a condition for all the different statuses and return a custom string to the view. The entire project's views currently use the .status syntax so I'm trying to smoothly install this. I started this solution based on reading the api docs:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#label-Overwriting+default+accessors
This is my main goal:
def status
if read_attribute(:status) == 'active_offer' && self.start_date > Time.now
'Active Offer'
elsif read_attribute(:status) == 'active_offer' && self.start_date < Time.now
'Expired'
else read_attribute(:status) == 'cancelled'
'Cancelled'
end
end
What can I do to make the state_machine block use the normal accessor so it gets the database value?
THE SILVER BULLET SOLUTION:
My main problem was the accessor override status. When state_machine code was running, it was reading the current status through my accessor override and therefore getting a custom string returned, which was an invalid status. I had to make state_machine use :state. I didn't do this originally because I already had a :state attr for provinces and such, so I migrated that to :address_state.
Pretty sure you just need to change the definition's name:
class Event < ActiveRecord::Base
state_machine :status, :initial => :active_offer do
end
def active_offer
'Active Offer'
end
end
EDIT:
If I understand you correctly, which I'm not certain I do, this will work:
state_machine :status, initial: :active_offer do
event :expire do
transition all => :expired
end
event :cancel do
transition all => :cancelled
end
end
Then you can do your if... statements, etc., in the controller or something, and transition them with something like #event.expire or if #event.expired. If you want it automated you'll need something like whenever.

How to call a page-object from a class.rb at Support folder

I am using the page-object gem. Suppose i have a page-object on features/bussines/pages/booking_page.rb for a page like:
class Booking
include PageObject
span(:txtFirstName, :id => 'details_first_name')
end
...and i use a "tools" class located at features/support/tools.rb with something like:
class MyTools
def call_to_page_object
on Booking do |page|
puts page.txtFirstName
end
end
end
...but this approach fails because calling to the object from the class is not allowed:
undefined method `on' for #<Booking:0x108f5b0c8> (NoMethodError)
Pretty sure i'm missing some concept on the way to use the page-object from a class but don't realize whats the problem. Can you please give me an idea about what could be wrong here, please?
Thank you very much!
============================
Justin found the reason why the call to the class crash. The final class code results:
class MyTools
#Include this module so that the class has the 'on' method
include PageObject::PageFactory
def initialize(browser)
#Assign a browser object to #browser, which the 'on' method assumes to exist
#browser = browser
end
def getCurrentRewards
on Booking do |page|
rewards_text = page.rewards_amount
rewards_amount = rewards_text.match(/(\d+.*\d*)/)[1].to_f
puts "The current rewards amount are: #{rewards_amount}."
return rewards_amount
end
end
end
And the call to the function:
user_rewards = UserData.new(#browser).getCurrentRewards
Why it did not work me? Two main reasons:
I didn't pass the browser object to the class <== REQUIRED
I didn't include the PageObject::PageFactory in the class <== REQUIRED for the "on" method.
Thanks all!
To use the on (or on_page) method requires two things:
The method to be available, which is done by including the PageObject::PageFactory module.
Having a #browser variable (within the scope of the class) that is the browser.
So you could make your MyTools class work by doing:
class MyTools
#Include this module so that the class has the 'on' method
include PageObject::PageFactory
def initialize(browser)
#Assign a browser object to #browser, which the 'on' method assumes to exist
#browser = browser
end
def call_to_page_object
on Booking do |page|
puts page.txtFirstName
end
end
end
You would then be calling your MyTools class like:
#Assuming your Cucumber steps have the the browser stored in #browser:
MyTools.new(#browser).call_to_page_object
What are you trying to do?
Did you read Cucumber & Cheese book?
Pages should be in the features/support/pages folder. You can put other files that pages need there too.
If you want to use on method in a class, you have to add this to the class:
include PageObject
The code from MyTools class looks to me like it should be in Cucumber step file, not in a class.
Your class should use the extend keyword to access special class methods like span:
class Booking
extend PageObject
span(:txtFirstName, :id => 'details_first_name')
end
I hope this works.

Dynamic State Machine in Ruby? Do State Machines Have to be Classes?

Question is, are state machines always defined statically (on classes)? Or is there a way for me to have it so each instance of the class with has it's own set of states?
I'm checking out Stonepath for implementing a Task Engine. I don't really see the distinction between "states" and "tasks" in there, so I'm thinking I could just map a Task directly to a state. This would allow me to be able to define task-lists (or workflows) dynamically, without having to do things like:
aasm_event :evaluate do
transitions :to => :in_evaluation, :from => :pending
end
aasm_event :accept do
transitions :to => :accepted, :from => :pending
end
aasm_event :reject do
transitions :to => :rejected, :from => :pending
end
Instead, a WorkItem (the main workflow/task manager model), would just have many tasks. Then the tasks would work like states, so I could do something like this:
aasm_initial_state :initial
tasks.each do |task|
aasm_state task.name.to_sym
end
previous_state = nil
tasks.each do |tasks|
aasm_event task.name.to_sym do
transitions :to => "#{task.name}_phase".to_sym, :from => previous_state ? "#{task.name}_phase" : "initial"
end
previous_state = state
end
However, I can't do that with the aasm gem because those methods (aasm_state and aasm_event) are class methods, so every instance of the class with that state machine has the same states. I want it so a "WorkItem" or "TaskList" dynmically creates a sequence of states and transitions based on the tasks it has.
This would allow me to dynamically define workflows and just have states map to tasks.
Are state machines ever used like this? It seems that this ruby workflow gem is similar to what I'm describing.
Update: I can see doing something like the following, but it seems sort of hackish:
#implementation_state_machine = Class::new do
include AASM
aasm_initial_state :initial
tasks.each { |state| aasm_state :"#{task.name}"}
# ...
end
... where a property on my model would be implementation_state_machine. I'd have to override method_missing to delegate state-related methods (accepted_phase?) to the implementation anonymous class.
Yeah, that does seem very hacky and quite messy. I wrote a new gem recently that allows you to use dynamic 'to' transitions with a decision setting.
So instead of building your events and transitions dynamically, would be it be possible to map them out first, and use the decide setting to allow the transition decide which new state to enter? You can also wrap your from transition in an array so you wouldn't need to do :from => previous_state ? "#{task.name}_phase" : "initial", you could just do :from => [ :cool_task_phase, :initial ]
I find that setting out your transitions and events out first, allows you to get a greater picture on what your model is doing.
Check it out at http://github.com/ryanza/stateflow
Hopefully you can find some use out of this.
In my implementation state machine is a hash https://github.com/mpapis/state_attr
state_attr :state, {
nil => :first,
:first => [:second, :third],
:second => :last,
:third => nil,
}
you can define as many state attributes as you like
BTW: in the background there is still a class but only as a proxy to attribute

Should the Applicant class "require 'mad_skills'" or "include 'mad_skills'"?

Also, what does "self.send attr" do? Is attr assumed to be a private instance variable of the ActiveEngineer class? Are there any other issues with this code in terms of Ruby logic?
class Applicant < ActiveEngineer
require 'ruby'
require 'mad_skills'
require 'oo_design'
require 'mysql'
validates :bachelors_degree
def qualified?
[:smart, :highly_productive, :curious, :driven, :team_player ].all? do
|attr|
self.send attr
end
end
end
class Employer
include TopTalent
has_millions :subscribers, :include=>:mostly_women
has_many :profits, :revenue
has_many :recent_press, :through=>[:today_show, :good_morning_america,
:new_york_times, :oprah_magazine]
belongs_to :south_park_sf
has_many :employees, :limit=>10
def apply(you)
unless you.build_successful_startups
raise "Not wanted"
end
unless you.enjoy_working_at_scale
raise "Don't bother"
end
end
def work
with small_team do
our_offerings.extend you
subscribers.send :thrill
[:scaling, :recommendation_engines, : ].each do |challenge|
assert intellectual_challenges.include? challenge
end
%w(analytics ui collaborative_filtering scraping).each{|task|
task.build }
end
end
end
def to_apply
include CoverLetter
include Resume
end
require 'mad_skills' loads the code in mad_skills.rb (or it loads mad_skills.so/.dll depending on which one exists). You need to require a file before being able to use classes, methods etc. defined in that file (though in rails files are automatically loaded when trying to access classes that have the same name as the file). Putting require inside a class definition, does not change its behaviour at all (i.e. putting it at the top of the file would not make a difference).
include MadSkills takes the module MadSkills and includes it into Applicant's inheritance chain, i.e. it makes all the methods in MadSkills available to instances of Applicant.
self.send attr executes the method with the name specified in attr on self and returns its return value. E.g. attr = "hello"; self.send(attr) will be the same as self.hello. In this case it executes the methods smart, highly_productive, curious, driven, and team_player and checks that all of them return true.

Resources