1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Better RubyMotion file management.

CoreDataQuery persistence <3
This commit is contained in:
Yannick Rekinger 2016-03-18 02:53:19 +01:00
parent 6863096299
commit 2b9d850d99
6 changed files with 124 additions and 5 deletions

View file

@ -12,5 +12,4 @@ require 'aasm/core/state'
require 'aasm/localizer'
require 'aasm/state_machine'
require 'aasm/persistence'
require 'aasm/persistence/plain_persistence' # RubyMotion support
require 'aasm/aasm'

View file

@ -17,10 +17,7 @@ module AASM
module ClassMethods
# make sure inheritance (aka subclassing) works with AASM
def inherited(base)
AASM::StateMachine[base] = {}
AASM::StateMachine[self].keys.each do |state_machine_name|
AASM::StateMachine[base][state_machine_name] = AASM::StateMachine[self][state_machine_name].clone
end
AASM::StateMachine.inherit(self, base)
super
end

View file

@ -16,6 +16,8 @@ module AASM
include_persistence base, :sequel
elsif hierarchy.include?("Dynamoid::Document")
include_persistence base, :dynamoid
elsif hierarchy.include?("CDQManagedObject")
include_persistence base, :core_data_query
else
include_persistence base, :plain
end

View file

@ -0,0 +1,88 @@
require_relative 'base'
module AASM
module Persistence
module CoreDataQueryPersistence
# This method:
#
# * extends the model with ClassMethods
# * includes InstanceMethods
#
# Adds
#
# after_initialize :aasm_ensure_initial_state
#
def self.included(base)
base.send(:include, AASM::Persistence::Base)
base.send(:include, AASM::Persistence::CoreDataQueryPersistence::InstanceMethods)
base.after_initialize :aasm_ensure_initial_state
end
module InstanceMethods
# Writes <tt>state</tt> to the state column and persists it to the database
# using update_attribute (which bypasses validation)
#
# foo = Foo.find(1)
# foo.aasm.current_state # => :opened
# foo.close!
# foo.aasm.current_state # => :closed
# Foo.find(1).aasm.current_state # => :closed
#
# NOTE: intended to be called from an event
def aasm_write_state(state, name=:default)
raise "Cowardly refusing to save the current CoreDataQuery context"
aasm_write_state_without_persistence(state, name)
end
# Writes <tt>state</tt> to the state column, but does not persist it to the database
#
# foo = Foo.find(1)
# foo.aasm.current_state # => :opened
# foo.close
# foo.aasm.current_state # => :closed
# Foo.find(1).aasm.current_state # => :opened
# foo.save
# foo.aasm.current_state # => :closed
# Foo.find(1).aasm.current_state # => :closed
#
# NOTE: intended to be called from an event
def aasm_write_state_without_persistence(state, name=:default)
write_attribute(self.class.aasm(name).attribute_name, state.to_s)
end
private
# Ensures that if the aasm_state column is nil and the record is new
# that the initial state gets populated before validation on create
#
# foo = Foo.new
# foo.aasm_state # => nil
# foo.valid?
# foo.aasm_state # => "open" (where :open is the initial state)
#
#
# foo = Foo.find(:first)
# foo.aasm_state # => 1
# foo.aasm_state = nil
# foo.valid?
# foo.aasm_state # => nil
#
def aasm_ensure_initial_state
AASM::StateMachine[self.class].keys.each do |state_machine_name|
send("#{self.class.aasm(state_machine_name).attribute_name}=", aasm(state_machine_name).enter_initial_state.to_s) if send(self.class.aasm(state_machine_name).attribute_name).blank?
end
end
end # InstanceMethods
# module NamedScopeMethods
# def aasm_state_with_named_scope name, options = {}
# aasm_state_without_named_scope name, options
# self.named_scope name, :conditions => { "#{table_name}.#{self.aasm.attribute_name}" => name.to_s} unless self.respond_to?(name)
# end
# end
end
end # Persistence
end # AASM

View file

@ -10,6 +10,13 @@ module AASM
(@machines ||= {})[klass.to_s] = machine
end
def self.inherit(klass, alias_klass)
AASM::StateMachine[alias_klass] = {}
AASM::StateMachine[klass].keys.each do |state_machine_name|
AASM::StateMachine[alias_klass][state_machine_name] = AASM::StateMachine[klass][state_machine_name].clone
end
end
attr_accessor :states, :events, :initial_state, :config, :name, :global_callbacks
def initialize(name)

26
lib/motion-aasm.rb Normal file
View file

@ -0,0 +1,26 @@
unless defined?(Motion::Project::App)
raise "This must be required from within a RubyMotion Rakefile"
end
file_dependencies = {
'aasm/persistence/base.rb' => ['aasm/base.rb']
}
exclude_files = [
'rspec'
]
Motion::Project::App.setup do |app|
parent = File.expand_path File.dirname(__FILE__)
app.files += Dir.glob(File.join(parent, "aasm/**/*.rb")).reject { |file| exclude_files.any? { |exclude| file.match(exclude) } }
app.files_dependencies file_dependencies.inject({}, &->(file_dependencies, (file, *dependencies)) do
file = File.join(parent, file)
dependencies = dependencies.flatten(1).map do |dependency|
File.join(parent, dependency)
end
file_dependencies.merge({ file => dependencies })
end)
end