Module: Arcade::Support::String

Defined in:
lib/support/string.rb

Instance Method Summary collapse

Instance Method Details

#camelcase_and_namespaceObject

returns an Array [ Namespace(first character upcase) , Type(class, camelcase) ]

if the namespace is not found, joins all string-parts


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/support/string.rb', line 9

def camelcase_and_namespace
  if  self.count("_")  >= 1 || self.count('-') >=1
    delimiters = Regexp.union(['-', '_'])
    n,c= self.split(delimiters).then { |first, *rest| [first.tap {|s| s[0] = s[0].upcase}, rest.map(&:capitalize).join]  }
    ## if base is the first part of the type-name, probably Arcade::Base is choosen a namespace. thats wrong
    namespace_present = unless n == 'Base'
                          Object.const_get(n)  rescue  false # Database.namespace
                        else 
                          false
    end
    # if no namespace is found, leave it empty and return the capitalized string as class name
    namespace_present && !c.nil? ? [namespace_present,c] : [Database.namespace, n+c]
  else
    [ Database.namespace, self.capitalize ]
  end
end

#capitalize_first_letterObject



45
46
47
# File 'lib/support/string.rb', line 45

def capitalize_first_letter
  self.sub(/^(.)/) { $1.capitalize }
end

#load_rid(autocomplete = false) ⇒ Object

Load the database object if the string is a rid (Default: No Autoloading of rid-links)



82
83
84
# File 'lib/support/string.rb', line 82

def load_rid autocomplete = false
  db.get( self ){  autocomplete }  if rid?  rescue nil
end

#quoteObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/support/string.rb', line 66

def quote
  str = self.dup
  if str[0, 1] == "'" && str[-1, 1] == "'"
    self
  else
    last_pos = 0
    while (pos = str.index("'", last_pos))
      str.insert(pos, "\\") if pos > 0 && str[pos - 1, 1] != "\\"
      last_pos = pos + 1
    end
    "'#{str}'"
  end
end

#ridObject

return a valid rid (format: “nn:mm”) or nil



53
54
55
# File 'lib/support/string.rb', line 53

def rid
  self["#"].nil? ? "#"+ self : self if rid? 
end

#rid?Boolean

a rid is either #nn:nn or nn:nn

Returns:

  • (Boolean)


49
50
51
# File 'lib/support/string.rb', line 49

def rid?
  self =~ /\A[#]{,1}[0-9]{1,}:[0-9]{1,}\z/
end

#snake_caseObject



26
27
28
29
30
31
32
33
# File 'lib/support/string.rb', line 26

def snake_case
  n= if split('::').first == Database.namespace.to_s
     split('::')[1..-1].join
   else
     split("::").join
   end
 n.gsub(/([^\^])([A-Z])/,'\1_\2').downcase
end

#to_aObject



62
63
64
# File 'lib/support/string.rb', line 62

def to_a
  [ self ]
end

#to_humanObject

args.delete_if{|_,y|( y.is_a?(Array) || y.is_a?(Hash)) && y.blank? }

  return nil if args.empty?
  Arcade::Query.new( from: self , kind: :update, set: args).execute
end


95
96
97
# File 'lib/support/string.rb', line 95

def to_human
  self
end

#underscoreObject

borrowed from ActiveSupport::Inflector



36
37
38
39
40
41
42
43
44
# File 'lib/support/string.rb', line 36

def underscore
  word = self.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end

#where(**args) ⇒ Object



57
58
59
60
61
# File 'lib/support/string.rb', line 57

def where **args
  if rid?
    from_db.where **args
  end
end