Class: Locomotive::Wagon::PullBaseCommand

Inherits:
Struct
  • Object
show all
Includes:
AssetsConcern
Defined in:
lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb

Constant Summary

Constants included from AssetsConcern

AssetsConcern::REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AssetsConcern

#replace_asset_urls, #replace_asset_urls_in_hash

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client

Returns:

  • (Object)

    the current value of api_client



5
6
7
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 5

def api_client
  @api_client
end

#current_siteObject

Returns the value of attribute current_site

Returns:

  • (Object)

    the current value of current_site



5
6
7
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 5

def current_site
  @current_site
end

#envObject

Returns the value of attribute env

Returns:

  • (Object)

    the current value of env



5
6
7
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 5

def env
  @env
end

#pathObject

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



5
6
7
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 5

def path
  @path
end

Class Method Details

.pull(api_client, current_site, path, env) ⇒ Object



9
10
11
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 9

def self.pull(api_client, current_site, path, env)
  new(api_client, current_site, path, env).pull
end

Instance Method Details

#_pull_with_timezoneObject



21
22
23
24
25
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 21

def _pull_with_timezone
  Time.use_zone(current_site.try(:timezone)) do
    _pull
  end
end

#clean_attributes(attributes) ⇒ Object



44
45
46
47
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 44

def clean_attributes(attributes)
  # remove nil or empty values
  attributes.delete_if { |_, v| v.nil? || v == '' || (v.is_a?(Hash) && v.empty?) }
end

#default_localeObject



80
81
82
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 80

def default_locale
  current_site.locales.first
end

#dump(element, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 32

def dump(element, options = {})
  if element.is_a?(Hash)
    StyledYAML.dump(element.dup.tap do |attributes|
      [*options[:inline]].each do |name|
        attributes[name] = StyledYAML.inline(attributes[name])
      end
    end)
  else
    element.to_yaml
  end.gsub(/\A---\n/, '')
end

#instrument(action = nil, payload = {}, &block) ⇒ Object



27
28
29
30
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 27

def instrument(action = nil, payload = {}, &block)
  name = [instrument_base_name, [*action]].flatten.compact.join('.')
  ActiveSupport::Notifications.instrument(name, { name: resource_name }.merge(payload), &block)
end

#instrument_base_nameObject



72
73
74
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 72

def instrument_base_name
  'wagon.pull'
end

#is_default_locale?(locale) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 84

def is_default_locale?(locale)
  default_locale == locale
end

#localesObject



88
89
90
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 88

def locales
  current_site.locales
end

#pullObject



13
14
15
16
17
18
19
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 13

def pull
  instrument do
    instrument :start
    self._pull_with_timezone
    instrument :done
  end
end

#reset_file(filepath) ⇒ Object



67
68
69
70
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 67

def reset_file(filepath)
  _filepath = File.join(path, filepath)
  FileUtils.rm(_filepath) if File.exists?(_filepath)
end

#resource_nameObject



76
77
78
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 76

def resource_name
  self.class.name[/::Pull(\w+)Command$/, 1].underscore
end

#write_to_file(filepath, content = nil, mode = 'w+', &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/locomotive/wagon/commands/pull_sub_commands/pull_base_command.rb', line 49

def write_to_file(filepath, content = nil, mode = 'w+', &block)
  _filepath = File.join(path, filepath)

  folder = File.dirname(_filepath)

  FileUtils.mkdir_p(folder) unless File.exists?(folder)

  File.open(_filepath, mode) do |file|
    if content
      file.write(content)
    elsif block_given?
      file.write(yield)
    else
      file.write('')
    end
  end
end