Module: Extras::Array

Defined in:
lib/extras/array.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#stringify(allowed_keys: nil, allowed_vals: nil) ⇒ Object


Stringify an array's keys, skipping anything within the allowed list.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/extras/array.rb', line 35

def stringify(allowed_keys: nil, allowed_vals: nil)
  keys = allowed_keys || self.class.allowed[:keys]

  map do |v|
    v = v.to_s unless keys.include?(v.class)
    !v.respond_to?(:stringify) ? v : v.stringify({
      :allowed_keys => allowed_keys,
      :allowed_vals => allowed_vals
    })
  end
end

#symbolize(allowed_keys: nil, allowed_vals: nil) ⇒ Object


Symbolize an array's keys, skpping anything within the allowed list.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/extras/array.rb', line 51

def symbolize(allowed_keys: nil, allowed_vals: nil)
  keys = allowed_keys || self.class.allowed[:keys]

  map do |v|
    v = v.to_sym unless !v.respond_to?(:to_sym) || keys.include?(v.class)
    !v.respond_to?(:symbolize) ? v : v.symbolize({
      :allowed_keys => allowed_keys,
      :allowed_vals => allowed_vals
    })
  end
end