Class: Middleman::S3SyncExtension

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman-s3_sync/extension.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options_hash = {}, &block) ⇒ S3SyncExtension

Returns a new instance of S3SyncExtension.



44
45
46
# File 'lib/middleman-s3_sync/extension.rb', line 44

def initialize(app, options_hash = {}, &block)
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegate option readers to the options object



91
92
93
94
95
96
97
# File 'lib/middleman-s3_sync/extension.rb', line 91

def method_missing(method, *args, &block)
  if options.respond_to?(method)
    options.send(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#acl_enabled?Boolean

Returns:



82
83
84
85
86
87
88
# File 'lib/middleman-s3_sync/extension.rb', line 82

def acl_enabled?
  # ACLs are disabled if acl is explicitly set to nil, empty string, or false
  acl_value = options.acl
  return false if acl_value.nil? || acl_value == '' || acl_value == false
  # Otherwise ACLs are enabled (using default or explicit value)
  true
end

#after_buildObject



63
64
65
# File 'lib/middleman-s3_sync/extension.rb', line 63

def after_build
  ::Middleman::S3Sync.sync() if options.after_build
end

#after_configurationObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/middleman-s3_sync/extension.rb', line 48

def after_configuration
  read_config
  options.aws_access_key_id ||= ENV['AWS_ACCESS_KEY_ID']
  options.aws_secret_access_key ||= ENV['AWS_SECRET_ACCESS_KEY']
  options.aws_session_token ||= ENV['AWS_SESSION_TOKEN'] || ENV['AWS_SECURITY_TOKEN']
  options.bucket ||= ENV['AWS_BUCKET']
  options.http_prefix = app.http_prefix if app.respond_to? :http_prefix
  options.build_dir ||= app.build_dir if app.respond_to? :build_dir
  if options.prefix
    options.prefix = options.prefix.end_with?("/") ? options.prefix : options.prefix + "/"
    options.prefix = "" if options.prefix == "/"
  end
  ::Middleman::S3Sync.s3_sync_options = s3_sync_options
end

#caching_policy(content_type, policy = {}) ⇒ Object



130
131
132
# File 'lib/middleman-s3_sync/extension.rb', line 130

def caching_policy(content_type, policy = {})
  ::Middleman::S3Sync.add_caching_policy(content_type, policy)
end

#default_caching_policy(policy = {}) ⇒ Object



126
127
128
# File 'lib/middleman-s3_sync/extension.rb', line 126

def default_caching_policy(policy = {})
  ::Middleman::S3Sync.add_caching_policy(:default, policy)
end

#manipulate_resource_list(resources) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/middleman-s3_sync/extension.rb', line 67

def manipulate_resource_list(resources)
  ::Middleman::S3Sync.mm_resources = resources.each_with_object([]) do |resource, list|
    next if resource.ignored?

    list << resource
    list << resource.target_resource if resource.respond_to?(:target_resource)
  end

  resources
end

#read_config(io = nil) ⇒ void

This method returns an undefined value.

Read config options from an IO stream and set them on self. Defaults to reading from the .s3_sync file in the MM project root if it exists.

Parameters:

  • (defaults to: nil)

    an IO stream to read from



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/middleman-s3_sync/extension.rb', line 108

def read_config(io = nil)
  unless io
    root_path = ::Middleman::Application.root
    config_file_path = File.join(root_path, ".s3_sync")

    # skip if config file does not exist
    return unless File.exist?(config_file_path)

    io = File.open(config_file_path, "r")
  end

  config = (YAML.load(io) || {}).symbolize_keys

  config.each do |key, value|
    options[key.to_sym] = value
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:



99
100
101
# File 'lib/middleman-s3_sync/extension.rb', line 99

def respond_to_missing?(method, include_private = false)
  options.respond_to?(method) || super
end

#s3_sync_optionsObject



78
79
80
# File 'lib/middleman-s3_sync/extension.rb', line 78

def s3_sync_options
  self
end