Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement