Class: Boxcab::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/boxcab/page.rb

Constant Summary collapse

ATTRIBUTE_NAMES =
%w(id title url path position schema content)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Page

Returns a new instance of Page.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/boxcab/page.rb', line 7

def initialize(attributes = {})
  @attributes = {}.with_indifferent_access
  @read_only  = false

  unless attributes.blank?
    @previous_attributes = attributes.with_indifferent_access
    @attributes = @previous_attributes.with_indifferent_access
  end

  @attributes['content'] ||= {}

  # new schema in any case
  @attributes['schema'] = []
end

Class Method Details

.build_default(url, path) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/boxcab/page.rb', line 31

def self.build_default(url, path)
  new.tap do |page|
    page.title  = path.split('/').last.humanize
    page.url    = url
    page.path   = path
  end
end

Instance Method Details

#attributes=(new_attributes) ⇒ Object



27
28
29
# File 'lib/boxcab/page.rb', line 27

def attributes=(new_attributes)
  @attributes.merge!(new_attributes)
end

#changed?Boolean

only called before an existing page is going to be updated

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/boxcab/page.rb', line 45

def changed?
  return true if @attributes.slice(:title, :position) != @previous_attributes.slice(:title, :position)

  @attributes[:schema] != @previous_attributes[:schema]
end

#content=(content) ⇒ Object



39
40
41
42
# File 'lib/boxcab/page.rb', line 39

def content=(content)
  @read_only = true
  @attributes['content'] = content
end

#persisted?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/boxcab/page.rb', line 55

def persisted?
  !self.id.nil?
end

#read_only?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/boxcab/page.rb', line 51

def read_only?
  @read_only
end

#to_apiObject



59
60
61
62
63
64
65
# File 'lib/boxcab/page.rb', line 59

def to_api
  if persisted?
    @attributes.slice(:title, :position, :schema)
  else
    @attributes.slice(:title, :url, :path, :position, :schema)
  end
end