Class: String

Inherits:
Object show all
Defined in:
lib/ayril/core_ext/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#end_with?(string) ⇒ Boolean

Returns:

  • (Boolean)


91
# File 'lib/ayril/core_ext/core_ext.rb', line 91

def end_with?(string) self.index(string) == (self.length - string.length) end

#interpolate(object, pattern = /(^|.|\r|\n)(#\{(.*?)\})/) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ayril/core_ext/core_ext.rb', line 49

def interpolate(object, pattern=/(^|.|\r|\n)(#\{(.*?)\})/)
  self.gsub(pattern) do |match|
    return '' if object.nil?

    before = $1 || ''
    return $2 if before == '\\'

    ctx = object; expr = $3
    pattern = /^([^.\[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/
    match = pattern.match(expr)
    return before if match.nil?
    
    while not match.nil?
      comp = match[1].start_with?('[') ? match[2].gsub('\\\\]', ']') : match[1]
      if ctx.kind_of?(Array) or ctx.kind_of?(MatchData)
        ctx = ctx[comp.to_i]
      elsif ctx.kind_of?(Hash)
        types = ctx.keys.invoke(:class)
        
        major_type = types.uniq.map do |type| 
          [type, types.find_all { |i| i == type }.length]
        end.max { |a, b| a[1] <=> b[1] }[0]
        
        method = {
          Symbol => :to_sym,
          String => :to_s
        }[major_type]

        ctx = ctx[comp.send method]
      else
        ctx = ctx[comp]
      end
      break if ctx.nil? or match[3] == ''
      expr = expr[(match[3] == '[' ? match[1].length : match[0].length)..-1]
      match = pattern.match expr
    end
    
    before + ctx.to_s
  end
end

#start_with?(string) ⇒ Boolean

Returns:

  • (Boolean)


90
# File 'lib/ayril/core_ext/core_ext.rb', line 90

def start_with?(string) self.index(string) == 0 end

#to_elemObject



93
# File 'lib/ayril/core_ext/core_ext.rb', line 93

def to_elem; Ayril::XMLElement.new self end