Class: Glass::TimelineItem
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Glass::TimelineItem
- Defined in:
- lib/glass/timeline_item.rb
Defined Under Namespace
Classes: GoogleAccountNotSpecifiedError, MenuItemHandlerIsNotDefinedError, TimelineInsertionError, UnserializedTemplateError
Instance Attribute Summary collapse
- #client ⇒ Object
-
#mirror_content ⇒ Object
a couple custom attr_readers which raise a helpful error message if the value is nil; i.e.
-
#template_name ⇒ Object
this is not intended to be a part of the public api.
-
#template_type ⇒ Object
Returns the value of attribute template_type.
-
#to_json ⇒ Object
Returns the value of attribute to_json.
Class Method Summary collapse
-
.defaults_template(opts = {}) ⇒ Object
end.
-
.defaults_template_with(name_of_template) ⇒ Object
end.
-
.defines_callback_methods(action, opts) ⇒ Object
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.
-
.has_menu_item(action_sym, opts = {}) ⇒ Object
def custom_handler_methodname # this gets executed when this # action occurs.
-
.manages_templates(opts = {}) ⇒ Object
end.
-
.menu_items_hash ⇒ Object
convert the menu items into hash form not a part of the public api.
Instance Method Summary collapse
-
#actions ⇒ Object
i’d use cattr_accessor, but unfortunately ruby has some very crappy class variables, which are nonsensically over-written across sub-classes.
-
#has_default_template? ⇒ Boolean
this is not intended to be a part of the public api.
- #insert(opts = {}) ⇒ Object
-
#menu_items_hash ⇒ Object
convert class to instance method.
- #mirror_delete ⇒ Object
- #mirror_get(opts = {}) ⇒ Object
- #mirror_insert(opts = {}) ⇒ Object
- #mirror_patch(opts = {}) ⇒ Object
- #mirror_update(timeline_item, opts = {}) ⇒ Object
- #save_data(result) ⇒ Object
- #serialize(opts = {}) ⇒ Object
-
#setup_template(variables = {}) ⇒ Object
this is not intended to be a part of the public api.
Instance Attribute Details
#client ⇒ Object
43 44 45 46 |
# File 'lib/glass/timeline_item.rb', line 43 def client raise UnserializedTemplateError unless @client @client end |
#mirror_content ⇒ Object
a couple custom attr_readers which raise a helpful error message if the value is nil; i.e. error flow handling.
39 40 41 42 |
# File 'lib/glass/timeline_item.rb', line 39 def mirror_content raise UnserializedTemplateError unless @mirror_content @mirror_content end |
#template_name ⇒ Object
this is not intended to be a part of the public api
264 265 266 |
# File 'lib/glass/timeline_item.rb', line 264 def template_name @template_name = self.class.default_template end |
#template_type ⇒ Object
Returns the value of attribute template_type.
28 29 30 |
# File 'lib/glass/timeline_item.rb', line 28 def template_type @template_type end |
#to_json ⇒ Object
Returns the value of attribute to_json.
28 29 30 |
# File 'lib/glass/timeline_item.rb', line 28 def to_json @to_json end |
Class Method Details
.defaults_template(opts = {}) ⇒ Object
end
59 60 61 62 |
# 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) ⇒ Object
end
75 76 77 78 79 80 81 |
# 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) ⇒ Object
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.
139 140 141 142 143 144 145 146 147 |
# 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 = {}) ⇒ Object
def custom_handler_methodname
# this gets executed when this
# action occurs.
end
end
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/glass/timeline_item.rb', line 119 def self.(action_sym, opts={}) self.actions ||= [] self. ||= [] unless self.actions.include?(action_sym) self.actions += [action_sym] defines_callback_methods(action_sym, opts) = ::Glass::MenuItem.create(action_sym, opts) self. += [] end end |
.manages_templates(opts = {}) ⇒ Object
end
95 96 97 |
# 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 ⇒ Object
convert the menu items into hash form not a part of the public api.
153 154 155 |
# File 'lib/glass/timeline_item.rb', line 153 def self. {menuItems: self..map(&:serialize) } end |
Instance Method Details
#actions ⇒ Object
i’d use cattr_accessor, but unfortunately ruby has some very crappy class variables, which are nonsensically over-written across sub-classes. so use the rails class_attribute helpers instead.
24 |
# File 'lib/glass/timeline_item.rb', line 24 class_attribute :actions |
#has_default_template? ⇒ Boolean
this is not intended to be a part of the public api
275 276 277 |
# File 'lib/glass/timeline_item.rb', line 275 def has_default_template? self.class.default_template end |
#insert(opts = {}) ⇒ Object
210 211 212 213 |
# 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 ⇒ Object
convert class to instance method. not meant to be a part of the public api.
159 160 161 |
# File 'lib/glass/timeline_item.rb', line 159 def self.class. end |
#mirror_delete ⇒ Object
238 239 240 241 242 |
# 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 = {}) ⇒ Object
221 222 223 224 225 |
# 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 = {}) ⇒ Object
216 217 218 219 220 |
# 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 = {}) ⇒ Object
226 227 228 229 230 231 |
# 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 = {}) ⇒ Object
232 233 234 235 236 237 |
# 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) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# 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 = {}) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 |
# 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.) json_hash.merge(opts) self.to_json = json_hash self.client = Glass::Client.create(self) return self end |
#setup_template(variables = {}) ⇒ Object
this is not intended to be a part of the public api
269 270 271 272 |
# 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 |