Class: LucidQueryResult

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus_data/lucid_query_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, result_set: nil) ⇒ LucidQueryResult

Returns a new instance of LucidQueryResult.



17
18
19
20
21
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 17

def initialize(key: nil, result_set: nil, _loading: false)
  @class_name = 'LucidQueryResult'
  @key = key ? key.to_s : self.object_id.to_s
  @result_set = result_set
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(accessor_name, *args, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 42

def method_missing(accessor_name, *args, &block)
  sid_or_array = if @result_set
                   @result_set[accessor_name]
                 else
                   stored_results = Redux.fetch_by_path(:data_state, @class_name, @key)
                   stored_results.JS[accessor_name] if stored_results
                 end
  return nil unless sid_or_array
  if stored_results.JS['_is_array_']
    sid_or_array.map { |sid| Isomorfeus.instance_from_sid(sid) }
  else
    Isomorfeus.instance_from_sid(sid_or_array)
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



2
3
4
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 2

def key
  @key
end

Instance Method Details

#[](accessor_name) ⇒ Object



4
5
6
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 4

def [](accessor_name)
  self.send(accessor_name.to_sym)
end

#_load_from_store!Object



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

def _load_from_store!
  @result_set = nil
end

#included_items_to_transportObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 91

def included_items_to_transport
  data_hash = {}
  @result_set.each_value do |value|
    if value.class == Array
      value.each do |v|
        data_hash.deep_merge!(v.to_transport)
        if v.respond_to?(:included_items_to_transport)
          data_hash.deep_merge!(v.included_items_to_transport)
        end
      end
    else
      unless value.nil?
        data_hash.deep_merge!(value.to_transport)
        if value.respond_to?(:included_items_to_transport)
          data_hash.deep_merge!(value.included_items_to_transport)
        end
      end
    end
  end
  data_hash
end

#key?(k) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 31

def key?(k)
  if @result_set
    @result_set.key?(k)
  else
    stored_results = Redux.fetch_by_path(:data_state, @class_name, @key)
    return false unless stored_results
    `Object.hasOwnProperty(stored_results, k)`
  end
end

#loaded?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 27

def loaded?
  Redux.fetch_by_path(:data_state, @class_name, @key) ? true : false
end

#sidObject



8
9
10
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 8

def sid
  [@class_name, @key]
end

#sid_sObject



12
13
14
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 12

def sid_s
  "[#{@class_name}|#{@key}]"
end

#to_transportObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/isomorfeus_data/lucid_query_result.rb', line 78

def to_transport
  sids_hash = {}
  @result_set.each do |key, value_or_array|
    if value_or_array.class == Array
      sids_hash[key.to_s] = value_or_array.map(&:sid)
      sids_hash[:_is_array_] = true
    else
      sids_hash[key.to_s] = value_or_array.nil? ? nil : value_or_array.sid
    end
  end
  { @class_name => { @key => sids_hash }}
end