Class: Pbw::Area
- Inherits:
-
Object
- Object
- Pbw::Area
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- app/models/pbw/area.rb
Class Method Summary collapse
- .creatable_by?(user, subject) ⇒ Boolean
- .deletable_by?(user, subject) ⇒ Boolean
- .editable_by?(user, subject) ⇒ Boolean
- .viewable_by?(user, subject) ⇒ Boolean
Instance Method Summary collapse
- #add_constraint!(constraint) ⇒ Object
- #add_item!(item, quantity) ⇒ Object
- #after_add_item(item, quantity) ⇒ Object
- #after_remove_item(item, quantity) ⇒ Object
- #after_token_enters(token) ⇒ Object
- #after_token_leaves(token) ⇒ Object
- #attach_tick_process(process, ticks_to_wait = 0) ⇒ Object
- #attach_update_process(process, updates_to_wait = 0) ⇒ Object
- #before_add_item(item, quantity) ⇒ Object
- #before_remove_item(item, quantity) ⇒ Object
- #before_token_enters(token) ⇒ Object
- #before_token_leaves(token) ⇒ Object
- #check_constraints_and_capabilities(&changeset) ⇒ Object
- #check_triggers! ⇒ Object
- #count_item(item) ⇒ Object
- #has_constraint?(constraint) ⇒ Boolean
- #remove_constraint!(constraint) ⇒ Object
- #remove_item!(item, quantity) ⇒ Object
Class Method Details
.creatable_by?(user, subject) ⇒ Boolean
21 22 23 |
# File 'app/models/pbw/area.rb', line 21 def self.creatable_by?(user, subject) user.admin? end |
.deletable_by?(user, subject) ⇒ Boolean
29 30 31 |
# File 'app/models/pbw/area.rb', line 29 def self.deletable_by?(user, subject) user.admin? end |
.editable_by?(user, subject) ⇒ Boolean
25 26 27 |
# File 'app/models/pbw/area.rb', line 25 def self.editable_by?(user, subject) user.admin? end |
.viewable_by?(user, subject) ⇒ Boolean
17 18 19 |
# File 'app/models/pbw/area.rb', line 17 def self.viewable_by?(user, subject) true end |
Instance Method Details
#add_constraint!(constraint) ⇒ Object
101 102 103 104 105 106 107 |
# File 'app/models/pbw/area.rb', line 101 def add_constraint!(constraint) return false unless constraint && constraint.before_add(self) self.constraints << constraint save constraint.after_add(self) self end |
#add_item!(item, quantity) ⇒ Object
82 83 84 85 86 87 |
# File 'app/models/pbw/area.rb', line 82 def add_item!(item, quantity) return false unless quantity && quantity.responds_to?(:abs) return remove_item!(item, quantity.abs) if quantity < 0 return false unless item && before_add_item(item,quantity) ItemContainer.find_or_create_for_area(self, item, quantity) end |
#after_add_item(item, quantity) ⇒ Object
56 57 58 |
# File 'app/models/pbw/area.rb', line 56 def after_add_item(item, quantity) # stub method end |
#after_remove_item(item, quantity) ⇒ Object
65 66 67 |
# File 'app/models/pbw/area.rb', line 65 def after_remove_item(item, quantity) # stub method end |
#after_token_enters(token) ⇒ Object
38 39 40 |
# File 'app/models/pbw/area.rb', line 38 def after_token_enters(token) # stub method end |
#after_token_leaves(token) ⇒ Object
47 48 49 |
# File 'app/models/pbw/area.rb', line 47 def after_token_leaves(token) # stub method end |
#attach_tick_process(process, ticks_to_wait = 0) ⇒ Object
69 70 71 |
# File 'app/models/pbw/area.rb', line 69 def attach_tick_process(process, ticks_to_wait=0) AttachedProcess.create(area: self, process: process, tickable: true, ticks_waiting: ticks_to_wait) end |
#attach_update_process(process, updates_to_wait = 0) ⇒ Object
73 74 75 |
# File 'app/models/pbw/area.rb', line 73 def attach_update_process(process, updates_to_wait=0) AttachedProcess.create(area: self, process: process, updatable: true, updates_waiting: updates_to_wait) end |
#before_add_item(item, quantity) ⇒ Object
51 52 53 54 |
# File 'app/models/pbw/area.rb', line 51 def before_add_item(item, quantity) # stub method true end |
#before_remove_item(item, quantity) ⇒ Object
60 61 62 63 |
# File 'app/models/pbw/area.rb', line 60 def before_remove_item(item, quantity) # stub method true end |
#before_token_enters(token) ⇒ Object
33 34 35 36 |
# File 'app/models/pbw/area.rb', line 33 def before_token_enters(token) # stub method true end |
#before_token_leaves(token) ⇒ Object
42 43 44 45 |
# File 'app/models/pbw/area.rb', line 42 def before_token_leaves(token) # stub method true end |
#check_constraints_and_capabilities(&changeset) ⇒ Object
117 118 119 |
# File 'app/models/pbw/area.rb', line 117 def check_constraints_and_capabilities(&changeset) self.constraints.any?{|c| !c.before_process(self, changeset)} end |
#check_triggers! ⇒ Object
121 122 123 |
# File 'app/models/pbw/area.rb', line 121 def check_triggers! self.triggers.each{|t| t.check! } end |
#count_item(item) ⇒ Object
77 78 79 80 |
# File 'app/models/pbw/area.rb', line 77 def count_item(item) container = ItemContainer.find_for_area(self) container ? container.quantity : 0 end |
#has_constraint?(constraint) ⇒ Boolean
96 97 98 99 |
# File 'app/models/pbw/area.rb', line 96 def has_constraint?(constraint) constraint = Constraint.find(constraint) if constraint.is_a?(String) self.constraints.include?(constraint) end |
#remove_constraint!(constraint) ⇒ Object
109 110 111 112 113 114 115 |
# File 'app/models/pbw/area.rb', line 109 def remove_constraint!(constraint) return false unless constraint && constraint.before_remove(self) self.constraints.delete_if{|c| c.name == constraint.name} save constraint.after_remove(self) self end |
#remove_item!(item, quantity) ⇒ Object
89 90 91 92 93 94 |
# File 'app/models/pbw/area.rb', line 89 def remove_item!(item, quantity) return false unless quantity && quantity.responds_to?(:abs) return add_item!(item, quantity.abs) if quantity < 0 return false unless item && before_remove_item(item,quantity) ItemContainer.find_or_create_for_area(self, item, (0 - quantity)) end |