Module: Handlebarer::Serialize

Defined in:
lib/handlebarer/serialize.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

nodoc



34
35
36
# File 'lib/handlebarer/serialize.rb', line 34

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#hbs_attributesArray

List of Model attributes that should be serialized when called ‘to_hbs` on Model instance

Returns:

  • (Array)

    list of serializable attributes



56
57
58
59
60
61
62
63
64
# File 'lib/handlebarer/serialize.rb', line 56

def hbs_attributes
  s = self.class.class_variable_get(:@@serialize)
  if s[:merge]
    attrs = s[:attrs] + self.attributes.keys
  else
    attrs = s[:attrs]
  end
  attrs.collect{|attr| attr.to_sym}.uniq
end

#to_hbsHash

Serialize instance attributes to a Hash based on serializable attributes defined on Model class.

Returns:

  • (Hash)

    hash of model instance attributes



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/handlebarer/serialize.rb', line 40

def to_hbs
  h = {:model => self.class.name.downcase}
  self.hbs_attributes.each do |attr|
    h[attr] = self.send(attr)

    ans = h[attr].class.ancestors
    if h[attr].class.respond_to?(:hbs_serializable) || ans.include?(Enumerable) || ans.include?(ActiveModel::Validations)
      h[attr] = h[attr].to_hbs
    else
    end
  end
  h
end