Class: MDWA::DSL::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/mdwa/dsl/entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Entity

Returns a new instance of Entity.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mdwa/dsl/entity.rb', line 12

def initialize( name )
  # set the entity name
  self.name = name
  
  # arrays
  self.attributes       = {}
  self.associations     = {}
  self.actions          = EntityActions.new(self)
  self.specifications   = []
  self.code_generations = []
  self.in_requirements  = []
  
  # fixed attributes
  self.resource    = true
  self.user        = false
  self.ajax        = false
  self.force       = false
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



10
11
12
# File 'lib/mdwa/dsl/entity.rb', line 10

def actions
  @actions
end

#ajaxObject

Returns the value of attribute ajax.



9
10
11
# File 'lib/mdwa/dsl/entity.rb', line 9

def ajax
  @ajax
end

#associationsObject

Returns the value of attribute associations.



10
11
12
# File 'lib/mdwa/dsl/entity.rb', line 10

def associations
  @associations
end

#attributesObject

Returns the value of attribute attributes.



10
11
12
# File 'lib/mdwa/dsl/entity.rb', line 10

def attributes
  @attributes
end

#code_generationsObject

Returns the value of attribute code_generations.



10
11
12
# File 'lib/mdwa/dsl/entity.rb', line 10

def code_generations
  @code_generations
end

#forceObject

Returns the value of attribute force.



9
10
11
# File 'lib/mdwa/dsl/entity.rb', line 9

def force
  @force
end

#in_requirementsObject

Returns the value of attribute in_requirements.



10
11
12
# File 'lib/mdwa/dsl/entity.rb', line 10

def in_requirements
  @in_requirements
end

#model_nameObject

Returns the value of attribute model_name.



9
10
11
# File 'lib/mdwa/dsl/entity.rb', line 9

def model_name
  @model_name
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/mdwa/dsl/entity.rb', line 9

def name
  @name
end

#purposeObject

Returns the value of attribute purpose.



9
10
11
# File 'lib/mdwa/dsl/entity.rb', line 9

def purpose
  @purpose
end

#resourceObject

Returns the value of attribute resource.



9
10
11
# File 'lib/mdwa/dsl/entity.rb', line 9

def resource
  @resource
end

#scaffold_nameObject

Returns the value of attribute scaffold_name.



9
10
11
# File 'lib/mdwa/dsl/entity.rb', line 9

def scaffold_name
  @scaffold_name
end

#specificationsObject

Returns the value of attribute specifications.



10
11
12
# File 'lib/mdwa/dsl/entity.rb', line 10

def specifications
  @specifications
end

#userObject

Returns the value of attribute user.



9
10
11
# File 'lib/mdwa/dsl/entity.rb', line 9

def user
  @user
end

Instance Method Details

#after_declarationObject

Executed after the entity is declared, but before the inclusion in the entities singleton array.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mdwa/dsl/entity.rb', line 73

def after_declaration
  
  # include rails default attributes
  self.attribute('id', 'integer') if self.attributes['id'].blank?
  self.attribute('created_at', 'datetime') if self.attributes['created_at'].blank?
  self.attribute('updated_at', 'datetime') if self.attributes['updated_at'].blank?
  self.attribute('removed', 'boolean') if self.attributes['removed'].blank?
  # if it's a user and have no attributes, include "name" to prevent errors
  if user?
    self.attribute('name', 'string') if self.attributes['name'].blank?
    self.attribute('email', 'string') if self.attributes['email'].blank?
    self.attribute('password', 'password') if self.attributes['password'].blank?
    self.attribute('password_confirmation', 'password') if self.attributes['password_confirmation'].blank?
  end
  
end

#ajax?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mdwa/dsl/entity.rb', line 58

def ajax?
  self.ajax
end

#association {|assoc| ... } ⇒ Object

Defines one association

Yields:

  • (assoc)


105
106
107
108
109
110
# File 'lib/mdwa/dsl/entity.rb', line 105

def association
  assoc = EntityAssociation.new(self)
  yield( assoc ) if block_given?
  # assoc.raise_errors_if_invalid!
  self.associations[assoc.name] = assoc
end

#attribute(name, type, options = {}) ⇒ Object

Declares one attribute of the list using the block given.



92
93
94
95
96
97
98
99
100
# File 'lib/mdwa/dsl/entity.rb', line 92

def attribute(name, type, options = {})     
  attr = EntityAttribute.new(self)
  attr.name = name
  attr.type = type
  attr.default = true if options.include?(:default) and options[:default]
  attr.options = options
  attr.raise_errors_if_invalid!
  self.attributes[attr.name] = attr
end

#collection_action(name, method, request_type) ⇒ Object

Include a collection action Params: name, method = get, request_type = html



122
123
124
# File 'lib/mdwa/dsl/entity.rb', line 122

def collection_action(name, method, request_type)
  self.actions.collection_action(name, method, request_type)
end

#default_attributeObject

Selects the default attribute of the entity



167
168
169
170
171
172
173
# File 'lib/mdwa/dsl/entity.rb', line 167

def default_attribute
  default_attr = self.attributes.values.first # first element value
  self.attributes.each do |key, attr|
    default_attr = attr if attr.default?
  end
  return default_attr
end

#file_nameObject

Entity file name



160
161
162
# File 'lib/mdwa/dsl/entity.rb', line 160

def file_name
  self.name.singularize.underscore
end

#force?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/mdwa/dsl/entity.rb', line 62

def force?
  self.force
end

#generateObject

Generate MDWA scaffold code for structural schema.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/mdwa/dsl/entity.rb', line 178

def generate
  # generates nothing if is not a resource
  return nil unless self.resource
  
  gen = []
  gen << scaffold_name
  
  attributes.each do |key, attr|
    gen << attr.generate
  end
  
  associations.each do |key, assoc|
    gen << assoc.generate
  end
  
  gen << "--ajax" if ajax
  gen << "--force" if force
  gen << "--model='#{model_name}'" if model_name != scaffold_name
  
  "mdwa:#{user? ? 'user_scaffold': 'scaffold'} #{gen.join(' ')}"
end

#generator_modelObject

Return an instance of Generators::Model



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mdwa/dsl/entity.rb', line 139

def generator_model
  @generator_model = Generators::Model.new(self.scaffold_name)
  @generator_model.specific_model_name = self.model_name if(!self.model_name.blank? and self.model_name != self.scaffold_name)
  self.attributes.values.each do |attribute|
    @generator_model.add_attribute Generators::ModelAttribute.new( "#{attribute.name}:#{attribute.type}" )
  end
  self.associations.values.each do |association|
    model1 = Generators::Model.new(self.model_name)
    entity2 = DSL.entity(association.destination)
    model2 = Generators::Model.new(entity2.model_name)
    assoc = Generators::ModelAssociation.new(model1, model2, association.generator_type, entity2.default_attribute.name)
    assoc.composition = true if association.composition
    assoc.skip_views  = association.skip_views
    @generator_model.associations << assoc
  end
  return @generator_model
end

#member_action(name, method = nil, request_type = nil) ⇒ Object

Include a member action Params: name, method = get, request_type = html



115
116
117
# File 'lib/mdwa/dsl/entity.rb', line 115

def member_action(name, method = nil, request_type = nil)
  self.actions.member_action(name, method, request_type)
end

#resource?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/mdwa/dsl/entity.rb', line 54

def resource?
  self.resource
end

#specify(description) {|specification| ... } ⇒ Object

Entity specifications. Params: description and block

Yields:

  • (specification)


130
131
132
133
134
# File 'lib/mdwa/dsl/entity.rb', line 130

def specify(description)
  specification = EntitySpecification.new(description)
  yield(specification) if block_given?
  self.specifications << specification
end

#user?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mdwa/dsl/entity.rb', line 66

def user?
  self.user
end