module ActivemodelFlags::HasFlags::LocalInstanceMethods
Public Instance Methods
has(flag)
click to toggle source
setters
# File lib/activemodel_flags/has_flags.rb, line 89 def has(flag) ensure_serialized old_flags = self.flags self.flags[flag.to_s] = true on_flag_change(old_flags[flag.to_s], self.flags[flag.to_s]) unless old_flags == self.flags end
has!(flag)
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 99 def has!(flag) has(flag) self.save! end
has?(flag)
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 77 def has?(flag) self.flags[flag.to_s] end
has_flag?(flag)
click to toggle source
getters
# File lib/activemodel_flags/has_flags.rb, line 73 def has_flag?(flag) return false if self.flags.blank? self.flags[flag.to_s] != nil end
hasnt(flag)
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 105 def hasnt(flag) ensure_serialized old_flags = self.flags self.flags[flag.to_s] = false on_flag_change(old_flags[flag.to_s], self.flags[flag.to_s]) unless old_flags == self.flags end
Also aliased as: has_not
hasnt!(flag)
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 116 def hasnt!(flag) hasnt(flag) self.save! end
Also aliased as: has_not!
hasnt?(flag)
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 81 def hasnt?(flag) !has?(flag) end
Also aliased as: has_not?
method_missing(method)
click to toggle source
Calls superclass method
# File lib/activemodel_flags/has_flags.rb, line 132 def method_missing(method) if method.to_s =~ /^has_[a-zA-Z0-9_]+\?$/ key = method.to_s.chomp("?").reverse.chomp("_sah").reverse has?(key.to_sym) elsif method.to_s =~ /^hasnt_[a-zA-Z0-9_]+\?$/ key = method.to_s.chomp("?").reverse.chomp("_tnsah").reverse hasnt?(key.to_sym) elsif method.to_s =~ /^has_[a-zA-Z0-9_]+!$/ key = method.to_s.chomp("!").reverse.chomp("_sah").reverse has!(key.to_sym) elsif method.to_s =~ /^hasnt_[a-zA-Z0-9_]+!$/ key = method.to_s.chomp("!").reverse.chomp("_tnsah").reverse hasnt!(key.to_sym) else super end end
respond_to?(method, include_private = false)
click to toggle source
Calls superclass method
# File lib/activemodel_flags/has_flags.rb, line 150 def respond_to?(method, include_private = false) has_flag?(method.to_s) || super(method, include_private) end
unset_flag!(flag)
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 122 def unset_flag!(flag) old_flags = self.flags self.flags.delete(flag.to_s) on_flag_change(old_flags[flag.to_s], self.flags[flag.to_s]) unless old_flags == self.flags self.save! end
Protected Instance Methods
on_flag_change(old_val, new_val)
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 156 def on_flag_change(old_val, new_val) # Override this method if you want to react to flag changes end
Private Instance Methods
ensure_serialized()
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 166 def ensure_serialized if self.flags.is_a?(String) self.flags = JSON.load(self.flags) end end
init_flags()
click to toggle source
# File lib/activemodel_flags/has_flags.rb, line 162 def init_flags self.flags = {} unless self.flags.present? end