Class: MDWA::Generators::Model
- Inherits:
-
Object
- Object
- MDWA::Generators::Model
- Defined in:
- lib/mdwa/generators/model.rb
Instance Attribute Summary collapse
-
#associations ⇒ Object
Returns the value of attribute associations.
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#specific_model_name ⇒ Object
Returns the value of attribute specific_model_name.
Instance Method Summary collapse
- #add_association(model_association) ⇒ Object
- #add_attribute(model_attribute) ⇒ Object
- #controller_name ⇒ Object
-
#initialize(arg) ⇒ Model
constructor
Sets the variables by the string Format: <namespace>/<model>, the namespace is optional.
- #klass ⇒ Object
-
#model_class ⇒ Object
Returns the associated model in app/models folder.
- #namespace? ⇒ Boolean
- #nested_many? ⇒ Boolean
- #object_name ⇒ Object
- #plural_name ⇒ Object
- #raw ⇒ Object
- #simple_attributes ⇒ Object
- #singular_name ⇒ Object
- #space ⇒ Object
- #specific? ⇒ Boolean
- #specific_model ⇒ Object
- #to_params ⇒ Object
- #to_route_object(prefix = '') ⇒ Object
- #to_route_url ⇒ Object
- #to_sym ⇒ Object
- #valid? ⇒ Boolean
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
#associations ⇒ Object
Returns the value of attribute associations.
8 9 10 |
# File 'lib/mdwa/generators/model.rb', line 8 def associations @associations end |
#attributes ⇒ Object
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/mdwa/generators/model.rb', line 8 def attributes @attributes end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/mdwa/generators/model.rb', line 8 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
8 9 10 |
# File 'lib/mdwa/generators/model.rb', line 8 def namespace @namespace end |
#specific_model_name ⇒ Object
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_name ⇒ Object
52 53 54 |
# File 'lib/mdwa/generators/model.rb', line 52 def controller_name namespace_scope + name.pluralize end |
#klass ⇒ Object
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_class ⇒ Object
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
93 94 95 |
# File 'lib/mdwa/generators/model.rb', line 93 def namespace? !namespace.blank? end |
#nested_many? ⇒ 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_name ⇒ Object
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_name ⇒ Object
89 90 91 |
# File 'lib/mdwa/generators/model.rb', line 89 def plural_name name.underscore.pluralize end |
#raw ⇒ Object
81 82 83 |
# File 'lib/mdwa/generators/model.rb', line 81 def raw klass.underscore end |
#simple_attributes ⇒ Object
114 115 116 |
# File 'lib/mdwa/generators/model.rb', line 114 def simple_attributes attributes.select{ |a| !a.references? } end |
#singular_name ⇒ Object
85 86 87 |
# File 'lib/mdwa/generators/model.rb', line 85 def singular_name name.underscore end |
#space ⇒ Object
97 98 99 |
# File 'lib/mdwa/generators/model.rb', line 97 def space namespace.underscore end |
#specific? ⇒ Boolean
28 29 30 |
# File 'lib/mdwa/generators/model.rb', line 28 def specific? !specific_model_name.blank? end |
#specific_model ⇒ Object
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_params ⇒ Object
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_url ⇒ Object
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_sym ⇒ Object
118 119 120 |
# File 'lib/mdwa/generators/model.rb', line 118 def to_sym self.name.to_sym end |
#valid? ⇒ Boolean
24 25 26 |
# File 'lib/mdwa/generators/model.rb', line 24 def valid? name.underscore =~ /^[a-z][a-z0-9_\/]+$/ end |