Pro Yearly is on sale from $80 to $50! »

Boring Ruby Code

9800e8331d2341549301c957a878f1c8?s=47 Adam Niedzielski
July 07, 2017
510

Boring Ruby Code

Brighton Ruby 2017

9800e8331d2341549301c957a878f1c8?s=128

Adam Niedzielski

July 07, 2017
Tweet

Transcript

  1. @niedzielskiadam blog.sundaycoding.com Boring Ruby Code

  2. @niedzielskiadam blog.sundaycoding.com easy to understand

  3. @niedzielskiadam blog.sundaycoding.com easy to read

  4. @niedzielskiadam blog.sundaycoding.com easy to delete

  5. @niedzielskiadam blog.sundaycoding.com ❓

  6. @niedzielskiadam blog.sundaycoding.com

  7. @niedzielskiadam blog.sundaycoding.com conferences

  8. @niedzielskiadam blog.sundaycoding.com structure of this talk

  9. @niedzielskiadam blog.sundaycoding.com real code ™

  10. @niedzielskiadam blog.sundaycoding.com def initialize(options) %w(id name path size data mode

    commit_id binary).each do |key| self.send("#{key}=", options[key.to_sym]) end end
  11. @niedzielskiadam blog.sundaycoding.com def initialize(options) %w(id name path size data mode

    commit_id binary).each do |key| self.send("#{key}=", options[key.to_sym]) end end
  12. @niedzielskiadam blog.sundaycoding.com %i(call to_document to_html).each do |meth| define_method(meth) do |text,

    context| context = transform_context(context) html_pipeline.send(meth, text, context) end end
  13. @niedzielskiadam blog.sundaycoding.com %i(call to_document to_html).each do |meth| define_method(meth) do |text,

    context| context = transform_context(context) html_pipeline.send(meth, text, context) end end
  14. @niedzielskiadam blog.sundaycoding.com def call(text, context) context = transform_context(context) html_pipeline.call(text, context)

    end def to_document(text, context) context = transform_context(context) html_pipeline.to_document(text, context) end def to_html(text, context) context = transform_context(context) html_pipeline.to_html(text, context) end
  15. @niedzielskiadam blog.sundaycoding.com %i(call to_document to_html).each do |meth| define_method(meth) do |text,

    context| context = transform_context(context) html_pipeline.send(meth, text, context) end end
  16. @niedzielskiadam blog.sundaycoding.com https://commons.wikimedia.org/wiki/File:Dry_fruit.jpg

  17. @niedzielskiadam blog.sundaycoding.com %i(original_position position).each do |meth| define_method "#{meth}=" do |new_position|

    if new_position.is_a?(String) new_position = JSON.parse(new_position) rescue nil end if new_position.is_a?(Hash) new_position = new_position.with_indifferent_access new_position = Gitlab::Diff::Position.new(new_position) end super(new_position) end end
  18. @niedzielskiadam blog.sundaycoding.com %i(original_position position).each do |meth| define_method "#{meth}=" do |new_position|

    if new_position.is_a?(String) new_position = JSON.parse(new_position) rescue nil end if new_position.is_a?(Hash) new_position = new_position.with_indifferent_access new_position = Gitlab::Diff::Position.new(new_position) end super(new_position) end end
  19. @niedzielskiadam blog.sundaycoding.com %i(original_position position).each do |meth| define_method "#{meth}=" do |new_position|

    new_position = some_transformation(new_position) super(new_position) end end
  20. @niedzielskiadam blog.sundaycoding.com def original_position=(new_position) super(some_transformation(new_position)) end def position=(new_position) super(some_transformation(new_position)) end

  21. @niedzielskiadam blog.sundaycoding.com story time

  22. @niedzielskiadam blog.sundaycoding.com

  23. @niedzielskiadam blog.sundaycoding.com before_action :authorize_download_code!

  24. @niedzielskiadam blog.sundaycoding.com

  25. @niedzielskiadam blog.sundaycoding.com

  26. @niedzielskiadam blog.sundaycoding.com Emacs vs Vim

  27. @niedzielskiadam blog.sundaycoding.com

  28. @niedzielskiadam blog.sundaycoding.com

  29. @niedzielskiadam blog.sundaycoding.com def method_missing(method_sym, *arguments, &block) if method_sym.to_s =~ /\Aauthorize_(.*)!\z/

    authorize_action!($1.to_sym) else super end end
  30. @niedzielskiadam blog.sundaycoding.com def method_missing(method_sym, *arguments, &block) if method_sym.to_s =~ /\Aauthorize_(.*)!\z/

    authorize_action!($1.to_sym) else super end end
  31. @niedzielskiadam blog.sundaycoding.com https://commons.wikimedia.org/wiki/File:Gizl%C9%99nqa%C3%A7.jpg

  32. @niedzielskiadam blog.sundaycoding.com def method_missing(m, *args, &block) if m == :lookup

    && !block_given? lookup_cache[m] ||= {} lookup_cache[m][args.join(":")] ||= raw_repository.send(m, *args, &block) else raw_repository.send(m, *args, &block) end end
  33. @niedzielskiadam blog.sundaycoding.com def method_missing(m, *args, &block) if m == :lookup

    && !block_given? lookup_cache[m] ||= {} lookup_cache[m][args.join(":")] ||= raw_repository.send(m, *args, &block) else raw_repository.send(m, *args, &block) end end
  34. @niedzielskiadam blog.sundaycoding.com def method_missing(m, *args, &block) if m == :lookup

    && !block_given? lookup_cache[m] ||= {} lookup_cache[m][args.join(":")] ||= raw_repository.send(m, *args, &block) else raw_repository.send(m, *args, &block) end end
  35. @niedzielskiadam blog.sundaycoding.com def method_missing(method, *args) return super unless self.respond_to?(method) self.send(method,

    *args) do |item| block_given? ? yield(item) : item end end
  36. @niedzielskiadam blog.sundaycoding.com def method_missing(method, *args) return super unless self.respond_to?(method) self.send(method,

    *args) do |item| block_given? ? yield(item) : item end end
  37. @niedzielskiadam blog.sundaycoding.com def method_missing(method, *args) return super unless self.respond_to?(method) self.send(method,

    *args) do |item| block_given? ? yield(item) : item end end
  38. @niedzielskiadam blog.sundaycoding.com def method_missing(method, *args) return super unless self.respond_to?(method) self.send(method,

    *args) do |item| block_given? ? yield(item) : item end end
  39. @niedzielskiadam blog.sundaycoding.com module Banzai module Filter def self.[](name) const_get("#{name.to_s.camelize}Filter") end

    end end
  40. @niedzielskiadam blog.sundaycoding.com module Banzai module Filter def self.[](name) const_get("#{name.to_s.camelize}Filter") end

    end end
  41. @niedzielskiadam blog.sundaycoding.com https://www.amazon.com/Wenger-16999-Swiss-Knife-Giant/dp/B001DZTJRQ

  42. @niedzielskiadam blog.sundaycoding.com def project_icon(project_id, options = {}) project = if

    project_id.is_a?(Project) project_id else Project.find_by_full_path(project_id) end end
  43. @niedzielskiadam blog.sundaycoding.com def project_icon(project_id, options = {}) project = if

    project_id.is_a?(Project) project_id else Project.find_by_full_path(project_id) end end
  44. @niedzielskiadam blog.sundaycoding.com https://commons.wikimedia.org/wiki/File:Graptemys.oculifera.jpg

  45. @niedzielskiadam blog.sundaycoding.com def initialize(repository, name, target) encode! name @name =

    name.gsub(/\Arefs\/(tags|heads)\//, '') @dereferenced_target = Gitlab::Git::Commit.find(repository, target) @target = if target.respond_to?(:oid) target.oid elsif target.respond_to?(:name) target.name elsif target.is_a? String target else nil end end
  46. @niedzielskiadam blog.sundaycoding.com def initialize(repository, name, target) encode! name @name =

    name.gsub(/\Arefs\/(tags|heads)\//, '') @dereferenced_target = Gitlab::Git::Commit.find(repository, target) @target = if target.respond_to?(:oid) target.oid elsif target.respond_to?(:name) target.name elsif target.is_a? String target else nil end end
  47. @niedzielskiadam blog.sundaycoding.com def initialize(repository, name, target) encode! name @name =

    name.gsub(/\Arefs\/(tags|heads)\//, '') @dereferenced_target = Gitlab::Git::Commit.find(repository, target) @target = if target.respond_to?(:oid) target.oid elsif target.respond_to?(:name) target.name elsif target.is_a? String target else nil end end
  48. @niedzielskiadam blog.sundaycoding.com def find(repo, commit_id = "HEAD") return commit_id if

    commit_id.is_a?(Gitlab::Git::Commit) return decorate(commit_id) if commit_id.is_a?(Rugged::Commit) obj = if commit_id.is_a?(String) repo.rev_parse_target(commit_id) else Gitlab::Git::Ref.dereference_object(commit_id) end return nil unless obj.is_a?(Rugged::Commit) decorate(obj) end
  49. @niedzielskiadam blog.sundaycoding.com def find(repo, commit_id = "HEAD") return commit_id if

    commit_id.is_a?(Gitlab::Git::Commit) return decorate(commit_id) if commit_id.is_a?(Rugged::Commit) obj = if commit_id.is_a?(String) repo.rev_parse_target(commit_id) else Gitlab::Git::Ref.dereference_object(commit_id) end return nil unless obj.is_a?(Rugged::Commit) decorate(obj) end
  50. @niedzielskiadam blog.sundaycoding.com def diff @diff ||= Gitlab::Git::Diff.new( st_diff ) if

    st_diff.respond_to?(:map) end
  51. @niedzielskiadam blog.sundaycoding.com def diff @diff ||= Gitlab::Git::Diff.new( st_diff ) if

    st_diff.respond_to?(:map) end
  52. @niedzielskiadam blog.sundaycoding.com

  53. @niedzielskiadam blog.sundaycoding.com if @note.is_a?(Note) Banzai::NoteRenderer.render([@note], @project, current_user) end

  54. @niedzielskiadam blog.sundaycoding.com if @note.is_a?(Note) Banzai::NoteRenderer.render([@note], @project, current_user) end

  55. @niedzielskiadam blog.sundaycoding.com I lied

  56. @niedzielskiadam blog.sundaycoding.com easy to understand

  57. @niedzielskiadam blog.sundaycoding.com easy to read

  58. @niedzielskiadam blog.sundaycoding.com easy to delete

  59. @niedzielskiadam blog.sundaycoding.com cheers!

  60. @niedzielskiadam blog.sundaycoding.com