Class: Pop

Inherits:
RestfulModel show all
Defined in:
lib/pop.rb

Defined Under Namespace

Classes: AssetMustBeSaved, TemplateRequired

Instance Attribute Summary collapse

Attributes inherited from RestfulModel

#_id, #created_at

Instance Method Summary collapse

Methods inherited from RestfulModel

#==, collection_name, #path, #save!, #update

Constructor Details

#initialize(parent) ⇒ Pop

Returns a new instance of Pop.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pop.rb', line 32

def initialize(parent)
  if parent.is_a?(Template)
    @_api = parent.instance_variable_get :@_api
    self.template_id = parent._id
    self.title = parent.title
    self.name = parent.name
    self.unpopulated_api_regions = parent.api_regions.dup
    self.unpopulated_api_tags = parent.api_tags.dup
    self.custom_code = parent.custom_code
    self.custom_links = parent.custom_links.dup if parent.custom_links

  elsif parent.is_a?(RestfulModelCollection)
    @_api = parent.instance_variable_get :@_api

  elsif parent.is_a?(Populr)
    @_api = parent
  else
    raise "You must create a pop with a template, collection, or API model."
  end

  # We could choose to make parent the restfulModelCollection that is passed to us,
  # but that would result in PUTs and POSTs to /templates/:id/pops, which isn't
  # how the API is currently set up. For now, all pop PUTS, POSTs, etc... go to /pops/
  @_parent = @_api.pops

  @newly_populated_regions = {}
  @newly_populated_tags = {}
end

Instance Attribute Details

Returns the value of attribute clone_link_enabled.



20
21
22
# File 'lib/pop.rb', line 20

def clone_link_enabled
  @clone_link_enabled
end

Returns the value of attribute clone_link_url.



21
22
23
# File 'lib/pop.rb', line 21

def clone_link_url
  @clone_link_url
end

#collaboration_interstitial_textObject

Returns the value of attribute collaboration_interstitial_text.



23
24
25
# File 'lib/pop.rb', line 23

def collaboration_interstitial_text
  @collaboration_interstitial_text
end

Returns the value of attribute collaboration_link_enabled.



22
23
24
# File 'lib/pop.rb', line 22

def collaboration_link_enabled
  @collaboration_link_enabled
end

Returns the value of attribute collaboration_link_url.



24
25
26
# File 'lib/pop.rb', line 24

def collaboration_link_url
  @collaboration_link_url
end

#collaboration_webhookObject

Returns the value of attribute collaboration_webhook.



25
26
27
# File 'lib/pop.rb', line 25

def collaboration_webhook
  @collaboration_webhook
end

#custom_codeObject

Returns the value of attribute custom_code.



19
20
21
# File 'lib/pop.rb', line 19

def custom_code
  @custom_code
end

Returns the value of attribute custom_links.



18
19
20
# File 'lib/pop.rb', line 18

def custom_links
  @custom_links
end

#domainObject

Returns the value of attribute domain.



26
27
28
# File 'lib/pop.rb', line 26

def domain
  @domain
end

#label_namesObject

Returns the value of attribute label_names.



13
14
15
# File 'lib/pop.rb', line 13

def label_names
  @label_names
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/pop.rb', line 10

def name
  @name
end

#newly_populated_regionsObject (readonly)

Returns the value of attribute newly_populated_regions.



29
30
31
# File 'lib/pop.rb', line 29

def newly_populated_regions
  @newly_populated_regions
end

#newly_populated_tagsObject (readonly)

Returns the value of attribute newly_populated_tags.



30
31
32
# File 'lib/pop.rb', line 30

def newly_populated_tags
  @newly_populated_tags
end

#passwordObject

Returns the value of attribute password.



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

def password
  @password
end

#published_pop_urlObject

Returns the value of attribute published_pop_url.



15
16
17
# File 'lib/pop.rb', line 15

def published_pop_url
  @published_pop_url
end

#slugObject

Returns the value of attribute slug.



12
13
14
# File 'lib/pop.rb', line 12

def slug
  @slug
end

#template_idObject

Returns the value of attribute template_id.



9
10
11
# File 'lib/pop.rb', line 9

def template_id
  @template_id
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/pop.rb', line 11

def title
  @title
end

#tracersObject

Returns the value of attribute tracers.



14
15
16
# File 'lib/pop.rb', line 14

def tracers
  @tracers
end

#unpopulated_api_regionsObject

Returns the value of attribute unpopulated_api_regions.



16
17
18
# File 'lib/pop.rb', line 16

def unpopulated_api_regions
  @unpopulated_api_regions
end

#unpopulated_api_tagsObject

Returns the value of attribute unpopulated_api_tags.



17
18
19
# File 'lib/pop.rb', line 17

def unpopulated_api_tags
  @unpopulated_api_tags
end

Instance Method Details

#as_json(options = {}) ⇒ Object

Raises:



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pop.rb', line 68

def as_json(options = {})
  raise TemplateRequired.new if options[:api_representation] && !template_id

  if options[:api_representation]
    hash = {}
    hash[:pop] = super(options)
    hash[:populate_tags] = @newly_populated_tags
    hash[:populate_regions] = @newly_populated_regions
    hash
  else
    super(options)
  end
end

#disable_cloningObject



108
109
110
111
# File 'lib/pop.rb', line 108

def disable_cloning
  self.clone_link_enabled = false
  self.clone_link_url = nil # doesn't get saed, just for developer interface
end

#disable_collaborationObject



98
99
100
101
# File 'lib/pop.rb', line 98

def disable_collaboration
  self.collaboration_link_enabled = false
  self.collaboration_link_url = nil
end

#enable_cloning!Object



103
104
105
106
# File 'lib/pop.rb', line 103

def enable_cloning!
  self.clone_link_enabled = true
  self.save! # go and populate our model with the clone link
end

#enable_collaboration!(interstitial_text = '', webhook = nil) ⇒ Object



91
92
93
94
95
96
# File 'lib/pop.rb', line 91

def enable_collaboration!(interstitial_text = '', webhook = nil)
  self.collaboration_link_enabled = true
  self.collaboration_webhook = webhook
  self.collaboration_interstitial_text = interstitial_text
  self.save! # go and populate our model with the collaboration link
end

#has_unpopulated_region(region_identifier) ⇒ Object



113
114
115
# File 'lib/pop.rb', line 113

def has_unpopulated_region(region_identifier)
  self.unpopulated_api_regions.include?(region_identifier)
end

#has_unpopulated_tag(tag_identifier) ⇒ Object



133
134
135
# File 'lib/pop.rb', line 133

def has_unpopulated_tag(tag_identifier)
  self.unpopulated_api_tags.include?(tag_identifier)
end

#inflate(json) ⇒ Object



61
62
63
64
65
66
# File 'lib/pop.rb', line 61

def inflate(json)
  super(json)
  self.tracers = RestfulModelCollection.new(Tracer, @_api, self)
  @newly_populated_regions = {}
  @newly_populated_tags = {}
end

#populate_region(region_identifier, assets) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/pop.rb', line 122

def populate_region(region_identifier, assets)
  assets = [assets] unless assets.is_a?(Array)

  @newly_populated_regions[region_identifier] ||= []
  @newly_populated_regions[region_identifier].concat(assets.map {|a|
    raise AssetMustBeSaved.new unless a._id
    a._id
  })
  self.unpopulated_api_regions.delete(region_identifier)
end

#populate_tag(tag_identifier, tag_contents) ⇒ Object



137
138
139
140
# File 'lib/pop.rb', line 137

def populate_tag(tag_identifier, tag_contents)
  @newly_populated_tags[tag_identifier] = tag_contents
  self.unpopulated_api_tags.delete(tag_identifier)
end

#publish!Object



83
84
85
# File 'lib/pop.rb', line 83

def publish!
  update('POST', 'publish')
end

#type_of_unpopulated_region(region_identifier) ⇒ Object



117
118
119
120
# File 'lib/pop.rb', line 117

def type_of_unpopulated_region(region_identifier)
  return false unless self.has_unpopulated_region(region_identifier)
  self.unpopulated_api_regions[region_identifier]['type']
end

#unpublish!Object



87
88
89
# File 'lib/pop.rb', line 87

def unpublish!
  update('POST', 'unpublish')
end