Class: Pbw::Token

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/pbw/token.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.creatable_by?(user, subject) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/pbw/token.rb', line 22

def self.creatable_by?(user, subject)
    true
end

.deletable_by?(user, subject) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/pbw/token.rb', line 30

def self.deletable_by?(user, subject)
    user.admin? || subject.user == user
end

.editable_by?(user, subject) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'app/models/pbw/token.rb', line 26

def self.editable_by?(user, subject)
    user.admin? || subject.user == user
end

.viewable_by?(user, subject) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'app/models/pbw/token.rb', line 18

def self.viewable_by?(user, subject)
    user.admin? || subject.user == user
end

Instance Method Details

#add_capability!(capability) ⇒ Object



148
149
150
151
152
153
154
# File 'app/models/pbw/token.rb', line 148

def add_capability!(capability)
    return false unless capability && capability.before_add(self)
    self.capabilities << capability
    save
    capability.after_add(self)
    self
end

#add_constraint!(constraint) ⇒ Object



127
128
129
130
131
132
133
# File 'app/models/pbw/token.rb', line 127

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



87
88
89
90
91
92
# File 'app/models/pbw/token.rb', line 87

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_token(self, item, quantity)
end

#after_add_item(item, quantity) ⇒ Object



57
58
59
# File 'app/models/pbw/token.rb', line 57

def after_add_item(item, quantity)
    # stub method
end

#after_move(area) ⇒ Object



48
49
50
# File 'app/models/pbw/token.rb', line 48

def after_move(area)
    # stub method
end

#after_ownership(user) ⇒ Object



39
40
41
# File 'app/models/pbw/token.rb', line 39

def after_ownership(user)
    # stub method
end

#after_remove_item(item, quantity) ⇒ Object



66
67
68
# File 'app/models/pbw/token.rb', line 66

def after_remove_item(item, quantity)
    # stub method
end

#attach_tick_process(process, ticks_to_wait = 0) ⇒ Object



70
71
72
# File 'app/models/pbw/token.rb', line 70

def attach_tick_process(process, ticks_to_wait=0)
    AttachedProcess.create(token: self, process: process, tickable: true, ticks_waiting: ticks_to_wait)
end

#attach_update_process(process, updates_to_wait = 0) ⇒ Object



74
75
76
# File 'app/models/pbw/token.rb', line 74

def attach_update_process(process, updates_to_wait=0)
    AttachedProcess.create(token: self, process: process, updatable: true, updates_waiting: updates_to_wait)
end

#before_add_item(item, quantity) ⇒ Object



52
53
54
55
# File 'app/models/pbw/token.rb', line 52

def before_add_item(item, quantity)
    # stub method
    true
end

#before_move(area) ⇒ Object



43
44
45
46
# File 'app/models/pbw/token.rb', line 43

def before_move(area)
    # stub method
    true
end

#before_ownership(user) ⇒ Object



34
35
36
37
# File 'app/models/pbw/token.rb', line 34

def before_ownership(user)
    # stub method
    true
end

#before_remove_item(item, quantity) ⇒ Object



61
62
63
64
# File 'app/models/pbw/token.rb', line 61

def before_remove_item(item, quantity)
    # stub method
    true
end

#can_convert?(item) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/models/pbw/token.rb', line 78

def can_convert?(item)
    self.capabilities.any?{|c| c.can_convert?(item)}
end

#check_constraints_and_capabilities(&changeset) ⇒ Object



164
165
166
167
# File 'app/models/pbw/token.rb', line 164

def check_constraints_and_capabilities(&changeset)
    self.capabilities.any?{|c| !c.before_process(self, changeset)}
    self.constraints.any?{|c| !c.before_process(self, changeset)}
end

#check_triggers!Object



169
170
171
# File 'app/models/pbw/token.rb', line 169

def check_triggers!
    self.triggers.each{|t| t.check! }
end

#count_item(item) ⇒ Object



82
83
84
85
# File 'app/models/pbw/token.rb', line 82

def count_item(item)
    container = ItemContainer.find_for_token(self)
    container ? container.quantity : 0
end

#has_capability?(capability) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
# File 'app/models/pbw/token.rb', line 143

def has_capability?(capability)
    capability = Capability.find(capability) if capability.is_a?(String)
    self.capabilities.include?(capability)
end

#has_constraint?(constraint) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
# File 'app/models/pbw/token.rb', line 122

def has_constraint?(constraint)
    constraint = Constraint.find(constraint) if constraint.is_a?(String)
    self.constraints.include?(constraint)
end

#move_to_area!(area) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/pbw/token.rb', line 110

def move_to_area!(area)
    return false unless area && before_move(area) && (self.area.nil? || self.area.before_token_leaves(self)) && area.before_token_enters(self)
    old_area = self.area
    self.area = area
    if save
        after_move(area)
        old_area.after_token_leaves(self) if old_area
        area.after_token_enters(self)
    end
    self.area
end

#remove_capability!(capability) ⇒ Object



156
157
158
159
160
161
162
# File 'app/models/pbw/token.rb', line 156

def remove_capability!(capability)
    return false unless capability && capability.before_remove(self)
    self.capabilities.delete_if{|c| c.name == capability.name}
    save
    capability.after_remove(self)
    self
end

#remove_constraint!(constraint) ⇒ Object



135
136
137
138
139
140
141
# File 'app/models/pbw/token.rb', line 135

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



94
95
96
97
98
99
# File 'app/models/pbw/token.rb', line 94

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_token(self, item, (0 - quantity))
end

#set_ownership!(user) ⇒ Object



101
102
103
104
105
106
107
108
# File 'app/models/pbw/token.rb', line 101

def set_ownership!(user)
    return false unless user && before_ownership(user)
    self.user = user
    if save
        after_ownership(user)
    end
    self.user
end