Module: Arcade::Support::String

Defined in:
lib/arcade/support/string.rb

Instance Method Summary collapse

Instance Method Details

#camelcaseObject



28
29
30
31
# File 'lib/arcade/support/string.rb', line 28

def camelcase
# Split the string into words, capitalize each word, and join them together
  self.split('_').map(&:capitalize).join
end

#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
25
26
27
# File 'lib/arcade/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
    # Arcade::Base descendants are prefetched in Init.models
    # They have to be fetched to  load both  `Arcade::Ge` and `Arcade::GeSomething`
    #                (otherwise: Arcade::Ge and Arcade::Ge::Something)
    namespace_present = unless n == 'Base'  ||  Arcade::Init.models.include?(n)
                          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



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

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)



89
90
91
# File 'lib/arcade/support/string.rb', line 89

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

#quoteObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/arcade/support/string.rb', line 73

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



60
61
62
# File 'lib/arcade/support/string.rb', line 60

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

#rid?Boolean

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

Returns:

  • (Boolean)


56
57
58
# File 'lib/arcade/support/string.rb', line 56

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

#snake_caseObject



33
34
35
36
37
38
39
40
# File 'lib/arcade/support/string.rb', line 33

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



69
70
71
# File 'lib/arcade/support/string.rb', line 69

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


102
103
104
# File 'lib/arcade/support/string.rb', line 102

def to_human
  self
end

#underscoreObject

borrowed from ActiveSupport::Inflector



43
44
45
46
47
48
49
50
51
# File 'lib/arcade/support/string.rb', line 43

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



64
65
66
67
68
# File 'lib/arcade/support/string.rb', line 64

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