class Submarine

Public Class Methods

config() click to toggle source

Global Submarine configuration getter. Returns the current configuration options.

Example:

>> Submarine.config
=> #<Submarine::Configuration @format_key=:text, @left_delimiter="[[", @right_delimiter="]]", @substitutions={}>
# File lib/submarine/submarine.rb, line 46
def config
  @config ||= Configuration.new
end
configure() { |config| ... } click to toggle source

Global Submarine configuration setter. The global default Submarine::Configuration options can be overwritten by passing a block to config and setting new options.

Example:

# config/initializers/submarine.rb  <-- if you're using Rails, say
Submarine.configure do |config|
  config.format_key = :copy
  config.left_delimiter = '<^'
  config.right_delimiter = '^>'
  config.substitutions = {company: 'Submarine Inc, Co'}
end

>> Submarine.config
=> #<Submarine::Configuration @format_key=:copy, @left_delimiter="<^", @right_delimiter="^>", @substitutions={company: 'Submarine Inc, Co'}>
# File lib/submarine/submarine.rb, line 66
def configure
  yield config
end
new(*attrs) click to toggle source

Constructs a new Submarine instance. Set instance variable and readers based on input hash.

# File lib/submarine/submarine.rb, line 6
def initialize *attrs
  raise MissingAttributesError unless attrs_provided?(attrs)
  raise MissingFormatKeyError unless attrs_includes_format_key?(attrs)

  attrs = attrs.first.merge(config.substitutions) # merge Configuration defaults

  attrs.each do |key, value|
    self.instance_variable_set("@#{key}", value) 
    self.class.class_eval{attr_reader key}
  end
end

Public Instance Methods

format!() click to toggle source

Destructively format the Submarine instance. Returns the formatted string.

Example:

>> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
=> #<Submarine @text="Hello, my name is [[name]].", @name="Joe">
>> sub.format!
=> "Hello, my name is Joe."
>> sub
=> #<Submarine @text="Hello, my name is Joe.", @name="Joe">

And, obviously, one line makes it more succinct:

>> Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe').format!
=> "Hello, my name is Joe."
# File lib/submarine/submarine.rb, line 33
def format!
  substitutions.each{|s| sub_string.gsub!(match(s), replace(s))}
  sub_string
end

Private Instance Methods

attrs_includes_format_key?(attrs) click to toggle source

Was the :format_key attribute passed to Submarine.new?

# File lib/submarine/submarine.rb, line 81
def attrs_includes_format_key? attrs
  attrs.first.symbolize_keys.keys.include?(config.format_key.to_sym)
end
attrs_provided?(attrs) click to toggle source

Were any attributes passed to Submarine.new?

# File lib/submarine/submarine.rb, line 75
def attrs_provided? attrs
  !attrs.empty?
end
config() click to toggle source

Returns the global Submarine config.

Example:

>> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
>> sub.send(:config)
=> #<Submarine::Configuration @format_key=:text, @left_delimiter="[[", @right_delimiter="]]", @substitutions={}>
# File lib/submarine/submarine.rb, line 124
def config
  self.class.config
end
match(key) click to toggle source

Returns the string to match for gsub with passed in key.

Example:

>> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
>> sub.send(:match, :name)
=> "[[name]]"
# File lib/submarine/submarine.rb, line 103
def match key
  "#{config.left_delimiter}#{key}#{config.right_delimiter}"
end
replace(key) click to toggle source

Returns the replacement for gsub with passed in key.

Example:

>> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
>> sub.send(:replace, :name)
=> "Joe"
# File lib/submarine/submarine.rb, line 113
def replace key
  instance_variable_get("@#{key}").to_s
end
sub_string() click to toggle source

Returns the string to be formatted.

Example:

>> sub = Submarine.new(text: 'Format this string!')
>> sub.send(:sub_string)
=> "Format this string!"
# File lib/submarine/submarine.rb, line 92
def sub_string
  instance_variable_get("@#{config.format_key}")
end
substitutions() click to toggle source

Returns an array of symbols to use for matching.

Example:

>> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
>> sub.send(:substitutions)
=> [:text, :name]
# File lib/submarine/submarine.rb, line 135
def substitutions
  subs = self.instance_variables
  subs.delete(config.format_key)
  subs.map{|s| s.to_s.gsub('@', '').to_sym}
end