Class: MDWA::Generators::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/mdwa/generators/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ Model

Sets the variables by the string Format: <namespace>/<model>, the namespace is optional.



14
15
16
17
18
19
20
21
22
# File 'lib/mdwa/generators/model.rb', line 14

def initialize( arg )
	
	self.namespace = '' # prevents unitialized variable errors
	self.namespace = arg.split('/').first.camelize if arg.split('/').count > 1
 		self.name = arg.split('/').last.singularize.camelize

 		self.attributes = []
 		self.associations = []
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



8
9
10
# File 'lib/mdwa/generators/model.rb', line 8

def associations
  @associations
end

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/mdwa/generators/model.rb', line 8

def attributes
  @attributes
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/mdwa/generators/model.rb', line 8

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



8
9
10
# File 'lib/mdwa/generators/model.rb', line 8

def namespace
  @namespace
end

#specific_model_nameObject

Returns the value of attribute specific_model_name.



8
9
10
# File 'lib/mdwa/generators/model.rb', line 8

def specific_model_name
  @specific_model_name
end

Instance Method Details

#add_association(model_association) ⇒ Object



110
111
112
# File 'lib/mdwa/generators/model.rb', line 110

def add_association(model_association)
	self.associations << model_association
end

#add_attribute(model_attribute) ⇒ Object



105
106
107
108
# File 'lib/mdwa/generators/model.rb', line 105

def add_attribute(model_attribute)
	self.attributes << model_attribute
	model_attribute.model = self
end

#controller_nameObject



52
53
54
# File 'lib/mdwa/generators/model.rb', line 52

def controller_name
	namespace_scope + name.pluralize
end

#klassObject



37
38
39
40
41
42
43
# File 'lib/mdwa/generators/model.rb', line 37

def klass
  if !specific?
	  return (namespace_scope + name)
     else 
       return specific_model.klass
     end
end

#model_classObject

Returns the associated model in app/models folder



48
49
50
# File 'lib/mdwa/generators/model.rb', line 48

def model_class
self.klass.classify.constantize
end

#namespace?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/mdwa/generators/model.rb', line 93

def namespace?
	!namespace.blank?
end

#nested_many?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/mdwa/generators/model.rb', line 101

def nested_many?
 self.attributes.select{|a| a.nested_many?}.count.zero? or self.associations.select{|a| a.nested_many?}.count.zero?
end

#object_nameObject



56
57
58
59
# File 'lib/mdwa/generators/model.rb', line 56

def object_name
  return (space + '_' + singular_name) if !space.blank?
  return singular_name
end

#plural_nameObject



89
90
91
# File 'lib/mdwa/generators/model.rb', line 89

def plural_name
	name.underscore.pluralize
end

#rawObject



81
82
83
# File 'lib/mdwa/generators/model.rb', line 81

def raw
	klass.underscore
end

#simple_attributesObject



114
115
116
# File 'lib/mdwa/generators/model.rb', line 114

def simple_attributes
	attributes.select{ |a| !a.references? }
end

#singular_nameObject



85
86
87
# File 'lib/mdwa/generators/model.rb', line 85

def singular_name
	name.underscore
end

#spaceObject



97
98
99
# File 'lib/mdwa/generators/model.rb', line 97

def space
	namespace.underscore
end

#specific?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/mdwa/generators/model.rb', line 28

def specific?
 !specific_model_name.blank?
end

#specific_modelObject



32
33
34
35
# File 'lib/mdwa/generators/model.rb', line 32

def specific_model
 return Model.new( specific_model_name ) if specific?
   return nil
end

#to_paramsObject



61
62
63
64
65
66
67
# File 'lib/mdwa/generators/model.rb', line 61

def to_params
 if !specific?
  return object_name
    else 
      return singular_name
    end
end

#to_route_object(prefix = '') ⇒ Object



76
77
78
79
# File 'lib/mdwa/generators/model.rb', line 76

def to_route_object(prefix = '')
 return "#{prefix}#{singular_name}" if !specific? or !namespace?
 return "[ :#{space}, #{prefix}#{singular_name}]" if specific? and namespace?
end

#to_route_urlObject



69
70
71
72
73
74
# File 'lib/mdwa/generators/model.rb', line 69

def to_route_url
 url = []
 url << space if namespace?
 url << plural_name
 url.join('/')
end

#to_symObject



118
119
120
# File 'lib/mdwa/generators/model.rb', line 118

def to_sym
	self.name.to_sym
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/mdwa/generators/model.rb', line 24

def valid?
	name.underscore =~ /^[a-z][a-z0-9_\/]+$/
end