Class: Contours::StructuredString
- Inherits:
-
String
- Object
- String
- Contours::StructuredString
- Defined in:
- lib/contours/structured_string.rb
Overview
This class represents a string which has a particular order to its content. The intended use is for setting up CSS class values for HTML.
An element may required that the first part of a class be a particular value and that the last part of a class be a particular value. For example, a field with an error might have a class like this:
"input my-special-class input-error"
Setting that up with a StructuredString would look like this:
config = StructuredString.new("input").last("input-error")
config << "my-special-class"
config.to_s #=> "input my-special-class input-error"
Class Method Summary collapse
-
.init(base) ⇒ Object
Ensure that the argument string is a StructuredString.
Instance Method Summary collapse
-
#<<(other) ⇒ Object
Add a value to the middle of the string.
- #==(other) ⇒ Object
- #as_json ⇒ Object
-
#first(value) ⇒ Object
Add a value to the beginning of the string.
-
#initialize(base = "", separator: " ") ⇒ StructuredString
constructor
Initialize with base string that is used to build the StructuredString Other values will be added to the string in the order they are added.
-
#last(value) ⇒ Object
Add a value to the end of the string.
-
#merge(data) ⇒ Object
Add a value to the string in a particular position The argument must be a string or a 2 element array If it is a 2 element array, the first element must be a string and the second element must the name of a method used to merge the string to the StructuredString.
-
#read(key) ⇒ Array
Read a particular portion of the structured string or raise an error.
- #to_json ⇒ Object
-
#to_s ⇒ String
(also: #to_str, #inspect)
Return the string representation of the StructuredString.
Constructor Details
#initialize(base = "", separator: " ") ⇒ StructuredString
Initialize with base string that is used to build the StructuredString Other values will be added to the string in the order they are added.
41 42 43 44 45 46 47 |
# File 'lib/contours/structured_string.rb', line 41 def initialize(base = "", separator: " ") @base = [base] @separator = separator @first = [] @last = [] @other = [] end |
Class Method Details
.init(base) ⇒ Object
Ensure that the argument string is a StructuredString
21 22 23 24 25 26 27 |
# File 'lib/contours/structured_string.rb', line 21 def self.init(base) if base.is_a?(StructuredString) base else new(base) end end |
Instance Method Details
#<<(other) ⇒ Object
Add a value to the middle of the string
88 89 90 91 92 |
# File 'lib/contours/structured_string.rb', line 88 def <<(other) @other << other.to_s __setobj__ to_s self end |
#==(other) ⇒ Object
191 192 193 |
# File 'lib/contours/structured_string.rb', line 191 def ==(other) to_s == other.to_s end |
#as_json ⇒ Object
183 184 185 |
# File 'lib/contours/structured_string.rb', line 183 def as_json(*) to_s end |
#first(value) ⇒ Object
Add a value to the beginning of the string
58 59 60 61 62 |
# File 'lib/contours/structured_string.rb', line 58 def first(value) @first << value.to_s __setobj__ to_s self end |
#last(value) ⇒ Object
Add a value to the end of the string
73 74 75 76 77 |
# File 'lib/contours/structured_string.rb', line 73 def last(value) @last << value.to_s __setobj__ to_s self end |
#merge(data) ⇒ Object
Add a value to the string in a particular position The argument must be a string or a 2 element array If it is a 2 element array, the first element must be a string and the second element must the name of a method used to merge the string to the StructuredString.
For example:
config = StructuredString.new("input")
config.merge("my-special-class")
config.merge(["input-error", :last])
config.to_s #=> "input my-special-class input-error"
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/contours/structured_string.rb', line 138 def merge(data) case data in StructuredString @first += data.read :first @base += data.read :base @other += data.read :other @last += data.read :last in [String, Symbol] if method(data.last)&.arity == 1 send(data.last, data.first) else raise ArgumentError, %(Tried to use '#{data.last}' with "#{data.first}" but it doesn't take 1 argument) end in String send(:<<, data) else raise ArgumentError, "Must be a string or a 2 element array got: #{data.inspect}" end self end |
#read(key) ⇒ Array
Read a particular portion of the structured string or raise an error
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/contours/structured_string.rb', line 110 def read(key) case key when :first @first when :base @base when :other @other when :last @last else raise ArgumentError, "Unknown accessor: #{key.inspect}" end end |
#to_json ⇒ Object
187 188 189 |
# File 'lib/contours/structured_string.rb', line 187 def to_json(*) to_s.to_json end |
#to_s ⇒ String Also known as: to_str, inspect
Return the string representation of the StructuredString
172 173 174 175 176 177 178 179 |
# File 'lib/contours/structured_string.rb', line 172 def to_s [@first, @base, @other, @last] .flatten .compact .map(&:to_s) .reject(&:empty?) .join(@separator) end |