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.
- #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
89 90 91 |
# File 'lib/ruby_mvc/models/model.rb', line 89 def [](key) @data[key.to_sym] end |
#[]=(key, val) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby_mvc/models/model.rb', line 93 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
111 112 113 114 115 |
# File 'lib/ruby_mvc/models/model.rb', line 111 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
85 86 87 |
# File 'lib/ruby_mvc/models/model.rb', line 85 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.
120 121 122 123 124 125 |
# File 'lib/ruby_mvc/models/model.rb', line 120 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 |
#size ⇒ Object
103 104 105 |
# File 'lib/ruby_mvc/models/model.rb', line 103 def size @data.size end |