Class: RubyMVC::Models::Model
- Inherits:
-
Object
- Object
- RubyMVC::Models::Model
- Extended by:
- Toolkit::SignalHandler::ClassMethods
- Includes:
- Toolkit::SignalHandler
- Defined in:
- lib/ruby_mvc/models/model.rb
Overview
This class defines the generic API for models in RubyMVC. Models expose key/value pairs to other parts of the system, but they are more than simple Hash instances because not all properties of a model are always editable.
Direct Known Subclasses
Class Method Summary collapse
-
.adapt(obj, options = {}) ⇒ Object
This method is used to use the Model class to adapt any object that responds to the #keys, #size, #[] and #[]= methods.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
-
#each_label(&block) ⇒ Object
This method is used to iterate over the properties of the model.
-
#initialize(options = {}) ⇒ Model
constructor
The base model is backed by a simple Hash, and supports specifying the following options for defining which properties are editable or not.
-
#is_editable?(key) ⇒ Boolean
This method is used to check whether a property key is editable or not.
-
#keys ⇒ Object
This method is used to provide the property keys of the model as symbols.
-
#label_for(key) ⇒ Object
This method is used to retrieve the label for the specified model key.
-
#labels ⇒ Object
This method is used to retrieve the property key and the display label for the key to be used by views when displaying model data.
-
#link_labels ⇒ Object
This method provides information about inter-model links to provide built-in support for master-detail types of relationships between models.
- #size ⇒ Object
Methods included from Toolkit::SignalHandler::ClassMethods
signal, signals, valid_signal!, valid_signal?
Methods included from Toolkit::SignalHandler
#signal_connect, #signal_disconnect, #signal_emit
Constructor Details
#initialize(options = {}) ⇒ Model
The base model is backed by a simple Hash, and supports specifying the following options for defining which properties are editable or not.
57 58 59 60 |
# File 'lib/ruby_mvc/models/model.rb', line 57 def initialize( = {}) @data = .delete(:data) || {} @options = end |
Class Method Details
.adapt(obj, options = {}) ⇒ Object
This method is used to use the Model class to adapt any object that responds to the #keys, #size, #[] and #[]= methods
48 49 50 51 |
# File 'lib/ruby_mvc/models/model.rb', line 48 def self.adapt(obj, = {}) return obj if obj.is_a? Model self.new(.merge({:data => obj})) end |
Instance Method Details
#[](key) ⇒ Object
97 98 99 |
# File 'lib/ruby_mvc/models/model.rb', line 97 def [](key) @data[key.to_sym] end |
#[]=(key, val) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/ruby_mvc/models/model.rb', line 101 def []=(key, val) k = key.to_sym old = @data[k] @data[k] = val if old != val signal_emit("property-changed", self, k, old, val) end end |
#each_label(&block) ⇒ Object
This method is used to iterate over the properties of the model. For each property, the key, label and value are provide as arguments to the block
119 120 121 122 123 |
# File 'lib/ruby_mvc/models/model.rb', line 119 def each_label(&block) labels.each do |l| block.call((k = l[:key]), l[:label], @data[k]) end end |
#is_editable?(key) ⇒ Boolean
This method is used to check whether a property key is editable or not
93 94 95 |
# File 'lib/ruby_mvc/models/model.rb', line 93 def is_editable?(key) (@options[:editable] || {})[key.to_sym] || true end |
#keys ⇒ Object
This method is used to provide the property keys of the model as symbols.
65 66 67 |
# File 'lib/ruby_mvc/models/model.rb', line 65 def keys @data.keys.sort.collect { |k| k.to_sym } end |
#label_for(key) ⇒ Object
This method is used to retrieve the label for the specified model key.
128 129 130 131 132 133 |
# File 'lib/ruby_mvc/models/model.rb', line 128 def label_for(key) if !@labels labels end @labels[key.to_sym] end |
#labels ⇒ Object
This method is used to retrieve the property key and the display label for the key to be used by views when displaying model data.
73 74 75 76 77 78 79 80 |
# File 'lib/ruby_mvc/models/model.rb', line 73 def labels @labels = {} self.keys.collect do |k| x = { :key => k.to_sym, :label => k.to_s.capitalize } @labels[x[:key]] = x[:label] x end end |
#link_labels ⇒ Object
This method provides information about inter-model links to provide built-in support for master-detail types of relationships between models.
86 87 88 |
# File 'lib/ruby_mvc/models/model.rb', line 86 def link_labels {} end |
#size ⇒ Object
111 112 113 |
# File 'lib/ruby_mvc/models/model.rb', line 111 def size @data.size end |