Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module BitmaskAttrAccessor
- def bitmask_attr_accessor(accessor_name, bitmask_name, options)
- define_method("#{accessor_name}=") do |values|
- values = [*values].map { |value| value.to_sym }
- bitmask_value = (values & options)
- .map { |value| 2**options.index(value) }
- .inject(0, :+)
- send("#{bitmask_name}=", bitmask_value)
- end
- define_method(accessor_name) do
- bitmask_value = send(bitmask_name).to_i || 0
- options.reject do |option|
- (bitmask_value & 2**options.index(option)).zero?
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement