Module: Waves::Layers::Inflect::English

Defined in:
lib/layers/inflect/english.rb

Overview

Adds plural/singular methods for English to String

Class Method Summary collapse

Class Method Details

.included(app) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/layers/inflect/english.rb', line 8

def self.included(app)
  
  require 'english/inflect'
            
  Waves::Resources::Mixin::ClassMethods.module_eval do
    def singular ; basename.snake_case.singular ; end
    def plural ; basename.snake_case.plural ; end
  end
    
  Waves::Resources::Mixin.module_eval do
    def singular ; self.class.singular ; end
    def plural ; self.class.plural ; end
  end
  
  Waves::Resources::Paths.module_eval do
    def resource ; self.class.resource.singular ; end
    def resources ; self.class.resource.plural ; end

    # TODO: be nice to DRY this up ... basically the same code 
    # as the mixin except with the singular / plural stuff
    # mixed in ...
    def generate( template, args )
      return "/#{ args * '/' }" unless template.is_a?( Array ) and not template.empty?
      path = []
      ( "/#{ path * '/' }" ) if template.all? do | want |
        case want
        when true then path += args
        when String then path << want
        when Symbol
          case want
          when :resource then path << resource
          when :resources then path << resources
          else path << args.shift
          end
        when Regexp then path << args.shift
        when Hash
          key, value = want.to_a.first
          case key
          when :resource then path << resource
          when :resources then path << resources
          else 
            case value
            when true then path += args
            when String, Symbol, RegExp then path << args.unshift
            end
          end
        end
      end
    end
  end
  
end