Skip to content

Commit ae192a3

Browse files
committed
added type and model_name default behavior
1 parent cf58eed commit ae192a3

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

ruby/hyper-model/lib/reactive_record/active_record/class_methods.rb

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -347,20 +347,11 @@ def server_method(name, default: nil)
347347
end
348348
end
349349

350-
# def define_attribute_methods
351-
# columns_hash.each do |name, column_hash|
352-
# next if name == primary_key
353-
# column_hash[:serialized?] = true if ReactiveRecord::Base.serialized?[self][name]
354-
#
355-
# define_method(name) { @backing_record.get_attr_value(name, nil) } unless method_defined?(name)
356-
# define_method("#{name}!") { @backing_record.get_attr_value(name, true) } unless method_defined?("#{name}!")
357-
# define_method("#{name}=") { |val| @backing_record.set_attr_value(name, val) } unless method_defined?("#{name}=")
358-
# define_method("#{name}_changed?") { @backing_record.changed?(name) } unless method_defined?("#{name}_changed?")
359-
# define_method("#{name}?") { @backing_record.get_attr_value(name, nil).present? } unless method_defined?("#{name}?")
360-
# end
361-
# self.inheritance_column = nil if inheritance_column && !columns_hash.key?(inheritance_column)
362-
# end
363-
350+
# define all the methods for each column. To allow overriding the methods they will NOT
351+
# be defined if already defined (i.e. by the model) See the instance_methods module for how
352+
# super calls are handled in this case. The _hyperstack_internal_setter_... methods
353+
# are used by the load_from_json method when bringing in data from the server, and so therefore
354+
# does not want to be overriden.
364355

365356
def define_attribute_methods
366357
columns_hash.each do |name, column_hash|

ruby/hyper-model/lib/reactive_record/active_record/instance_methods.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
module ActiveRecord
22
module InstanceMethods
33

4+
# if methods are missing, then they must be a column, which we look up
5+
# in the columns_hash.
6+
7+
# For effeciency all attributes will by default have all the methods defined,
8+
# when the class is loaded. See define_attribute_methods class method.
9+
# However a model may override the attribute methods definition, but then call
10+
# super. Which will result in the method missing call.
11+
12+
# When loading data from the server we do NOT want to call overridden methods
13+
# so we also define a _hyperstack_internal_setter_... method for each attribute
14+
# as well as for belongs_to relationships, server_methods, and the special
15+
# type and model_name methods. See the ClassMethods module for details.
16+
417
def method_missing(missing, *args, &block)
518
column = self.class.columns_hash.detect { |name, *| missing =~ /^#{name}/ }
619
if column
@@ -20,6 +33,16 @@ def method_missing(missing, *args, &block)
2033
# ignore load_from_json when it calls _hyperstack_internal_setter_id
2134
def _hyperstack_internal_setter_id(*); end
2235

36+
# the system assumes that there is "virtual" model_name and type attribute so
37+
# we define the internal setter here. If the user defines some other attributes
38+
# or uses these names no harm is done since the exact same method would have been
39+
# defined by the define_attribute_methods class method anyway.
40+
%i[model_name type].each do |attr|
41+
define_method("_hyperstack_internal_setter_#{attr}") do |val|
42+
@backing_record.set_attr_value(:model_name, val)
43+
end
44+
end
45+
2346
def inspect
2447
"<#{model_name}:#{ReactiveRecord::Operations::Base::FORMAT % to_key} "\
2548
"(#{ReactiveRecord::Operations::Base::FORMAT % object_id}) "\

0 commit comments

Comments
 (0)