Class: RivetCms::Entry

Inherits:
Object
  • Object
show all
Defined in:
app/models/rivet_cms/entry.rb

Overview

Read-only view of one serialized document, as returned by the Ruby content helpers. Field values are reachable as methods (entry.title), via [] for keys that collide with real methods (entry), or through #data.

Instance Method Summary collapse

Constructor Details

#initialize(serialized) ⇒ Entry

Returns a new instance of Entry.



6
7
8
# File 'app/models/rivet_cms/entry.rb', line 6

def initialize(serialized)
  @hash = serialized
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



46
47
48
49
50
51
# File 'app/models/rivet_cms/entry.rb', line 46

def method_missing(name, *args, &block)
  key = name.to_s
  return wrap(data[key]) if args.empty? && block.nil? && data.key?(key)

  super
end

Instance Method Details

#[](key) ⇒ Object



38
39
40
# File 'app/models/rivet_cms/entry.rb', line 38

def [](key)
  wrap(data[key.to_s])
end

#content_typeObject



18
19
20
# File 'app/models/rivet_cms/entry.rb', line 18

def content_type
  @hash[:content_type]
end

#dataObject



26
27
28
# File 'app/models/rivet_cms/entry.rb', line 26

def data
  @hash[:data] || {}
end

#idObject



10
11
12
# File 'app/models/rivet_cms/entry.rb', line 10

def id
  @hash[:id]
end

#inspectObject



57
58
59
# File 'app/models/rivet_cms/entry.rb', line 57

def inspect
  "#<RivetCms::Entry #{content_type}/#{slug} (#{state})>"
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/models/rivet_cms/entry.rb', line 42

def key?(key)
  data.key?(key.to_s)
end

#published?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'app/models/rivet_cms/entry.rb', line 34

def published?
  state.to_s == "published"
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/rivet_cms/entry.rb', line 53

def respond_to_missing?(name, include_private = false)
  data.key?(name.to_s) || super
end

#slugObject



14
15
16
# File 'app/models/rivet_cms/entry.rb', line 14

def slug
  @hash[:slug]
end

#stateObject



22
23
24
# File 'app/models/rivet_cms/entry.rb', line 22

def state
  @hash[:state]
end

#to_hObject



30
31
32
# File 'app/models/rivet_cms/entry.rb', line 30

def to_h
  @hash
end