submarine 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 35a13bbaed8ceffacdcd6e3f96916dca97245ad0
4
+ data.tar.gz: 22222a0a7ac87624c64afc8fd100e3c222fb0710
5
+ SHA512:
6
+ metadata.gz: faeb1a6de9fc7b1264b3dc067e7e936a1084e37c17d42ee2120452a2cd9a33c7a0415763b887fd06740fdd3a3b6e4ef58e5d46ecc38000dfd5cba9b0b1ceb89d
7
+ data.tar.gz: 8998375b0f98303823a2ad9aace1645c690357d2f3dbcbe38cd18dbf7b5deee85d7a8d85e8693d2a4ff0e4e393861d0d6c270eaf0fe5be5d9a6b61caafcda514
data/lib/submarine.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'facets/hash/symbolize_keys'
2
+
3
+ require File.dirname(__FILE__) + '/submarine/submarine'
4
+ require File.dirname(__FILE__) + '/submarine/exceptions'
5
+ require File.dirname(__FILE__) + '/submarine/configuration'
@@ -0,0 +1,43 @@
1
+ class Submarine::Configuration
2
+
3
+ # Acceptable accessors.
4
+ attr_accessor :format_key, :left_delimeter, :right_delimeter, :substitutions
5
+
6
+ # Constructs a new Configuration instance.
7
+ # Merges passed in options hash, sets instance variables.
8
+ #
9
+ def initialize *attrs
10
+ options = attrs.empty? ? {} : attrs.first
11
+ options = self.class.defaults.merge(options)
12
+ set_instance_variables options
13
+ end
14
+
15
+ # Configuration defaults.
16
+ #
17
+ def self.defaults
18
+ {
19
+ format_key: :text, # The key representing the string to be formatted
20
+ left_delimeter: '[[', # The left-side matching string, ie: '[[name]]'
21
+ right_delimeter: ']]', # The right-side matching string, ie: '[[name]]'
22
+ substitutions: {} # Optional global default substitutions
23
+ }
24
+ end
25
+
26
+ # Reload the Configuration instance variables to defaults.
27
+ #
28
+ def reload!
29
+ set_instance_variables(self.class.defaults)
30
+ end
31
+
32
+ private
33
+
34
+ # Set the instance variables from passed in options hash.
35
+ #
36
+ def set_instance_variables options
37
+ @format_key = options[:format_key].to_sym
38
+ @left_delimeter = options[:left_delimeter].to_s
39
+ @right_delimeter = options[:right_delimeter].to_s
40
+ @substitutions = options[:substitutions]
41
+ end
42
+
43
+ end
@@ -0,0 +1,19 @@
1
+ class Submarine
2
+
3
+ # If no attributes are sent to Submarine.new, throw this.
4
+ #
5
+ class MissingAttributesError < StandardError
6
+ def initialize(message = 'No attributes provided')
7
+ super(message)
8
+ end
9
+ end
10
+
11
+ # If no :format_key is sent to Submarine.new, throw this.
12
+ #
13
+ class MissingFormatKeyError < StandardError
14
+ def initialize(message = 'No format key provided')
15
+ super(message)
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,141 @@
1
+ class Submarine
2
+
3
+ # Constructs a new Submarine instance.
4
+ # Set instance variable and readers based on input hash.
5
+ #
6
+ def initialize *attrs
7
+ raise MissingAttributesError unless attrs_provided?(attrs)
8
+ raise MissingFormatKeyError unless attrs_includes_format_key?(attrs)
9
+
10
+ attrs = attrs.first.merge(config.substitutions) # merge Configuration defaults
11
+
12
+ attrs.each do |key, value|
13
+ self.instance_variable_set("@#{key}", value)
14
+ self.class.class_eval{attr_reader key}
15
+ end
16
+ end
17
+
18
+ # Destructively format the Submarine instance.
19
+ # Returns the formatted string.
20
+ #
21
+ # Example:
22
+ # >> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
23
+ # => #<Submarine @text="Hello, my name is [[name]].", @name="Joe">
24
+ # >> sub.format!
25
+ # => "Hello, my name is Joe."
26
+ # >> sub
27
+ # => #<Submarine @text="Hello, my name is Joe.", @name="Joe">
28
+ #
29
+ # And, obviously, one line makes it more succinct:
30
+ # >> Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe').format!
31
+ # => "Hello, my name is Joe."
32
+ #
33
+ def format!
34
+ substitutions.each{|s| sub_string.gsub!(match(s), replace(s))}
35
+ sub_string
36
+ end
37
+
38
+ class << self
39
+ # Global Submarine configuration getter.
40
+ # Returns the current configuration options.
41
+ #
42
+ # Example:
43
+ # >> Submarine.config
44
+ # => #<Submarine::Configuration @format_key=:text, @left_delimeter="[[", @right_delimeter="]]", @substitutions={}>
45
+ #
46
+ def config
47
+ @config ||= Configuration.new
48
+ end
49
+
50
+ # Global Submarine configuration setter.
51
+ # The global default Submarine::Configuration options can be overwritten
52
+ # by passing a block to config and setting new options.
53
+ #
54
+ # Example:
55
+ # # config/initializers/submarine.rb <-- if you're using Rails, say
56
+ # Submarine.configure do |config|
57
+ # config.format_key = :copy
58
+ # config.left_delimeter = '<^'
59
+ # config.right_delimeter = '^>'
60
+ # config.substitutions = {company: 'Submarine Inc, Co'}
61
+ # end
62
+ #
63
+ # >> Submarine.config
64
+ # => #<Submarine::Configuration @format_key=:copy, @left_delimeter="<^", @right_delimeter="^>", @substitutions={company: 'Submarine Inc, Co'}>
65
+ #
66
+ def configure
67
+ yield config
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ # Were any attributes passed to Submarine.new?
74
+ #
75
+ def attrs_provided? attrs
76
+ !attrs.empty?
77
+ end
78
+
79
+ # Was the :format_key attribute passed to Submarine.new?
80
+ #
81
+ def attrs_includes_format_key? attrs
82
+ attrs.first.symbolize_keys.keys.include?(config.format_key.to_sym)
83
+ end
84
+
85
+ # Returns the string to be formatted.
86
+ #
87
+ # Example:
88
+ # >> sub = Submarine.new(text: 'Format this string!')
89
+ # >> sub.send(:sub_string)
90
+ # => "Format this string!"
91
+ #
92
+ def sub_string
93
+ instance_variable_get("@#{config.format_key}")
94
+ end
95
+
96
+ # Returns the string to match for gsub with passed in key.
97
+ #
98
+ # Example:
99
+ # >> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
100
+ # >> sub.send(:match, :name)
101
+ # => "[[name]]"
102
+ #
103
+ def match key
104
+ "#{config.left_delimeter}#{key}#{config.right_delimeter}"
105
+ end
106
+
107
+ # Returns the replacement for gsub with passed in key.
108
+ #
109
+ # Example:
110
+ # >> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
111
+ # >> sub.send(:replace, :name)
112
+ # => "Joe"
113
+ def replace key
114
+ instance_variable_get("@#{key}").to_s
115
+ end
116
+
117
+ # Returns the global Submarine config.
118
+ #
119
+ # Example:
120
+ # >> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
121
+ # >> sub.send(:config)
122
+ # => #<Submarine::Configuration @format_key=:text, @left_delimeter="[[", @right_delimeter="]]", @substitutions={}>
123
+ #
124
+ def config
125
+ self.class.config
126
+ end
127
+
128
+ # Returns an array of symbols to use for matching.
129
+ #
130
+ # Example:
131
+ # >> sub = Submarine.new(text: 'Hello, my name is [[name]].', name: 'Joe')
132
+ # >> sub.send(:substitutions)
133
+ # => [:text, :name]
134
+ #
135
+ def substitutions
136
+ subs = self.instance_variables
137
+ subs.delete(config.format_key)
138
+ subs.map{|s| s.to_s.gsub('@', '').to_sym}
139
+ end
140
+
141
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: submarine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Caleb K Matthiesen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: facets
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.9.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.9.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.4.2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.4.2
55
+ description: A lightweight string formatter.
56
+ email: c@calebkm.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/submarine.rb
62
+ - lib/submarine/configuration.rb
63
+ - lib/submarine/exceptions.rb
64
+ - lib/submarine/submarine.rb
65
+ homepage: http://www.submarine-gem.org
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Deep sea string substitution.
89
+ test_files: []
90
+ has_rdoc: