Class: Metro::Model::ModelProperty

Inherits:
Property
  • Object
show all
Defined in:
lib/metro/models/properties/model_property.rb

Overview

A model property maintains a reference to another model. This property allows for an existing model to be more easily composed with other models.

The model is stored as a hash which describes the model.

Examples:

Defining a model property with a block


class ScoreBoard < Metro::Model
  property :position
  property :score
  property :label, type: :model do
    create "metro::ui::label", text: "", position: position
  end
end

Defining a model property with a hash


class ScoreBoard < Metro::Model
  property :position
  property :score
  property :label, type: :model, default: { model: "metro::ui::label",
    text: "", position: position }
  end
end

Constant Summary

Constants included from Units

Units::Bounds

Instance Attribute Summary

Attributes inherited from Property

#block, #model, #options

Instance Method Summary collapse

Methods inherited from Property

define_property, defined_properties, get, #get, get_or_set, gets, hash_with_default_to_nil, inherited, #initialize, properties, properties_hash, property, #set, set, sets

Constructor Details

This class inherits a constructor from Metro::Model::Property

Instance Method Details

#create_modelMetro::Model

Return a model created by the current model that owns the property.

Returns:

  • (Metro::Model)

    return a model created by the current model that owns the property.



77
78
79
# File 'lib/metro/models/properties/model_property.rb', line 77

def create_model
  model.create params[:model], params.except(:model)
end

#default_modelObject

Allow for a model property to be defined with a block initialization or a default hash.



67
68
69
70
71
72
73
# File 'lib/metro/models/properties/model_property.rb', line 67

def default_model
  if block
    model.instance_eval(&block)
  else
    create_model options[:default]
  end
end