Class: Contours::StructuredString

Inherits:
String
  • Object
show all
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

Instance Method Summary collapse

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.

Examples:

config = StructuredString.new("input")
config << "my-special-class"
config << "input-error"
config.to_s #=> "input my-special-class input-error"

Parameters:

  • base (String) (defaults to: "")

    the initial string

  • separator (String) (defaults to: " ")

    the separator used to join the parts of the string



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

Examples:

config = StructuredString.new("input")
config << "my-special-class"
config.to_s #=> "input my-special-class"

Parameters:

  • value (String)

    the value to add



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_jsonObject



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

Examples:

config = StructuredString.new("input")
config.first("my-special-class")
config.to_s #=> "my-special-class input"

Parameters:

  • value (String)

    the value to add



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

Examples:

config = StructuredString.new("input")
config.last("input-error")
config.to_s #=> "input input-error"

Parameters:

  • value (String)

    the value to add



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

Examples:

config = StructuredString.new("input")
config << "my-special-class"
config.first("first-class")
config.last("last-class")
config.read(:base) #=> ["input"]
config.read(:first) #=> ["first-class"]
config.read(:last) #=> ["last-class"]
config.read(:other) #=> ["my-special-class"]

Parameters:

  • key (Symbol)

    the portion of the string to read

Returns:

  • (Array)

    the portion of the string



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_jsonObject



187
188
189
# File 'lib/contours/structured_string.rb', line 187

def to_json(*)
  to_s.to_json
end

#to_sString Also known as: to_str, inspect

Return the string representation of the StructuredString

Examples:

config = StructuredString.new("input")
config << "my-special-class"
config.last("last-class")
config.first("first-class")
config << "input-error"
config.to_s #=> "first-class input my-special-class input-error last-class"

Returns:

  • (String)

    the string



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