File: inflector.rb

package info (click to toggle)
ruby-postmark 1.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: ruby: 5,413; makefile: 4
file content (28 lines) | stat: -rw-r--r-- 544 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Postmark
  module Inflector

    extend self

    def to_postmark(name)
      name.to_s.split('_').map { |part| capitalize_first_letter(part) }.join('')
    end

    def to_ruby(name)
      name.to_s.scan(camel_case_regexp).join('_').downcase.to_sym
    end

    def camel_case_regexp
      /(?:[A-Z](?:(?:[A-Z]+(?![a-z\d]))|[a-z\d]*))|[a-z\d\_]+/
    end

    protected

    def capitalize_first_letter(str)
      if str.length > 0
        str.slice(0..0).capitalize + str.slice(1..-1)
      else
        str
      end
    end
  end
end