Class: Metro::Model

Inherits:
Object
  • Object
show all
Includes:
HasEvents, KeyValueCoding, PropertyOwner, Units
Defined in:
lib/metro/models/model.rb,
lib/metro/models/properties/property.rb,
lib/metro/models/properties/font_property.rb,
lib/metro/models/properties/song_property.rb,
lib/metro/models/properties/text_property.rb,
lib/metro/models/properties/array_property.rb,
lib/metro/models/properties/color_property.rb,
lib/metro/models/properties/image_property.rb,
lib/metro/models/properties/model_property.rb,
lib/metro/models/properties/scale_property.rb,
lib/metro/models/properties/sample_property.rb,
lib/metro/models/properties/boolean_property.rb,
lib/metro/models/properties/numeric_property.rb,
lib/metro/models/properties/position_property.rb,
lib/metro/models/properties/animation_property.rb,
lib/metro/models/properties/dimensions_property.rb,
lib/metro/models/properties/options_property/options.rb,
lib/metro/models/properties/options_property/no_option.rb,
lib/metro/models/properties/options_property/options_property.rb

Overview

The Model is a basic, generic representation of a game object that has a visual representation within the scene’s window.

Model is designed to be an abstract class, to be subclassed by other models.

See Also:

  • Models::Generic

Defined Under Namespace

Classes: AnimationProperty, ArrayProperty, BooleanProperty, ColorProperty, DimensionsProperty, FontProperty, ImageProperty, ModelProperty, NumericProperty, OptionsProperty, PositionProperty, Property, SampleProperty, ScaleProperty, SongProperty, TextProperty

Constant Summary

Constants included from Units

Units::Bounds

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasEvents

included

Methods included from KeyValueCoding

#get, #set

Methods included from PropertyOwner

included, #properties

Constructor Details

#initialize(options = {}) ⇒ Model

Note:

Overridding initialize method should be avoided, using the {#aftter_initialize)

Create an instance of a model.

method or done with care to ensure that functionality is preserved.



169
170
171
172
# File 'lib/metro/models/model.rb', line 169

def initialize(options = {})
  _load(options)
  after_initialize
end

Instance Attribute Details

#sceneObject

The scene that this model is currently being displayed.

The current value of scene is managed by the scene as this is set when the scene is created.

See Also:



120
121
122
# File 'lib/metro/models/model.rb', line 120

def scene
  @scene
end

#windowObject

The window that this model that this window is currently being displayed.

The current value of window is managed by the scene as this is set when the Scene is added to the window. All the models gain access to the window.

See Also:



111
112
113
# File 'lib/metro/models/model.rb', line 111

def window
  @window
end

Class Method Details

.hierarchyObject

Returns an array of all ancestor models by name.

Returns:

  • an array of all ancestor models by name



223
224
225
# File 'lib/metro/models/model.rb', line 223

def self.hierarchy
  ancestors.find_all {|a| a.respond_to? :metro_name }.map(&:metro_name)
end

.inherited(base) ⇒ Object

Captures all classes that subclass Model.



230
231
232
233
# File 'lib/metro/models/model.rb', line 230

def self.inherited(base)
  models << base.to_s
  Models.add(base)
end

.metro_nameObject

Returns a common name that can be used through the system as a common identifier.

Returns:

  • a common name that can be used through the system as a common identifier.



216
217
218
# File 'lib/metro/models/model.rb', line 216

def self.metro_name
  name.underscore
end

.model_name(model_name = nil) ⇒ Object



78
79
80
81
# File 'lib/metro/models/model.rb', line 78

def self.model_name(model_name=nil)
  @model_name ||= to_s.underscore
  model_name ? @model_name = model_name.to_s : @model_name
end

.modelsObject

All subclasses of Model, this should be all the defined



238
239
240
# File 'lib/metro/models/model.rb', line 238

def self.models
  @models ||= []
end

Instance Method Details

#_load(options = {}) ⇒ Object

Loads a hash of content into the model. This process will convert the hash of content into setter and getter methods with appropriate ruby style names.

This is used internally when the model is created for the Scene. It is loaded with the contents of the view.



181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/metro/models/model.rb', line 181

def _load(options = {})
  # Clean up and symbolize all the keys then merge that with the existing properties
  options.keys.each do |key|
    property_name = key.to_s.underscore.to_sym
    if respond_to? "#{property_name}="
      send("#{property_name}=",options.delete(key))
    else
      options[property_name] = options.delete(key)
    end
  end

  properties.merge! options
end

#_saveObject

Generate a hash export of all the fields that were previously stored within the model.

This is used internally within the scene to transfer the data from one model to another model.



202
203
204
# File 'lib/metro/models/model.rb', line 202

def _save
  properties
end

#after_initializeObject

Note:

This method should be implemented in the Model subclass.

This is an entry point for customization. As the model’s #initialize method performs may perform some initialization that may be necessary.

At this point the model has been created. However, the window and scene of the model will not have been defined and defined properties rely on the window or scene will return nil values. Other properties also will likely not be set.



31
# File 'lib/metro/models/model.rb', line 31

def after_initialize ; end

#boundsObject

By default a model has no bounds. Each subclass of model will have to define how their bounds are defined.



159
160
161
# File 'lib/metro/models/model.rb', line 159

def bounds
  Bounds.none
end

#create(model_name, options = {}) ⇒ Metro::Model

A helper method that allows the current model to generate another model. This is useful as it allows for the current model to pass window and scene state to the created model.

Parameters:

  • model_name (String)

    the name of the model to be created.

Returns:



146
147
148
149
150
151
152
153
# File 'lib/metro/models/model.rb', line 146

def create(model_name,options={})
  # @TODO: this is another path that parallels the ModelFactory
  model_class = Metro::Models.find(model_name)
  mc = model_class.new options
  mc.scene = scene
  mc.window = window
  mc
end

#drawObject

Note:

This method should be implemented in the Model subclass.

This is called after every #update and when the OS wants the window to repaint itself.



66
# File 'lib/metro/models/model.rb', line 66

def draw ; end

#draw_completed?Boolean

Note:

This method should be implemented in the Model sublclass if you are interested in having the model be removed from the scene.

This is called after a draw. A model normally is not removed after a draw, however if the model responds true to #draw_completed? then it will be removed.

Returns:

  • (Boolean)


76
# File 'lib/metro/models/model.rb', line 76

def draw_completed? ; false ; end

#modelString

Returns the name of the model class.

Returns:

  • (String)

    the name of the model class.



86
# File 'lib/metro/models/model.rb', line 86

property :model, type: :text

#nameString

Returns the name of model as it is used within the view or the scene. This is the common name, the key within the view file, or the name symbol name specified in the scene.

Returns:

  • (String)

    the name of model as it is used within the view or the scene. This is the common name, the key within the view file, or the name symbol name specified in the scene.



93
# File 'lib/metro/models/model.rb', line 93

property :name, type: :text

#notification(event) ⇒ Object

Generate a custom notification event with the given name.

Parameters:

  • event (Symbol)

    the name of the notification to generate.



129
130
131
# File 'lib/metro/models/model.rb', line 129

def notification(event)
  scene.notification(event.to_sym,self)
end

#saveable_to_viewTrueClass, FalseClass

Returns true if the model should be saved to the view file, false when the model should not be savedable to the view file.

Returns:

  • (TrueClass, FalseClass)

    true if the model should be saved to the view file, false when the model should not be savedable to the view file.



99
# File 'lib/metro/models/model.rb', line 99

property :saveable_to_view, type: :boolean, default: true

#showObject

Note:

This method may be implemented in the Model subclass.

This is an entry point for customization. After the model’s properties have been set and the model has been assigned to the window and scene this method is called. Here is where customization of properties or final positioning can be performed.



41
# File 'lib/metro/models/model.rb', line 41

def show ; end

#to_hashObject

Generate a hash representation of the model.



209
210
211
# File 'lib/metro/models/model.rb', line 209

def to_hash
  { name => properties.except(:name) }
end

#updateObject

Note:

This method should be implemented in the Model subclass

This is called every update interval while the actor is in the scene



48
# File 'lib/metro/models/model.rb', line 48

def update ; end

#update_completed?Boolean

Note:

This method should be implemented in the Model sublclass if you are interested in having the model be removed from the scene.

This is called after an update. A model normally is not removed after an update, however if the model responds true to #update_completed? then it will be removed.

Returns:

  • (Boolean)


58
# File 'lib/metro/models/model.rb', line 58

def update_completed? ; false ; end