Class: Staticd::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/staticd/models/base.rb

Overview

Base class for Staticd Datamapper models

Direct Known Subclasses

DomainName, Release, Resource, Route, Site

Instance Method Summary collapse

Instance Method Details

#errorObject

Get the first error message in the datamapper error list



38
39
40
# File 'lib/staticd/models/base.rb', line 38

def error
  errors.full_messages.first
end

#to_h(context = nil) ⇒ Object

Return a hash of public attributes of the model

Take a context as argument (default to :normal). Available contexts are:

  • normal: extract every public attributes of the model into a hash.

  • full: extract every public attributes of the model and every public attributes of its associed models into a hash.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/staticd/models/base.rb', line 16

def to_h(context=nil)
  case context
  when :full
    data = attributes
    links = model.relationships.map{ |association| association.name }
    links.each do |link|
      linked_data = send(link)
      linked_data_value =
        if linked_data.respond_to?(:map)
          linked_data.map{ |element| element.to_h }
        else
          linked_data.to_h
        end
      data.merge!({link => linked_data_value})
    end
    data
  else
    attributes
  end
end