devs for leokz e 7masters - wtf oriented programming

58
WTF ORIENTED PROGRAMMING by @AkitaOnRails

Upload: fabio-akita

Post on 15-Jul-2015

842 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Devs for Leokz e 7Masters - WTF Oriented Programming

WTF ORIENTED PROGRAMMINGby @AkitaOnRails

Page 2: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 3: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 4: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 5: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 6: Devs for Leokz e 7Masters - WTF Oriented Programming

$('a').click(function(){  

       window.location  =  $(a).attr('href');  

})

Page 7: Devs for Leokz e 7Masters - WTF Oriented Programming

project_id  =  @user.project  ==  nil  ?  nil  :  @user.project.id

Page 8: Devs for Leokz e 7Masters - WTF Oriented Programming

project_id  =  @user.project  ==  nil  ?  nil  :  @user.project.id

project_id  =  @user.try(:project).try(:id)

Page 9: Devs for Leokz e 7Masters - WTF Oriented Programming

@politician  =  Politician.where(name:  "Terminator").first  puts  @politician.try(:studio).try(:name)

Page 10: Devs for Leokz e 7Masters - WTF Oriented Programming

@politician  =  Politician.where(name:  "Terminator").first  puts  @politician.try(:studio).try(:name)

LAW OF DEMETER

Page 11: Devs for Leokz e 7Masters - WTF Oriented Programming

@politician  =  Politician.where(name:  "Terminator").first  puts  @politician.try(:studio).try(:name)

LAW OF DEMETER

Page 12: Devs for Leokz e 7Masters - WTF Oriented Programming

class  Politician  

   delegate  :name,  to:  :studio,  

       prefix:  true,  allow_nil:  true  

   #  ...  

end  

@politician.studio.name

Page 13: Devs for Leokz e 7Masters - WTF Oriented Programming

class  Politician  

   delegate  :name,  to:  :studio,  

       prefix:  true,  allow_nil:  true  

   #  ...  

end  

@[email protected]_name

Page 14: Devs for Leokz e 7Masters - WTF Oriented Programming

class  Politician  

   delegate  :name,  to:  :studio,  

       prefix:  true,  allow_nil:  true  

   #  ...  

end  

@[email protected]_name

Page 15: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 16: Devs for Leokz e 7Masters - WTF Oriented Programming

array  =  []  list.each  do  |state|      array  <<  [state.name,  state.acronym]  end  array

Page 17: Devs for Leokz e 7Masters - WTF Oriented Programming

array  =  []  list.each  do  |state|      array  <<  [state.name,  state.acronym]  end  array

for  state  in  list

Page 18: Devs for Leokz e 7Masters - WTF Oriented Programming

array  =  []  list.each  do  |state|      array  <<  [state.name,  state.acronym]  end  array

list.map  {  |state|  [state.name,  state.acronym]  }

for  state  in  list

Page 19: Devs for Leokz e 7Masters - WTF Oriented Programming

array  =  []  list.each  do  |state|      if  state.name  =~  /^S/          array  <<  [state.name,  state.acronym]      end  end  array

Page 20: Devs for Leokz e 7Masters - WTF Oriented Programming

array  =  []  list.each  do  |state|      if  state.name  =~  /^S/          array  <<  [state.name,  state.acronym]      end  end  array

list.      select  {  |state|  state.name  =~  /^S/  }.      map  {  |state|  [state.name,  state.acronym]  }

Page 21: Devs for Leokz e 7Masters - WTF Oriented Programming

def  formatdate(d)          months  =  Hash.new          months["Jan"]  =  "janeiro"          months["Feb"]  =  "fevereiro"          #  ...          months["Dec"]  =  "dezembro"              weeks  =  Hash.new          weeks["Sun"]  =  "Domingo"          #  ...          weeks["Fri"]  =  "Sexta"          weeks["Sat"]  =  "Sábado"                    return  weeks[d.strftime("%a")]+",  "+d.strftime("%d")+"  de  "+months[d.strftime("%b")]  end

Page 22: Devs for Leokz e 7Masters - WTF Oriented Programming

def  formatdate(d)          months  =  Hash.new          months["Jan"]  =  "janeiro"          months["Feb"]  =  "fevereiro"          #  ...          months["Dec"]  =  "dezembro"              weeks  =  Hash.new          weeks["Sun"]  =  "Domingo"          #  ...          weeks["Fri"]  =  "Sexta"          weeks["Sat"]  =  "Sábado"                    return  weeks[d.strftime("%a")]+",  "+d.strftime("%d")+"  de  "+months[d.strftime("%b")]  end

#  config/locales/pt-­‐BR.yml  pt-­‐BR:      time:          formats:              short_wday:  "%a,  %d  de  %B”  

d.to_s(:short_wday)

Page 23: Devs for Leokz e 7Masters - WTF Oriented Programming

.row      .wrap-­‐select          select.payment-­‐select              -­‐  if  @payment_type  ==  'pagamento'                  option.icons-­‐select.card-­‐credit[value="pagamento"  data-­‐foo="card-­‐credit"  selected]  Pagamento              -­‐  else                  option.icons-­‐select.card-­‐credit  value="pagamento"  data-­‐foo="card-­‐credit"  Pagamento              -­‐  if  @payment_type.include?  'cartao'                  option.icons-­‐select.card-­‐credit[value="cartao"  data-­‐foo="card-­‐credit"  selected]  Cartão              -­‐  else                  option.icons-­‐select.card-­‐credit  value="cartao"  data-­‐foo="card-­‐credit"  Cartão              -­‐  if  @payment_type  ==  'dinheiro'                  option.icons-­‐select.cash[value="dinheiro"  data-­‐foo="cash"  selected]  Dinheiro              -­‐  else                  option.icons-­‐select.cash  value="dinheiro"  data-­‐foo="cash"  Dinheiro              -­‐  if  @payment_type  ==  'boleto'                  option.icons-­‐select.banking-­‐billet[value="boleto"  data-­‐foo="banking-­‐billet"  selected]  Boleto              -­‐  else                  option.icons-­‐select.banking-­‐billet  value="boleto"  data-­‐foo="banking-­‐billet"  Boleto

Page 24: Devs for Leokz e 7Masters - WTF Oriented Programming

.row      .wrap-­‐select          select.payment-­‐select              -­‐  if  @payment_type  ==  'pagamento'                  option.icons-­‐select.card-­‐credit[value="pagamento"  data-­‐foo="card-­‐credit"  selected]  Pagamento              -­‐  else                  option.icons-­‐select.card-­‐credit  value="pagamento"  data-­‐foo="card-­‐credit"  Pagamento              -­‐  if  @payment_type.include?  'cartao'                  option.icons-­‐select.card-­‐credit[value="cartao"  data-­‐foo="card-­‐credit"  selected]  Cartão              -­‐  else                  option.icons-­‐select.card-­‐credit  value="cartao"  data-­‐foo="card-­‐credit"  Cartão              -­‐  if  @payment_type  ==  'dinheiro'                  option.icons-­‐select.cash[value="dinheiro"  data-­‐foo="cash"  selected]  Dinheiro              -­‐  else                  option.icons-­‐select.cash  value="dinheiro"  data-­‐foo="cash"  Dinheiro              -­‐  if  @payment_type  ==  'boleto'                  option.icons-­‐select.banking-­‐billet[value="boleto"  data-­‐foo="banking-­‐billet"  selected]  Boleto              -­‐  else                  option.icons-­‐select.banking-­‐billet  value="boleto"  data-­‐foo="banking-­‐billet"  Boleto

.row      .wrap-­‐select          select.payment-­‐select              -­‐  pc  =  %w(card-­‐credit  card-­‐credit  cash  banking-­‐billet)              -­‐  %w(pagamento  cartao  dinheiro  boleto).each_with_index  do  |i,  ptype|                  option.icons-­‐select[class=pc[i]  value=ptype  data-­‐foo=pc[i]  selected=(@payment_type==ptype)]  I18n.t(ptype)

Page 25: Devs for Leokz e 7Masters - WTF Oriented Programming

<?php  if  (LANGUAGE  ==  'en')  {  ?>  <body  class="en">  <?php  }  ?>  

<?php  if  (LANGUAGE  ==  'pt')  {  ?>  <body  class="pt">  <?php  }  ?>  

<?php  if  (LANGUAGE  ==  'mx')  {  ?>  <body  class="mx">  <?php  }  ?>  

<?php  if  (LANGUAGE  ==  'kp')  {  ?>  <body  class="kp">  <?php  }  ?>

Page 26: Devs for Leokz e 7Masters - WTF Oriented Programming

<?php  if  (LANGUAGE  ==  'en')  {  ?>  <body  class="en">  <?php  }  ?>  

<?php  if  (LANGUAGE  ==  'pt')  {  ?>  <body  class="pt">  <?php  }  ?>  

<?php  if  (LANGUAGE  ==  'mx')  {  ?>  <body  class="mx">  <?php  }  ?>  

<?php  if  (LANGUAGE  ==  'kp')  {  ?>  <body  class="kp">  <?php  }  ?>

<body  class="<?php  LANGUAGE  ?>">

Page 27: Devs for Leokz e 7Masters - WTF Oriented Programming

address  =  ((!client.street.to_s.nil?)?  client.street.to_s  :  "")  +    ((!client.number.to_s.nil?)?  "  Nº  "  +  client.number.to_s  :  "")  +  "  "  +    ((!client.city.to_s.nil?)?  client.city.to_s  :  "")  +    ((!client.zip.to_s.nil?)?  "  -­‐  CEP:  "  +  client.zip.to_s  :  "")

Page 28: Devs for Leokz e 7Masters - WTF Oriented Programming

address  =  ((!client.street.to_s.nil?)?  client.street.to_s  :  "")  +    ((!client.number.to_s.nil?)?  "  Nº  "  +  client.number.to_s  :  "")  +  "  "  +    ((!client.city.to_s.nil?)?  client.city.to_s  :  "")  +    ((!client.zip.to_s.nil?)?  "  -­‐  CEP:  "  +  client.zip.to_s  :  "")

address  =  "#{client.street}  Nº  #{client.number}  #{client.city}  -­‐  CEP:  #{client.zip}"

Page 29: Devs for Leokz e 7Masters - WTF Oriented Programming

address  =  ((!client.street.to_s.nil?)?  client.street.to_s  :  "")  +    ((!client.number.to_s.nil?)?  "  Nº  "  +  client.number.to_s  :  "")  +  "  "  +    ((!client.city.to_s.nil?)?  client.city.to_s  :  "")  +    ((!client.zip.to_s.nil?)?  "  -­‐  CEP:  "  +  client.zip.to_s  :  "")

address  =  "#{client.street}  Nº  #{client.number}  #{client.city}  -­‐  CEP:  #{client.zip}"

address  =  "%s  Nº  %s  %s  -­‐  CEP:  %s"  %  [client.street,  client.number,  client.city,  client.zip]

Page 30: Devs for Leokz e 7Masters - WTF Oriented Programming

address  =  ((!client.street.to_s.nil?)?  client.street.to_s  :  "")  +    ((!client.number.to_s.nil?)?  "  Nº  "  +  client.number.to_s  :  "")  +  "  "  +    ((!client.city.to_s.nil?)?  client.city.to_s  :  "")  +    ((!client.zip.to_s.nil?)?  "  -­‐  CEP:  "  +  client.zip.to_s  :  "")

address  =  "#{client.street}  Nº  #{client.number}  #{client.city}  -­‐  CEP:  #{client.zip}"

address  =  "%s  Nº  %s  %s  -­‐  CEP:  %s"  %  [client.street,  client.number,  client.city,  client.zip]

class  ClientDecorator      #  ...      def  formatted_address          "%s  Nº  %s  %s  -­‐  CEP:  %s"  %  [street,  number,  city,  zip]      end  end

Page 31: Devs for Leokz e 7Masters - WTF Oriented Programming

–Jamie Zawinksi

“Some people, when confronted with a problem, think, “I know, I’ll use regular expressions.” Now

they have two problems.”

http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/

Page 32: Devs for Leokz e 7Masters - WTF Oriented Programming

class  User  <  ActiveRecord::Base      validates  :email,  format:  {  with:  /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/  }  end

Page 33: Devs for Leokz e 7Masters - WTF Oriented Programming

class  User  <  ActiveRecord::Base      validates  :email,  format:  {  with:  /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/  }  end

class  EmailValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  email_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  email")          end      end  

 def  email_valid?(email)          Mail::Address.new(email)          true      rescue  Mail::Field::ParseError  =>  e          false      end    end

Page 34: Devs for Leokz e 7Masters - WTF Oriented Programming

class  User  <  ActiveRecord::Base      validates  :email,  format:  {  with:  /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/  }  end

class  EmailValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  email_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  email")          end      end  

 def  email_valid?(email)          Mail::Address.new(email)          true      rescue  Mail::Field::ParseError  =>  e          false      end    end

Page 35: Devs for Leokz e 7Masters - WTF Oriented Programming

class  User  <  ActiveRecord::Base      validates  :email,  format:  {  with:  /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/  }  end

class  EmailValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  email_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  email")          end      end  

 def  email_valid?(email)          Mail::Address.new(email)          true      rescue  Mail::Field::ParseError  =>  e          false      end    end

class  User  <  ActiveRecord::Base      validates  :email,  email:  true  end

Page 36: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 37: Devs for Leokz e 7Masters - WTF Oriented Programming

class  User  <  ActiveRecord::Base      validates  :link,  format:  {  with:          /(^$)|(^(http|https):\/\/[a-­‐z0-­‐9]+([\-­‐\.]{1}[a-­‐z0-­‐9]+)*\.[a-­‐z]{2,5}(([0-­‐9]{1,5})?\/.*)?$)/ix      }  end

https://coderwall.com/p/ztig5g/validate-urls-in-rails

Page 38: Devs for Leokz e 7Masters - WTF Oriented Programming

class  User  <  ActiveRecord::Base      validates  :link,  format:  {  with:          /(^$)|(^(http|https):\/\/[a-­‐z0-­‐9]+([\-­‐\.]{1}[a-­‐z0-­‐9]+)*\.[a-­‐z]{2,5}(([0-­‐9]{1,5})?\/.*)?$)/ix      }  end

https://coderwall.com/p/ztig5g/validate-urls-in-rails

class  UrlValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  url_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  URL")          end      end  

   #  a  URL  may  be  technically  well-­‐formed  but  may        #  not  actually  be  valid,  so  this  checks  for  both.      def  url_valid?(url)          url  =  URI.parse(url)  rescue  false          url.kind_of?(URI::HTTP)  ||  url.kind_of?(URI::HTTPS)      end    end

Page 39: Devs for Leokz e 7Masters - WTF Oriented Programming

class  User  <  ActiveRecord::Base      validates  :link,  format:  {  with:          /(^$)|(^(http|https):\/\/[a-­‐z0-­‐9]+([\-­‐\.]{1}[a-­‐z0-­‐9]+)*\.[a-­‐z]{2,5}(([0-­‐9]{1,5})?\/.*)?$)/ix      }  end

https://coderwall.com/p/ztig5g/validate-urls-in-rails

class  UrlValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  url_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  URL")          end      end  

   #  a  URL  may  be  technically  well-­‐formed  but  may        #  not  actually  be  valid,  so  this  checks  for  both.      def  url_valid?(url)          url  =  URI.parse(url)  rescue  false          url.kind_of?(URI::HTTP)  ||  url.kind_of?(URI::HTTPS)      end    end

Page 40: Devs for Leokz e 7Masters - WTF Oriented Programming

class  User  <  ActiveRecord::Base      validates  :link,  url:  true  end

class  User  <  ActiveRecord::Base      validates  :link,  format:  {  with:          /(^$)|(^(http|https):\/\/[a-­‐z0-­‐9]+([\-­‐\.]{1}[a-­‐z0-­‐9]+)*\.[a-­‐z]{2,5}(([0-­‐9]{1,5})?\/.*)?$)/ix      }  end

https://coderwall.com/p/ztig5g/validate-urls-in-rails

class  UrlValidator  <  ActiveModel::EachValidator      def  validate_each(record,  attribute,  value)          unless  url_valid?(value)              record.errors[attribute]  <<  (options[:message]  ||  "must  be  a  valid  URL")          end      end  

   #  a  URL  may  be  technically  well-­‐formed  but  may        #  not  actually  be  valid,  so  this  checks  for  both.      def  url_valid?(url)          url  =  URI.parse(url)  rescue  false          url.kind_of?(URI::HTTP)  ||  url.kind_of?(URI::HTTPS)      end    end

Page 41: Devs for Leokz e 7Masters - WTF Oriented Programming

def  query      Vote.connection.select_values  <<-­‐SQL          SELECT  voteable_id          FROM  votes          LEFT  OUTER  JOIN  authorships  ON  authorships.bill_id  =  voteable_id          LEFT  OUTER  JOIN  politicians  ON  politicians.id  =  politician_id          WHERE  voteable_type  =  'Bill'              AND  person_type  =  'User'              AND  votes.created_at  >=  now()  -­‐  interval  '90  days'              #{@condition.present??  "AND  #{@condition}"  :  ""}          GROUP  BY  voteable_id          ORDER  BY  count(*)  DESC  LIMIT  6;      SQL  end

http://guides.rubyonrails.org/active_record_querying.html

Page 42: Devs for Leokz e 7Masters - WTF Oriented Programming

def  query      Vote.connection.select_values  <<-­‐SQL          SELECT  voteable_id          FROM  votes          LEFT  OUTER  JOIN  authorships  ON  authorships.bill_id  =  voteable_id          LEFT  OUTER  JOIN  politicians  ON  politicians.id  =  politician_id          WHERE  voteable_type  =  'Bill'              AND  person_type  =  'User'              AND  votes.created_at  >=  now()  -­‐  interval  '90  days'              #{@condition.present??  "AND  #{@condition}"  :  ""}          GROUP  BY  voteable_id          ORDER  BY  count(*)  DESC  LIMIT  6;      SQL  end

def  query      query  =  Vote.select('voteable_id')          joins("LEFT  OUTER  JOIN  authorships  ON  authorships.bill_id  =  voteable_id").          joins("LEFT  OUTER  JOIN  politicians  ON  politicians.id  =  politician_id").          where(voteable_type:  'Bill',  person_type:  'User').          where("votes.created_at  >=  now()  -­‐  interval  '90  days'").          group('voteable_id').          order('count(*)  desc').          limit(6)      query  =  query.where(@condition)  if  @condition.present?      query  end

http://guides.rubyonrails.org/active_record_querying.html

Page 43: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 44: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 45: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 46: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 47: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 48: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 49: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 50: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 51: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 52: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 53: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 54: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 55: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 56: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 57: Devs for Leokz e 7Masters - WTF Oriented Programming
Page 58: Devs for Leokz e 7Masters - WTF Oriented Programming

OBRIGADO!

@akitaonrails