class BooticCli::Themes::UpdatedTheme

given :source and :target themes, UpdatedTheme computes assets and templates with more recent versions in :source

Constants

TemplateWithDiff

Attributes

force_update[R]
source[R]
target[R]

Public Class Methods

new(source:, target:, force_update: false) click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 20
def initialize(source:, target:, force_update: false)
  @source, @target = source, target
  # when doing a pull or push, we don't care if the other end has a more recent version
  # we only do that when syncing changes, in which case force_update should be false
  @force_update = force_update
end

Public Instance Methods

any?() click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 27
def any?
  templates.any? # || assets.any?
end
assets() click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 44
def assets
  @assets ||= map_pair(source.assets, target.assets) do |a, b|
    [should_update?(a, b), a]
  end
end
templates() click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 31
def templates
  @templates ||= map_pair(source.templates, target.templates) do |a, b|
    diff = Diffy::Diff.new(normalize_endings(b.body), normalize_endings(a.body), context: 1)

    if !diff.to_s.empty? && should_update?(a, b)
      c = TemplateWithDiff.new(a.file_name, a.body, a.updated_on, diff)
      [true, c]
    else
      [false, nil]
    end
  end
end

Private Instance Methods

build_lookup(list) click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 61
def build_lookup(list)
  list.each_with_object({}) do |item, lookup|
    lookup[item.file_name] = item
  end
end
map_pair(list1, list2) { |item, match| ... } click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 67
def map_pair(list1, list2, &block)
  lookup = build_lookup(list2)
  list1.each_with_object([]) do |item, arr|
    match = lookup[item.file_name]
    if match
      valid, item = yield(item, match)
      arr << item if valid
    end
  end
end
more_recent?(a, b) click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 57
def more_recent?(a, b)
  a.updated_on > b.updated_on
end
normalize_endings(str) click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 78
def normalize_endings(str)
  str.to_s.gsub(/\r\n?/, "\n")
end
should_update?(a, b) click to toggle source
# File lib/bootic_cli/themes/updated_theme.rb, line 53
def should_update?(a, b)
  force_update || (a != b && more_recent?(a, b))
end