Class: Hash

Inherits:
Object show all
Defined in:
lib/active_form/core_extensions.rb

Instance Method Summary collapse

Instance Method Details

#as_attributes_stringObject

define these methods for any value holder you want to bind



73
74
75
76
77
78
79
80
81
# File 'lib/active_form/core_extensions.rb', line 73

def as_attributes_string
  attributes = self.delete_blanks.stringify_keys
  parts = attributes.keys.sort.inject([]) do |attrs, k|
    v = attributes[k].to_s.to_xs.gsub(%r{"}, '"')
    attrs << "#{k}=\"#{v}\""
    attrs
  end
  parts.join(' ')
end

#bound_value(*args) ⇒ Object



83
84
85
86
# File 'lib/active_form/core_extensions.rb', line 83

def bound_value(*args)
  self[args[0]] = args[1] if args.length == 2
  self[args[0]]
end

#bound_value?(key) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/active_form/core_extensions.rb', line 88

def bound_value?(key)
  self.key?(key)
end

#delete_blanksObject

utility methods



94
95
96
97
98
99
100
101
102
103
# File 'lib/active_form/core_extensions.rb', line 94

def delete_blanks
  self.inject({}) do |hash,(key, value)|
    if value == :blank
      hash[key] = ''
    else
      hash[key] = value unless (value.blank? || value == false)
    end
    hash
  end
end

#delete_blanks!Object



105
106
107
# File 'lib/active_form/core_extensions.rb', line 105

def delete_blanks!
  replace delete_blanks
end