Class: BulldozeRenamer::StringInflector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/bulldoze_renamer/string_inflector.rb

Defined Under Namespace

Classes: StringDoesNotInflectToItselfError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current, target) ⇒ StringInflector

Returns a new instance of StringInflector.



20
21
22
23
# File 'lib/bulldoze_renamer/string_inflector.rb', line 20

def initialize(current, target)
  @current = current
  @target = target
end

Class Method Details

.active_support_inflectionsObject



10
11
12
# File 'lib/bulldoze_renamer/string_inflector.rb', line 10

def self.active_support_inflections
  [:underscore, :camelize]
end

.inflectionsObject



14
15
16
# File 'lib/bulldoze_renamer/string_inflector.rb', line 14

def self.inflections
  active_support_inflections + [:dasherize, :upcase, :js_camelize]
end

Instance Method Details

#dasherize(w) ⇒ Object



29
30
31
# File 'lib/bulldoze_renamer/string_inflector.rb', line 29

def dasherize(w)
  ActiveSupport::Inflector.dasherize(underscore(w))
end

#js_camelize(w) ⇒ Object



33
34
35
36
37
# File 'lib/bulldoze_renamer/string_inflector.rb', line 33

def js_camelize(w)
  c = camelize(w).dup
  c[0] = c[0].downcase
  c
end

#mappingsObject



39
40
41
42
43
44
45
46
47
48
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
# File 'lib/bulldoze_renamer/string_inflector.rb', line 39

def mappings
  result = {
    current_inflection: nil,
    target_inflection: nil,
    inflections: {}
  }
  StringInflector.inflections.each do |i|
    unless result[:current_inflection]
      if @current == send(i, @current)
        result[:current_inflection] = i
      end
    end

    unless result[:target_inflection]
      if @target == send(i, @target)
        result[:target_inflection] = i
      end
    end

    mapping = {
      current: send(i, @current),
      target: send(i, @target)
    }

    unless result[:inflections].
      values.
      map { |i| i[:current] }.
      include?(mapping[:current])
      result[:inflections][i] = mapping
    end
  end

  unless result[:current_inflection]
    raise StringDoesNotInflectToItselfError.new(@current)
  end

  unless result[:target_inflection]
    raise StringDoesNotInflectToItselfError.new(@target)
  end
  result
end

#upcase(w) ⇒ Object



25
26
27
# File 'lib/bulldoze_renamer/string_inflector.rb', line 25

def upcase(w)
  underscore(w).upcase
end