Module: ModularStateMachine::ClassMethods
- Defined in:
- lib/modular_state_machine.rb
Instance Method Summary collapse
-
#build_boolean_instance_methods(attribute, options = []) ⇒ Object
Builds out the boolean methods for constants defined in
mod_machine_for. -
#build_mutable_instance_methods(attribute, options = []) ⇒ Object
Builds out mutable update methods for constants defined in
state_machine_for. -
#state_machine_for(attribute, options = []) ⇒ Object
Builds a module with constants assigned to the index passed in the
optionsarray.
Instance Method Details
#build_boolean_instance_methods(attribute, options = []) ⇒ Object
Builds out the boolean methods for constants defined in mod_machine_for.
Arguments must follow the same format as the aforementioned method.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/modular_state_machine.rb', line 42 def build_boolean_instance_methods(attribute, = []) .each do |const| puts const.class class_eval <<-RUBY def #{const.to_s.split("::").last.tableize.singularize}? self.#{attribute.downcase} == #{attribute}::#{const.to_s.demodulize} end RUBY end end |
#build_mutable_instance_methods(attribute, options = []) ⇒ Object
Builds out mutable update methods for constants defined in state_machine_for.
Arguments must follow the same format as the aforementioned method.
example object.case_study! #=> UPDATE "objects" SET "category" = 2 WHERE "objects"."id" = 1; object.case_study? #=> true
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/modular_state_machine.rb', line 64 def build_mutable_instance_methods(attribute, = []) column = ":" + attribute.to_s.downcase .each do |const| class_eval <<-RUBY def #{const.to_s.demodulize.tableize.singularize}! update_attribute #{column}, #{attribute}::#{const.to_s.demodulize} end RUBY end end |
#state_machine_for(attribute, options = []) ⇒ Object
Builds a module with constants assigned to the index passed in the options array. Be sure
to pass all arguments as you would write them manually. In other words attribute and any
constants passed in options should be classified case (e.g. ThisModule).
attribute = Name of the class attribute that should be the module"s name. MUST be a attribute
of the class.
options = Array of constants identified with attribute
example state_machine_for("Category", ["PeerReviewed", "NonPeerReviewed", "CaseStudy"])
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/modular_state_machine.rb', line 27 def state_machine_for(attribute, = []) constants = "" .each_with_index do |constant, index| constants << "#{constant} = #{index}; " end mod = %(module #{attribute}; #{constants} def self.names; #{.map(&:to_sym)}; end; end) module_eval(mod) build_boolean_instance_methods(attribute, ) build_mutable_instance_methods(attribute, ) end |