Class: Middleman::VCard::VCardExtension

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

Overview

A Middleman extension to generate VCards and provide useful helpers to work with.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &block) ⇒ VCardExtension

Create the VCardExtension.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/middleman-vcard/extension.rb', line 23

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

  source_dir_path = Pathname.new(app.root).join(app.config.source)

  if options[:dst_path]
    unless options[:dst_path].start_with?(source_dir_path.to_s)
      error("Invalid `dst_path`. It's not inside the source folder.")
    end
    @vcard_dir_path  = Pathname.new(File.dirname(options[:dst_path]))
    @vcard_file_name = File.basename(options[:dst_path])
  else
    @vcard_dir_path  = source_dir_path
    @vcard_file_name = "#{options[:name]}.vcf"
  end

  # Define some config used later
  #
  # NOTE: We want to be consistent with Middleman conventions, so
  #       `vcard_dir_path` is a `String` because all Middleman paths are
  #       `String`s.
  app.config.define_setting :vcard_name,      options[:name]
  app.config.define_setting :vcard_file_name, @vcard_file_name
  app.config.define_setting :vcard_dir_path,  @vcard_dir_path.to_s
end

Instance Method Details

#after_configurationObject

After Middleman has been configured (i.e. build, console, server, etc..) generate the VCard.



53
54
55
56
57
58
59
60
61
62
# File 'lib/middleman-vcard/extension.rb', line 53

def after_configuration
  vcard_generator = Middleman::VCard::Generator.new(
      options[:name],
      emails:    options[:emails],
      phones:    options[:phones],
      addresses: options[:addresses],
      photo:     options[:photo],
      logger:    logger)
  vcard_generator.generate(@vcard_dir_path.join(@vcard_file_name).to_s)
end