class Glass::TimelineItem

Attributes

client[W]

only a writer for these two because I want to hook an error message to each of these if they haven’t had there values set yet.

mirror_content[W]

only a writer for these two because I want to hook an error message to each of these if they haven’t had there values set yet.

template_name[W]

only a writer for these two because I want to hook an error message to each of these if they haven’t had there values set yet.

template_type[RW]
to_json[RW]

Public Class Methods

defaults_template(opts={}) click to toggle source

end

# File lib/glass/timeline_item.rb, line 59
def self.defaults_template(opts={})
  puts "DEPRECATION WARNING: defaults_template is now deprecated, please use defaults_template_with instead"
  self.default_template = opts[:with]
end
defaults_template_with(name_of_template) click to toggle source

end

# File lib/glass/timeline_item.rb, line 75
def self.defaults_template_with(name_of_template)
  if name_of_template.is_a? Symbol
    self.default_template = name_of_template.to_s
  else
    raise InvalidArgumentError, 'Template name is not a symbol'
  end
end
defines_callback_methods(action, opts) click to toggle source

this is really just a little meta-programming trick which basically forces a call to the method specified by with parameter in the has_menu_item method.

it allows you to put the callback logic right there in the model.

# File lib/glass/timeline_item.rb, line 139
def self.defines_callback_methods(action, opts)
  self.send(:define_method, "handles_#{action.to_s.underscore}") do |json|
    if self.respond_to?(opts[:handles_with])
      self.method(opts[:handles_with]).arity > 0 ? self.send(opts[:handles_with], json) : self.send(opts[:handles_with])
    else
      raise MenuItemHandlerIsNotDefinedError
    end
  end
end
has_menu_item(action_sym, opts={}) click to toggle source
def custom_handler_methodname
  # this gets executed when this
  # action occurs.
end

end

# File lib/glass/timeline_item.rb, line 119
def self.has_menu_item(action_sym, opts={})
  self.actions ||= []
  self.menu_items ||= []
  unless self.actions.include?(action_sym)
    self.actions += [action_sym]
    defines_callback_methods(action_sym, opts)
    menu_item = ::Glass::MenuItem.create(action_sym, opts)
    self.menu_items += [menu_item]
  end
end
manages_templates(opts={}) click to toggle source
manages_templates :template_manager_name

this will set your template manager for this class. this will override defaults_template path if it is defined.

end
# File lib/glass/timeline_item.rb, line 95
def self.manages_templates(opts={})
  self.template_manager = opts[:with] if opts[:with]
end
menu_items_hash() click to toggle source

convert the menu items into hash form not a part of the public api.

Public Instance Methods

client() click to toggle source
# File lib/glass/timeline_item.rb, line 43
def client
  raise UnserializedTemplateError unless @client
  @client
end
has_default_template?() click to toggle source

this is not intended to be a part of the public api

# File lib/glass/timeline_item.rb, line 275
def has_default_template?
  self.class.default_template
end
insert(opts={}) click to toggle source
# File lib/glass/timeline_item.rb, line 210
def insert(opts={})
  puts "DEPRECATION WARNING: insert is now deprecated, please use mirror_insert instead"
  mirror_insert(opts)
end
menu_items_hash() click to toggle source

convert class to instance method. not meant to be a part of the public api.

mirror_content() click to toggle source

a couple custom attr_readers which raise a helpful error message if the value is nil; i.e. error flow handling.

# File lib/glass/timeline_item.rb, line 39
def mirror_content
  raise UnserializedTemplateError unless @mirror_content
  @mirror_content
end
mirror_delete() click to toggle source
# File lib/glass/timeline_item.rb, line 238
def mirror_delete
  self.client = Glass::Client.create(self)
  client.delete id: self.glass_item_id
  self.update_attributes(is_deleted: true)
end
mirror_get(opts={}) click to toggle source
# File lib/glass/timeline_item.rb, line 221
def mirror_get(opts={})
  self.client = Glass::Client.create(self)
  result = client.get(self.glass_item_id)
  save_data(result)
end
mirror_insert(opts={}) click to toggle source
# File lib/glass/timeline_item.rb, line 216
def mirror_insert(opts={})
  result = client.insert(opts)
  raise TimelineInsertionError if result.error?
  save_data(result)
end
mirror_patch(opts={}) click to toggle source
# File lib/glass/timeline_item.rb, line 226
def mirror_patch(opts={})
  opts.merge! glass_item_id: self.glass_item_id
  self.client = Glass::Client.create(self)
  result = client.patch(opts)
  save_data(result)
end
mirror_update(timeline_item, opts={}) click to toggle source
# File lib/glass/timeline_item.rb, line 232
def mirror_update(timeline_item, opts={})
  opts.merge! glass_item_id: self.glass_item_id
  self.client = Glass::Client.create(self)
  result = client.update(timeline_item, opts)
  save_data(result)
end
save_data(result) click to toggle source
# File lib/glass/timeline_item.rb, line 244
def save_data(result)
  data = result.data
  result_data_type = :html #default
  [:html, :text].each do |result_type|
    result_data_type = result_type if data.send(result_type).present?
  end
  self.update_attributes(glass_item_id: data.id,
                          glass_etag: data.etag,
                          glass_self_link: data.self_link,
                          glass_kind: data.kind,
                          glass_created_at: data.created,
                          glass_updated_at: data.updated,
                          glass_content_type: result_data_type,
                          glass_content: data.send(result_data_type))

  to_indifferent_json_hash(result)
end
serialize(opts={}) click to toggle source
# File lib/glass/timeline_item.rb, line 194
def serialize(opts={})
  raise GoogleAccountNotSpecifiedError unless self.google_account.present?
  type = self.template_type || :html
  json_hash = {}
  json_hash[type] = self.setup_template(opts.delete(:template_variables).merge({template_name: opts.delete(:template_name) }))
  json_hash = json_hash.merge(self.menu_items_hash)
  json_hash.merge(opts)
  self.to_json = json_hash
  self.client = Glass::Client.create(self)
  return self
end
setup_template(variables={}) click to toggle source

this is not intended to be a part of the public api

# File lib/glass/timeline_item.rb, line 269
def setup_template(variables={})
  variables[:template_name] ||= self.template_name
  Glass::Template.new(variables).render_self
end
template_name() click to toggle source

this is not intended to be a part of the public api

# File lib/glass/timeline_item.rb, line 264
def template_name
  @template_name = self.class.default_template
end

Private Instance Methods

to_indifferent_json_hash(obj) click to toggle source
# File lib/glass/timeline_item.rb, line 279
def to_indifferent_json_hash(obj)
  JSON.parse(obj.body).with_indifferent_access
end