Class: Dphil::ScriptString
- Inherits:
-
Object
- Object
- Dphil::ScriptString
- Extended by:
- Forwardable
- Defined in:
- lib/dphil/script_string.rb
Instance Attribute Summary collapse
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
-
#downcase ⇒ Object
String methods implemented to return ScString intances wherever possible.
- #downcase! ⇒ Object
- #gsub(pattern, rep_hash = nil) ⇒ Object
- #gsub!(pattern, rep_hash = nil) ⇒ Object
-
#initialize(str, script = nil) ⇒ ScriptString
constructor
A new instance of ScriptString.
- #inspect ⇒ Object
- #scan(pattern) ⇒ Object
- #script ⇒ Object
- #script=(script) ⇒ Object
- #slice(a, b = nil) ⇒ Object (also: #[])
- #slice!(a, b = nil) ⇒ Object
- #strip ⇒ Object
- #strip! ⇒ Object
- #transliterate(target) ⇒ Object
- #transliterate!(target) ⇒ Object
Constructor Details
#initialize(str, script = nil) ⇒ ScriptString
Returns a new instance of ScriptString.
12 13 14 15 16 17 18 |
# File 'lib/dphil/script_string.rb', line 12 def initialize(str, script = nil) raise "Source must be a String" unless str.respond_to?(:to_str) str = str.to_str str = str.dup if str.frozen? @string = str.encode!(Encoding::UTF_8) self.script = script || self.script end |
Instance Attribute Details
#string ⇒ Object (readonly)
Returns the value of attribute string.
10 11 12 |
# File 'lib/dphil/script_string.rb', line 10 def string @string end |
Instance Method Details
#downcase ⇒ Object
String methods implemented to return ScString intances wherever possible
52 53 54 |
# File 'lib/dphil/script_string.rb', line 52 def downcase self.class.new(Transliterate.unicode_downcase(@string), @script) end |
#downcase! ⇒ Object
56 57 58 59 |
# File 'lib/dphil/script_string.rb', line 56 def downcase! ret_val = Transliterate.unicode_downcase!(@string) self unless ret_val.nil? end |
#gsub(pattern, rep_hash = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dphil/script_string.rb', line 65 def gsub(pattern, rep_hash = nil) ret_val = if block_given? @string.gsub(pattern, &Proc.new) elsif !rep_hash.nil? @string.gsub(pattern, rep_hash) else @string.gsub(pattern) end return ret_val if ret_val.is_a?(Enumerator) self.class.new(ret_val, @script) end |
#gsub!(pattern, rep_hash = nil) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dphil/script_string.rb', line 77 def gsub!(pattern, rep_hash = nil) ret_val = if block_given? @string.gsub!(pattern, &Proc.new) elsif !rep_hash.nil? @string.gsub!(pattern, rep_hash) else @string.gsub!(pattern) end return ret_val if ret_val.is_a?(Enumerator) self unless ret_val.nil? end |
#inspect ⇒ Object
61 62 63 |
# File 'lib/dphil/script_string.rb', line 61 def inspect "#{@string.inspect}:#{script}" end |
#scan(pattern) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dphil/script_string.rb', line 89 def scan(pattern) ret_val = if block_given? @string.scan(pattern, &Proc.new) else @string.scan(pattern) end return self if ret_val == @string ret_val.map do |match| next self.class.new(match, @script) if match.is_a?(String) match.map do |group| self.class.new(group, @script) end end end |
#script ⇒ Object
20 21 22 |
# File 'lib/dphil/script_string.rb', line 20 def script @script ||= Transliterate.detect(@string) end |
#script=(script) ⇒ Object
24 25 26 |
# File 'lib/dphil/script_string.rb', line 24 def script=(script) @script = script.try(:flat_map, &:to_sym) || script.to_sym end |
#slice(a, b = nil) ⇒ Object Also known as: []
104 105 106 107 |
# File 'lib/dphil/script_string.rb', line 104 def slice(a, b = nil) slice = b.nil? ? @string.slice(a) : @string.slice(a, b) self.class.new(slice, @script) end |
#slice!(a, b = nil) ⇒ Object
110 111 112 113 |
# File 'lib/dphil/script_string.rb', line 110 def slice!(a, b = nil) slice = b.nil? ? @string.slice!(a) : @string.slice!(a, b) self.class.new(slice, @script) end |
#strip ⇒ Object
115 116 117 |
# File 'lib/dphil/script_string.rb', line 115 def strip self.class.new(@string.strip, @script) end |
#strip! ⇒ Object
119 120 121 122 |
# File 'lib/dphil/script_string.rb', line 119 def strip! ret_val = @string.strip! self unless ret_val.nil? end |
#transliterate(target) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dphil/script_string.rb', line 28 def transliterate(target) target = target.to_sym string = Transliterate.transliterate(@string, from: @script, to: target) if @script.is_a?(Array) new_target = @script.dup new_target[0] = target new_target.uniq! target = new_target end self.class.new(string, target) end |
#transliterate!(target) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/dphil/script_string.rb', line 40 def transliterate!(target) target = target.to_sym @string = Transliterate.transliterate(@string, from: @script, to: target) if @script.is_a?(Array) @script[0] = target @script.uniq! end @string end |