Module: FlagShihTzu

Defined in:
lib/flag_shih_tzu.rb

Defined Under Namespace

Modules: ClassMethods Classes: IncorrectFlagColumnException, NoSuchFlagException, NoSuchFlagQueryModeException

Constant Summary collapse

TRUE_VALUES =

taken from ActiveRecord::ConnectionAdapters::Column

[true, 1, '1', 't', 'T', 'true', 'TRUE']
DEFAULT_COLUMN_NAME =
'flags'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/flag_shih_tzu.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#disable_flag(flag, colmn = nil) ⇒ Object

Performs the bitwise operation so the flag will return false.



166
167
168
169
170
171
# File 'lib/flag_shih_tzu.rb', line 166

def disable_flag(flag, colmn = nil)
  colmn = determine_flag_colmn_for(flag) if colmn.nil?
  self.class.check_flag(flag, colmn)

  set_flags(self.flags(colmn) & ~self.class.flag_mapping[colmn][flag], colmn)
end

#enable_flag(flag, colmn = nil) ⇒ Object

Performs the bitwise operation so the flag will return true.



158
159
160
161
162
163
# File 'lib/flag_shih_tzu.rb', line 158

def enable_flag(flag, colmn = nil)
  colmn = determine_flag_colmn_for(flag) if colmn.nil?
  self.class.check_flag(flag, colmn)

  set_flags(self.flags(colmn) | self.class.flag_mapping[colmn][flag], colmn)
end

#flag_disabled?(flag, colmn = nil) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
# File 'lib/flag_shih_tzu.rb', line 180

def flag_disabled?(flag, colmn = nil)
  colmn = determine_flag_colmn_for(flag) if colmn.nil?
  self.class.check_flag(flag, colmn)

  !flag_enabled?(flag, colmn)
end

#flag_enabled?(flag, colmn = nil) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
# File 'lib/flag_shih_tzu.rb', line 173

def flag_enabled?(flag, colmn = nil)
  colmn = determine_flag_colmn_for(flag) if colmn.nil?
  self.class.check_flag(flag, colmn)

  get_bit_for(flag, colmn) == 0 ? false : true
end

#flags(colmn = DEFAULT_COLUMN_NAME) ⇒ Object



187
188
189
# File 'lib/flag_shih_tzu.rb', line 187

def flags(colmn = DEFAULT_COLUMN_NAME)
  self[colmn] || 0
end

#set_flags(value, colmn) ⇒ Object



191
192
193
# File 'lib/flag_shih_tzu.rb', line 191

def set_flags(value, colmn)
  self[colmn] = value
end