Class: PermissionsHash
Instance Method Summary collapse
- #[](k, tru = nil) ⇒ Object
- #deep_merge_permissions(other_hash) ⇒ Object
- #deep_merge_permissions!(other_hash) ⇒ Object
- #deep_remove_permissions(other_hash) ⇒ Object
- #deep_remove_permissions!(other_hash) ⇒ Object
- #to_hash ⇒ Object
- #to_yaml(*args) ⇒ Object
Methods inherited from Hash
Instance Method Details
#[](k, tru = nil) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/permissions_hash.rb', line 15 def [] k, tru = nil v = super(k.to_sym) || super(k.to_s) return self.class[{}] if v.nil? && !tru v.kind_of?(Hash) ? self.class[v] : v end |
#deep_merge_permissions(other_hash) ⇒ Object
33 34 35 |
# File 'lib/permissions_hash.rb', line 33 def other_hash self.dup. other_hash end |
#deep_merge_permissions!(other_hash) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/permissions_hash.rb', line 37 def other_hash other_hash.each_pair do |k,v| tv = self[k, true] self[k] = if tv.is_a?(Hash) && v.is_a?(Hash) tv.(v) elsif v.is_a?(Array) || tv.is_a?(Array) ([tv] + [v]).flatten.uniq.compact else v end end self end |
#deep_remove_permissions(other_hash) ⇒ Object
51 52 53 |
# File 'lib/permissions_hash.rb', line 51 def other_hash dup. other_hash end |
#deep_remove_permissions!(other_hash) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/permissions_hash.rb', line 55 def other_hash other_hash.each_pair do |k,v| tv = self[k, true] self[k] = if tv.is_a?(Hash) && v.is_a?(Hash) tv.(v) elsif v.is_a?(Array) || tv.is_a?(Array) self_arr = ([tv]).flatten.uniq.compact oth_arr = ([v]).flatten.uniq.compact diff = self_arr - oth_arr if diff.size < self_arr.size diff else (self_arr + oth_arr).flatten.uniq.compact end else v end end self end |
#to_hash ⇒ Object
26 27 28 29 30 31 |
# File 'lib/permissions_hash.rb', line 26 def to_hash inject({}) do |h, (k,v)| v = v.is_a?(Hash) ? v.to_hash : v h.merge!({k => v}) end end |
#to_yaml(*args) ⇒ Object
22 23 24 |
# File 'lib/permissions_hash.rb', line 22 def to_yaml *args to_hash.to_yaml *args end |