Class: Gorillib::Model::Name

Inherits:
String show all
Defined in:
lib/gorillib/model/active_model_naming.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from String

#blank?, #camelize, #constantize, #demodulize, #humanize, #safe_constantize, #snakeize, #titleize, #truncate, #underscore

Constructor Details

#initialize(klass, namespace = nil, name = nil) ⇒ Name

Returns a new instance of Name.

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gorillib/model/active_model_naming.rb', line 11

def initialize(klass, namespace = nil, name = nil)
  name ||= klass.name
  raise ArgumentError, "Class name cannot be blank. You need to supply a name argument when anonymous class given" if name.blank?

  super(name)

  @klass          = klass
  @singular       = _singularize(self).freeze
  @element        = inflector.underscore(inflector.demodulize(self)).freeze
  @human          = inflector.humanize(@element).freeze
  @i18n_key       = inflector.underscore(self).to_sym
  #
  self.namespace  = namespace
  self.plural     = inflector.pluralize(@singular)
end

Instance Attribute Details

#collectionObject (readonly) Also known as: cache_key

Returns the value of attribute collection.



4
5
6
# File 'lib/gorillib/model/active_model_naming.rb', line 4

def collection
  @collection
end

#elementObject (readonly)

Returns the value of attribute element.



4
5
6
# File 'lib/gorillib/model/active_model_naming.rb', line 4

def element
  @element
end

#i18n_keyObject (readonly)

Returns the value of attribute i18n_key.



4
5
6
# File 'lib/gorillib/model/active_model_naming.rb', line 4

def i18n_key
  @i18n_key
end

#namespaceObject

Returns the value of attribute namespace.



3
4
5
# File 'lib/gorillib/model/active_model_naming.rb', line 3

def namespace
  @namespace
end

#param_keyObject (readonly)

Returns the value of attribute param_key.



4
5
6
# File 'lib/gorillib/model/active_model_naming.rb', line 4

def param_key
  @param_key
end

#partial_pathObject (readonly)

Returns the value of attribute partial_path.



4
5
6
# File 'lib/gorillib/model/active_model_naming.rb', line 4

def partial_path
  @partial_path
end

#singularObject (readonly)

Returns the value of attribute singular.



4
5
6
# File 'lib/gorillib/model/active_model_naming.rb', line 4

def singular
  @singular
end

Instance Method Details

#plural=(str) ⇒ Object



27
28
29
30
31
32
# File 'lib/gorillib/model/active_model_naming.rb', line 27

def plural=(str)
  @plural         = str.dup.freeze
  @collection     = inflector.underscore(@plural).freeze
  @partial_path   = "#{@collection}/#{@element}".freeze
  str
end

#route_keyObject



48
49
50
51
52
# File 'lib/gorillib/model/active_model_naming.rb', line 48

def route_key
  rk = (namespace ? inflector.pluralize(param_key) : plural.dup)
  rk << "_index" if plural == singular
  rk.freeze
end

#singular_route_keyObject



44
45
46
# File 'lib/gorillib/model/active_model_naming.rb', line 44

def singular_route_key
  inflector.singularize(route_key).freeze
end