SHARE
TWEET

Untitled

a guest Apr 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module BitmaskAttrAccessor
  2.   def bitmask_attr_accessor(accessor_name, bitmask_name, options)
  3.  
  4.     define_method("#{accessor_name}=") do |values|
  5.       values = [*values].map { |value| value.to_sym }
  6.       bitmask_value = (values & options)
  7.         .map { |value| 2**options.index(value) }
  8.         .inject(0, :+)
  9.       send("#{bitmask_name}=", bitmask_value)
  10.     end
  11.  
  12.     define_method(accessor_name) do
  13.       bitmask_value = send(bitmask_name).to_i || 0
  14.       options.reject do |option|
  15.         (bitmask_value & 2**options.index(option)).zero?
  16.       end
  17.     end
  18.  
  19.   end
  20. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top