Module: HungryForm::Elements::Base::Hashable

Included in:
Element
Defined in:
lib/hungryform/elements/base/hashable.rb

Overview

This module adds hashing capabilities to form elements. Do not include this module in your classes. It is already included in the base_element class.

Sample usage:

class MyField

attr_accessor :param1, :param2
hashable :param1, :param2
...

end

Any instance of MyField class will have the “to_hash” method that will contain only the accessor/reader params defined in the hashable macro.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



20
21
22
23
24
# File 'lib/hungryform/elements/base/hashable.rb', line 20

def self.included(base)
  base.extend ClassMethods
  base.class_attribute :hashable_attributes
  base.hashable_attributes = []
end

Instance Method Details

#to_hashObject



26
27
28
29
30
31
# File 'lib/hungryform/elements/base/hashable.rb', line 26

def to_hash
  hash = self.class.hashable_attributes.each_with_object({}) do |param, h|
    h[param] = send(param) unless send(param).nil?
  end
  hash.merge(_type: self.class.name.demodulize)
end