Module: LucidObject::Mixin

Defined in:
lib/isomorfeus_data/lucid_object/mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/isomorfeus_data/lucid_object/mixin.rb', line 3

def self.included(base)
  base.include(Isomorfeus::Data::FieldSupport)
  base.extend(Isomorfeus::Data::GenericClassApi)
  base.include(Isomorfeus::Data::GenericInstanceApi)
  base.include(LucidI18n::Mixin)

  base.instance_exec do
    def escape_string(s)
      s.gsub(/([\\\&\:\(\)\[\]\{\}\!\"\~\^\|\<\>\=\*\?\+\-\s])/, '\\\\\1')
    end
  end

  def [](name)
    send(name)
  end

  def []=(name, val)
    send("#{name}=".to_sym, val)
  end

  def changed!
    @_changed = true
  end

  def to_transport
    hash = { 'fields' => _get_selected_fields }
    hash['revision'] = revision if revision
    result = { @class_name => { @key => hash }}
    result.deep_merge!(@class_name => { @previous_key => { new_key: @key }}) if @previous_key
    result
  end

  def included_items_to_transport
    f = fields
    data_hash = {}
    self.class.field_types.each do |field, type|
      if type == :object
        v = f[field]
        if v
          if v.is_a?(LucidObject::Mixin)
            data_hash.deep_merge!(v.to_transport)
            data_hash.deep_merge!(v.included_items_to_transport) if v.respond_to?(:included_items_to_transport)
          elsif v.is_a?(Array)
            v.each do |e|
              if e
                data_hash.deep_merge!(e.to_transport)
                data_hash.deep_merge!(e.included_items_to_transport) if e.respond_to?(:included_items_to_transport)
              end
            end
          end
        end
      end
    end
    data_hash
  end

  if RUBY_ENGINE == 'opal'
    def initialize(key: nil, revision: nil, fields: nil, _loading: false, attributes: nil)
      @key = key.nil? ? SecureRandom.uuid : key.to_s
      @class_name = self.class.name
      @class_name = @class_name.split('>::').last if @class_name.start_with?('#<')
      _update_paths
      @_revision = revision ? revision : Redux.fetch_by_path(:data_state, :revision, @class_name, @key)
      @_objects = {}
      @_changed = false
      loaded = loaded?
      fields = attributes if attributes
      if loaded
        raw_fields = Redux.fetch_by_path(*@_store_path)
        if `raw_fields === null`
          if fields
            _validate_fields(fields)
            @_changed_fields = fields
          else
            @_changed_fields = {}
          end
        elsif raw_fields && fields && ::Hash.new(raw_fields) != fields
          _validate_fields(fields)
          @_changed_fields = fields
        else
          @_changed_fields = {}
        end
      else
        fields = {} unless fields
        _validate_fields(fields) unless _loading
        @_changed_fields = fields
      end
    end

    def _load_from_store!
      @_changed_fields = {}
      @_objects = {}
      @_changed = false
    end

    def _update_paths
      @_store_path = [:data_state, @class_name, @key, :fields]
    end

    def each(&block)
      fields.each(&block)
    end
  else # RUBY_ENGINE
    Isomorfeus.add_valid_data_class(base) unless base == LucidObject::Base

    base.instance_exec do
      def _instance_from_transport_sid(sid, included_items_data, _already_loaded)
        o_class_name = sid[0]
        raise "invalid data class #{o_class_name}" unless Isomorfeus.valid_data_class_name?(o_class_name)
        o_class = Isomorfeus.cached_data_class(o_class_name)
        o_data = { o_class_name => { sid[1] => included_items_data[o_class_name][sid[1]] }}
        o_class.instance_from_transport(o_data, included_items_data, _already_loaded)
      end

      def instance_from_transport(instance_data, included_items_data, _already_loaded = {})
        key = instance_data[self.name].keys.first
        ref_s = gen_ref_s(key)
        return _already_loaded[ref_s] if _already_loaded.key?(ref_s)
        revision = instance_data[self.name][key].key?('revision') ? instance_data[self.name][key]['revision'] : nil
        data_obj = new(key: key, revision: revision)
        _already_loaded[ref_s] = data_obj
        fields = instance_data[self.name][key].key?('fields') ? instance_data[self.name][key]['fields'].transform_keys(&:to_sym) : nil
        if fields
          field_types.each do |field, type|
            if type == :object
              sid_or_array = fields[field]
              if sid_or_array&.any?
                if sid_or_array[0].is_a? String
                  fields[field] = _instance_from_transport_sid(sid_or_array, included_items_data, _already_loaded)
                elsif sid_or_array[0].is_a? Array
                  v = sid_or_array.map { |e_sid| _instance_from_transport_sid(e_sid, included_items_data, _already_loaded) }
                  v.compact!
                  fields[field] = v
                end
              end
            end
          end
        end
        data_obj._validate_fields(fields)
        data_obj.fields.merge!(fields)
        data_obj
      end

      def props_from_data(instance_data)
        key = instance_data[self.name].keys.first
        revision = instance_data[self.name][key].key?('revision') ? instance_data[self.name][key]['revision'] : nil
        fields = instance_data[self.name][key].key?('fields') ? instance_data[self.name][key]['fields'].transform_keys!(&:to_sym) : nil
        LucidProps.new({ key: key, revision: revision }.merge!(fields))
      end

      def object_accelerator
        @object_accelerator ||= Isomorfeus::Data::ObjectAccelerator.new(self)
      end

      def each(&block)
        if block_given?
          self.object_accelerator.each do |fields|
            block.call self.new(key: fields.delete(:key), fields: fields)
          end
        else
          Enumerator.new do |yielder|
            self.object_accelerator.each do |fields|
              yielder << self.new(key: fields.delete(:key), fields: fields)
            end
          end
        end
      end
      alias to_enum each

      def search(field = nil, val = nil, options = {}, query: nil)
        if field && !self.field_options[field]&.fetch(:index) == :yes
          raise "Can only search indexed #{self.field_types{field}}s, but #{self.field_types{field}} :#{field} is not indexed!"
        end
        objs = []
        query = query ? query : "#{field}:#{val}"
        self.object_accelerator.search_each(query, options) do |id|
          fields = self.object_accelerator.load_object(id: id)
          objs << self.new(key: fields.delete(:key), fields: fields)
        end
        objs
      end

      execute_create do |_already_saved: {}|
        self.key = SecureRandom.uuid unless self.key
        unless _already_saved.key?(self.ref_s)
          _already_saved[self.ref_s] = self
          self.class.object_accelerator.create_object(self.key, fields, _already_saved)
        end
        self
      end

      execute_destroy do |key:|
        self.object_accelerator.destroy_object(key.to_s)
        true
      end

      execute_load do |key:, _already_loaded: {}|
        ref_s = self.gen_ref_s(@key)
        if _already_loaded.key?(ref_s)
          _already_loaded[ref_s]
        else
          _already_loaded[ref_s] = true
          fields = self.object_accelerator.load_object(key: key.to_s, already_loaded: _already_loaded)
          obj = fields ? self.new(key: fields.delete(:key), fields: fields) : nil
          _already_loaded[ref_s] = obj
        end
      end

      execute_save do |_already_saved: {}|
        self.key = SecureRandom.uuid unless self.key
        unless _already_saved.key?(self.ref_s)
          _already_saved[self.ref_s] = true
          self.class.object_accelerator.save_object(self.key, fields, _already_saved)
        end
        self
      end
    end

    def initialize(key: nil, revision: nil, fields: nil, attributes: nil)
      @key = key.nil? ? SecureRandom.uuid : key.to_s
      @class_name = self.class.name
      @class_name = @class_name.split('>::').last if @class_name.start_with?('#<')
      @_revision = revision
      @_changed = false
      fields = attributes if attributes
      fields = {} unless fields
      _validate_fields(fields) if fields
      @_raw_fields = fields
    end

    def _unchange!
      @_changed = false
    end

    def each(&block)
      @_raw_fields.each(&block)
    end

    def reload
      new_instance = self.class.load(key: @key)
      @_raw_fields = new_instance.fields
      _unchange!
      self
    end
  end # RUBY_ENGINE
end

Instance Method Details

#[](name) ⇒ Object



15
16
17
# File 'lib/isomorfeus_data/lucid_object/mixin.rb', line 15

def [](name)
  send(name)
end

#[]=(name, val) ⇒ Object



19
20
21
# File 'lib/isomorfeus_data/lucid_object/mixin.rb', line 19

def []=(name, val)
  send("#{name}=".to_sym, val)
end

#changed!Object



23
24
25
# File 'lib/isomorfeus_data/lucid_object/mixin.rb', line 23

def changed!
  @_changed = true
end

#included_items_to_transportObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/isomorfeus_data/lucid_object/mixin.rb', line 35

def included_items_to_transport
  f = fields
  data_hash = {}
  self.class.field_types.each do |field, type|
    if type == :object
      v = f[field]
      if v
        if v.is_a?(LucidObject::Mixin)
          data_hash.deep_merge!(v.to_transport)
          data_hash.deep_merge!(v.included_items_to_transport) if v.respond_to?(:included_items_to_transport)
        elsif v.is_a?(Array)
          v.each do |e|
            if e
              data_hash.deep_merge!(e.to_transport)
              data_hash.deep_merge!(e.included_items_to_transport) if e.respond_to?(:included_items_to_transport)
            end
          end
        end
      end
    end
  end
  data_hash
end

#to_transportObject



27
28
29
30
31
32
33
# File 'lib/isomorfeus_data/lucid_object/mixin.rb', line 27

def to_transport
  hash = { 'fields' => _get_selected_fields }
  hash['revision'] = revision if revision
  result = { @class_name => { @key => hash }}
  result.deep_merge!(@class_name => { @previous_key => { new_key: @key }}) if @previous_key
  result
end