Boring Ruby Code

9800e8331d2341549301c957a878f1c8?s=47 Adam Niedzielski
October 06, 2017
50

Boring Ruby Code

Southeast Ruby

9800e8331d2341549301c957a878f1c8?s=128

Adam Niedzielski

October 06, 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 20 minutes talk in 30 minutes

  7. @niedzielskiadam blog.sundaycoding.com speaking after Ben

  8. @niedzielskiadam blog.sundaycoding.com https://en.wikipedia.org/wiki/Parthenon_(Nashville)#/media/File:Parthenon,_Nashville.JPG

  9. @niedzielskiadam blog.sundaycoding.com first time in US, yay!

  10. @niedzielskiadam blog.sundaycoding.com

  11. @niedzielskiadam blog.sundaycoding.com

  12. @niedzielskiadam blog.sundaycoding.com

  13. @niedzielskiadam blog.sundaycoding.com

  14. @niedzielskiadam blog.sundaycoding.com

  15. @niedzielskiadam blog.sundaycoding.com https://commons.wikimedia.org/wiki/File:Fuck_Yoga-Prenzlauer_Berg.jpg

  16. @niedzielskiadam blog.sundaycoding.com

  17. @niedzielskiadam blog.sundaycoding.com “good code”

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

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

  20. @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
  21. @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
  22. @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
  23. @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
  24. @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
  25. @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
  26. @niedzielskiadam blog.sundaycoding.com https://commons.wikimedia.org/wiki/File:Dry_fruit.jpg

  27. @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
  28. @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
  29. @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
  30. @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
  31. @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

  32. @niedzielskiadam blog.sundaycoding.com story time

  33. @niedzielskiadam blog.sundaycoding.com

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

  35. @niedzielskiadam blog.sundaycoding.com

  36. @niedzielskiadam blog.sundaycoding.com

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

  38. @niedzielskiadam blog.sundaycoding.com

  39. @niedzielskiadam blog.sundaycoding.com

  40. @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
  41. @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
  42. @niedzielskiadam blog.sundaycoding.com https://commons.wikimedia.org/wiki/File:Gizl%C9%99nqa%C3%A7.jpg

  43. @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
  44. @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
  45. @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
  46. @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
  47. @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
  48. @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
  49. @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
  50. @niedzielskiadam blog.sundaycoding.com module Banzai module Filter def self.[](name) const_get("#{name.to_s.camelize}Filter") end

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

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

  53. @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
  54. @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
  55. @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
  56. @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
  57. @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
  58. @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
  59. @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
  60. @niedzielskiadam blog.sundaycoding.com def diff @diff ||= Gitlab::Git::Diff.new( st_diff ) if

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

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

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

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

  65. @niedzielskiadam blog.sundaycoding.com recap

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

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

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

  69. @niedzielskiadam blog.sundaycoding.com thanks!