Module: FlagShihTzu

Defined in:
lib/flag_shih_tzu.rb,
lib/flag_shih_tzu/version.rb

Defined Under Namespace

Modules: ClassMethods Classes: IncorrectFlagColumnException, NoSuchFlagException, NoSuchFlagQueryModeException

Constant Summary

TRUE_VALUES =

taken from ActiveRecord::ConnectionAdapters::Column

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



11
12
13
14
15
# File 'lib/flag_shih_tzu.rb', line 11

def self.included(base)
  base.extend(ClassMethods)
  base.class_attribute :flag_options
  base.class_attribute :flag_mapping
end

Instance Method Details

- (Object) disable_flag(flag, colmn = nil)

Performs the bitwise operation so the flag will return false.



194
195
196
197
198
199
# File 'lib/flag_shih_tzu.rb', line 194

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

- (Object) enable_flag(flag, colmn = nil)

Performs the bitwise operation so the flag will return true.



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

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

- (Boolean) flag_disabled?(flag, colmn = nil)

Returns:

  • (Boolean)


208
209
210
211
212
213
# File 'lib/flag_shih_tzu.rb', line 208

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

- (Boolean) flag_enabled?(flag, colmn = nil)

Returns:

  • (Boolean)


201
202
203
204
205
206
# File 'lib/flag_shih_tzu.rb', line 201

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

- (Object) flags(colmn = DEFAULT_COLUMN_NAME)



215
216
217
# File 'lib/flag_shih_tzu.rb', line 215

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

- (Object) set_flags(value, colmn)



219
220
221
# File 'lib/flag_shih_tzu.rb', line 219

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