Class: Mxrb::RubyApp::Record
- Inherits:
-
Object
- Object
- Mxrb::RubyApp::Record
- Defined in:
- lib/mxrb/ruby_app.rb
Overview
Base for generated persistent models.
Direct Known Subclasses
Constant Summary collapse
- LIFECYCLE_EVENTS =
Runtime::Native::Store::LIFECYCLE_EVENTS
Class Attribute Summary collapse
-
.attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
.lifecycle_callbacks ⇒ Object
readonly
Returns the value of attribute lifecycle_callbacks.
-
.mendix_id ⇒ Object
readonly
Returns the value of attribute mendix_id.
-
.persistable ⇒ Object
readonly
Returns the value of attribute persistable.
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .attribute(name, type:, mendix_name:, required: false, default: nil) ⇒ Object
- .from_native(value) ⇒ Object
- .inherited(child) ⇒ Object
- .lifecycle(event, method_name = nil, &block) ⇒ Object
- .mendix_name(value = nil, id: nil) ⇒ Object
- .persistence(value) ⇒ Object
Instance Method Summary collapse
-
#initialize(id: nil, **values) ⇒ Record
constructor
A new instance of Record.
- #run_lifecycle_callback(callback) ⇒ Object
- #sync_to_native! ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(id: nil, **values) ⇒ Record
Returns a new instance of Record.
242 243 244 245 |
# File 'lib/mxrb/ruby_app.rb', line 242 def initialize(id: nil, **values) @id = id values.each { public_send("#{_1}=", _2) if respond_to?("#{_1}=") } end |
Class Attribute Details
.attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
191 192 193 |
# File 'lib/mxrb/ruby_app.rb', line 191 def attributes @attributes end |
.lifecycle_callbacks ⇒ Object (readonly)
Returns the value of attribute lifecycle_callbacks.
191 192 193 |
# File 'lib/mxrb/ruby_app.rb', line 191 def lifecycle_callbacks @lifecycle_callbacks end |
.mendix_id ⇒ Object (readonly)
Returns the value of attribute mendix_id.
191 192 193 |
# File 'lib/mxrb/ruby_app.rb', line 191 def mendix_id @mendix_id end |
.persistable ⇒ Object (readonly)
Returns the value of attribute persistable.
191 192 193 |
# File 'lib/mxrb/ruby_app.rb', line 191 def persistable @persistable end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
240 241 242 |
# File 'lib/mxrb/ruby_app.rb', line 240 def id @id end |
Class Method Details
.attribute(name, type:, mendix_name:, required: false, default: nil) ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/mxrb/ruby_app.rb', line 209 def attribute(name, type:, mendix_name:, required: false, default: nil) @attributes ||= [] @attributes << { name: name.to_sym, type: type.to_sym, mendix_name: mendix_name.to_s, required: required == true, default: default } attr_accessor name end |
.from_native(value) ⇒ Object
232 233 234 235 236 237 |
# File 'lib/mxrb/ruby_app.rb', line 232 def from_native(value) values = attributes.to_a.to_h do |attribute| [attribute.fetch(:name), value.members[attribute.fetch(:mendix_name)]] end new(id: value.id, **values).tap { _1.instance_variable_set(:@native_value, value) } end |
.inherited(child) ⇒ Object
193 194 195 196 197 |
# File 'lib/mxrb/ruby_app.rb', line 193 def inherited(child) super child.instance_variable_set(:@attributes, []) child.instance_variable_set(:@lifecycle_callbacks, {}) end |
.lifecycle(event, method_name = nil, &block) ⇒ Object
219 220 221 222 223 224 225 226 |
# File 'lib/mxrb/ruby_app.rb', line 219 def lifecycle(event, method_name = nil, &block) event = event.to_sym raise ArgumentError, "unknown lifecycle event #{event}" unless LIFECYCLE_EVENTS.include?(event) raise ArgumentError, 'callback method or block is required' unless method_name || block @lifecycle_callbacks ||= {} (@lifecycle_callbacks[event] ||= []) << (method_name || block) end |
.mendix_name(value = nil, id: nil) ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/mxrb/ruby_app.rb', line 199 def mendix_name(value = nil, id: nil) return @mendix_name unless value @mendix_name = value.to_s @mendix_id = id.to_s Registry.register(:record, @mendix_name, self) end |
.persistence(value) ⇒ Object
207 |
# File 'lib/mxrb/ruby_app.rb', line 207 def persistence(value) = (@persistable = value == true) |
Instance Method Details
#run_lifecycle_callback(callback) ⇒ Object
261 262 263 264 265 |
# File 'lib/mxrb/ruby_app.rb', line 261 def run_lifecycle_callback(callback) callback.is_a?(Proc) ? callback.call(self) : public_send(callback) ensure sync_to_native! end |
#sync_to_native! ⇒ Object
252 253 254 255 256 257 258 259 |
# File 'lib/mxrb/ruby_app.rb', line 252 def sync_to_native! return self unless @native_value self.class.attributes.to_a.each do |attribute| @native_value.members[attribute.fetch(:mendix_name)] = public_send(attribute.fetch(:name)) end self end |
#to_h ⇒ Object
247 248 249 250 |
# File 'lib/mxrb/ruby_app.rb', line 247 def to_h values = self.class.attributes.to_a.to_h { [_1.fetch(:name), public_send(_1.fetch(:name))] } { id:, type: self.class.mendix_name, attributes: values } end |