def instrument(name, payload={}) started = Time.now begin yield ensure @notifier.publish(name, started, Time.now, @id, payload) end end Number of Doubts
tion_controller (0.021 ms) MessagesController index GET /messages process_action.action_controller (95.463 ms) controller MessagesController action index method GET path /messages sql.active_record (5.671 ms) sql SELECT "messages"."id" AS t0_r0, "messages"."address_id" AS t0_r1, "messages"."from" AS t0_r2, "messages"."to" AS t0_r3, "messages"."disposable" AS t0_r4, "messages"."subject" AS t0_r5, "messages"."body" AS t0_r6, "messages"."plain" AS t0_r7, "messages"."html" AS t0_r8, "messages"."created_at" AS t0_r9, "messages"."updated_at" AS t0_r10, "messages"."parsed_message_id" AS t0_r11, "addresses"."id" AS t1_r0, "addresses"."name" AS t1_r1, "addresses"."person_id" AS t1_r2, "addresses"."created_at" AS t1_r3, "addresses"."updated_at" AS t1_r4 FROM "messages" LEFT render_template.action_view (63.632 ms) identifier /app/views/messages/index.html layout layouts/application
class UserController < ApplicationController def create user = User.create!(params[:user]) UserMailer.deliver_welcome_email(user) end end Number of Doubts
Definition Time def method_missing(method, *args, &block) unless self.class.attribute_methods_generated? self.class.define_attribute_methods ... end end def respond_to?(name, include_private = false) unless self.class.attribute_methods_generated? self.class.define_attribute_methods end super end
use super class Subject < ActiveRecord::Base def initialize(*args) super set_email_address # after_init end protected def set_email_address ... end end Number of Doubts
define_attribute_methods def define_attribute_methods(attr_names) attr_names.each { |attr_name| define_attribute_method(attr_name) } end THANKS ACTIVE MODEL
Post#name def name attr_name = 'name' unless @attributes.has_key?(attr_name) missing_attribute(attr_name, caller) end v = @attributes[attr_name] && v end
Post#name def name attr_name = 'name' unless @attributes.has_key?(attr_name) missing_attribute(attr_name, caller) end v = @attributes[attr_name] && v end SEEMS LEGIT
type_cast_code def type_cast_code(var_name) klass = self.class.name case type when :string, :text then var_name when :integer then "(#{var_name}.to_i rescue #{var_name} ? 1 : 0)" when :float then "#{var_name}.to_f" when :decimal then "#{klass}.value_to_decimal(#{var_name})" when :datetime, :timestamp then "#{klass}.string_to_time(#{var_name})" when :time then "#{klass}.string_to_dummy_time(#{var_name})" when :date then "#{klass}.string_to_date(#{var_name})" when :binary then "#{klass}.binary_to_string(#{var_name})" when :boolean then "#{klass}.value_to_boolean(#{var_name})" else var_name end end
type_cast def type_cast(value) return nil if value.nil? return coder.load(value) if encoded? klass = self.class case type when :string, :text then value when :integer then value.to_i rescue value ? 1 : 0 when :float then value.to_f when :decimal then klass.value_to_decimal(value) when :datetime, :timestamp then klass.string_to_time(value) when :time then klass.string_to_dummy_time(value) when :date then klass.string_to_date(value) when :binary then klass.binary_to_string(value) when :boolean then klass.value_to_boolean(value) else value end end
def read_attribute(attr_name) # If it's cached, just return it @attributes_cache.fetch(attr_name.to_s) { |name| column = @columns_hash[name] value = @attributes.fetch(name) { return attribute_missing(name, caller) } # Cache if we're supposed to if self.class.cache_attribute?(name) @attributes_cache[name] = column.type_cast(value) else column.type_cast value end } end