Module: HotLikeSauce::ClassMethods
- Defined in:
- lib/hot_like_sauce.rb
Constant Summary collapse
- @@attr_obscurable_mutex =
Mutex.new
Instance Method Summary collapse
- #attr_obscurable(*args) ⇒ Object
- #obscure_read_on_fields!(*fields_array) ⇒ Object
- #obscured_fields ⇒ Object
- #obscured_fields=(val) ⇒ Object
- #unobscure_read_on_fields!(*fields_array) ⇒ Object
- #unobscured_read_fields ⇒ Object
- #unobscured_read_fields=(val) ⇒ Object
Instance Method Details
#attr_obscurable(*args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/hot_like_sauce.rb', line 51 def attr_obscurable(*args) fields = args.map {|arg| arg if arg.is_a?(Symbol)}.compact = args.map {|arg| arg if arg.is_a?(Hash)}.compact.first ||= {} fields.each do |field| type = self.columns_hash[field.to_s].type raise "#{field} must be a string or text field" unless type == :string || type == :text @@attr_obscurable_mutex.synchronize do self.obscured_fields = (self.obscured_fields << field).uniq self.unobscured_read_fields = (self.unobscured_read_fields << field).uniq unless [:obscure_on_read] == true end define_method field do super() value = read_attribute(field) return value if value.nil? self.class.unobscured_read_fields.include?(field) ? _unobscure(value) : value end define_method "#{field}=" do |value| super(value) write_attribute(field, value.nil? ? nil : _obscure(value)) end if [:unobscured_accessor] == true define_method "unobscured_#{field}" do _unobscure(read_attribute(field)) end end end end |
#obscure_read_on_fields!(*fields_array) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/hot_like_sauce.rb', line 87 def obscure_read_on_fields!(*fields_array) if fields_array.empty? self.unobscured_read_fields = [] else self.unobscured_read_fields -= fields_array end end |
#obscured_fields ⇒ Object
35 36 37 |
# File 'lib/hot_like_sauce.rb', line 35 def obscured_fields @obscured_fields ||= [] end |
#obscured_fields=(val) ⇒ Object
39 40 41 |
# File 'lib/hot_like_sauce.rb', line 39 def obscured_fields=(val) @obscured_fields = val end |
#unobscure_read_on_fields!(*fields_array) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/hot_like_sauce.rb', line 95 def unobscure_read_on_fields!(*fields_array) if fields_array.empty? self.unobscured_read_fields = self.obscured_fields else self.unobscured_read_fields += self.obscured_fields & fields_array end end |
#unobscured_read_fields ⇒ Object
43 44 45 |
# File 'lib/hot_like_sauce.rb', line 43 def unobscured_read_fields @unobscured_read_fields ||= [] end |
#unobscured_read_fields=(val) ⇒ Object
47 48 49 |
# File 'lib/hot_like_sauce.rb', line 47 def unobscured_read_fields=(val) @unobscured_read_fields = val end |