Module: Jader::Serialize

Defined in:
lib/jader/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/jader/serialize.rb', line 34

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

Instance Method Details

#jade_attributesArray

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

Returns:

  • (Array)

    list of serializable attributes



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

def jade_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_jadeHash

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/jader/serialize.rb', line 40

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

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