Module: Mcfly::Model::ClassMethods

Defined in:
lib/marty/mcfly_model.rb

Instance Method Summary collapse

Instance Method Details

#base_mcfly_lookup(name, options = {}, &block) ⇒ Object



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
# File 'lib/marty/mcfly_model.rb', line 27

def base_mcfly_lookup(name, options = {}, &block)
  delorean_options = {
    # private: options.fetch(:private, false),
    cache: options.fetch(:cache, false),
    sig: options[:sig]
  }

  delorean_fn name, delorean_options do |ts, *args|
    raise 'time cannot be nil' if ts.nil?

    # FIXME: sig is removed from delorean. We need to find a better way
    # to control amount of arguments, instead of using *splat arguments.
    max_args = Array(options[:sig]).max if options[:sig]
    if max_args && (args.size + 1) > max_args
      err = "Too many args to #{name}." \
        "(given #{args.size + 1}, expected #{max_args})"

      raise ArgumentError, err
    end

    ts = Mcfly.normalize_infinity(ts)
    q = where("#{table_name}.obsoleted_dt >= ? AND " +
               "#{table_name}.created_dt < ?", ts, ts).scoping do
      block.call(ts, *args)
    end

    fa = get_final_attrs
    q = q.select(*fa) if fa.present? && q.is_a?(ActiveRecord::Relation)

    q = q.first if q.respond_to?(:first) && options[:mode] == :first

    if options[:to_hash]
      next hash_if_necessary(
        q,
        options.fetch(:to_hash, false)
      )
    end

    openstruct_if_necessary(q, options[:private])
  end
end

#cached_mcfly_lookup(name, options = {}, &block) ⇒ Object



69
70
71
# File 'lib/marty/mcfly_model.rb', line 69

def cached_mcfly_lookup(name, options = {}, &block)
  base_mcfly_lookup(name, options.merge(cache: true), &block)
end

#gen_mcfly_lookup(name, attrs, options = {}) ⇒ Object



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
# File 'lib/marty/mcfly_model.rb', line 77

def gen_mcfly_lookup(name, attrs, options = {})
  raise "bad options #{options.keys}" unless
  (options.keys - [:mode, :cache, :private, :to_hash]).empty?

  mode = options.fetch(:mode, :first)

  # if mode is nil, don't cache -- i.e. don't cache AR queries
  cache = mode && options[:cache]

  # the older mode=:all is not supported (it's bogus)
  raise "bad mode #{mode}" unless [nil, :first].member?(mode)

  assoc = Set.new(reflect_on_all_associations.map(&:name))

  qstr = attrs.map do |k, v|
    k = "#{k}_id" if assoc.member?(k)

    v ? "(#{k} = ? OR #{k} IS NULL)" : "(#{k} = ?)"
  end.join(' AND ')

  if Hash === attrs
    order = attrs.select { |_k, v| v }.keys.reverse.map do |k|
      k = "#{k}_id" if assoc.member?(k)

      "#{k} NULLS LAST"
    end.join(', ')
    attrs = attrs.keys
  else
    raise 'bad attrs' unless Array === attrs
  end

  base_mcfly_lookup(name, options + { sig:  attrs.length + 1,
                                         mode: mode }) do |_t, *attr_list|
    attr_list_ids = attr_list.each_with_index.map do |_x, i|
      assoc.member?(attrs[i]) ?
        (attr_list[i] && attr_list[i].id) : attr_list[i]
    end

    q = where(qstr, *attr_list_ids)
    q = q.order(order) if order
    q
  end
end

#gen_mcfly_lookup_cat(name, catrel, attrs, options = {}) ⇒ Object

rel_attr = :security_instrument cat_assoc_klass = Gemini::SecurityInstrumentCategorization cat_attr = :g_fee_category name = :lookup_q pc_name = :pc_lookup_q pc_attrs = true, security_instrument: true, coupon: true



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
# File 'lib/marty/mcfly_model.rb', line 143

def gen_mcfly_lookup_cat(name, catrel, attrs, options = {})
  rel_attr, cat_assoc_name, cat_attr = catrel

  raise "#{rel_attr} should be mapped in attrs" if attrs[rel_attr].nil?

  cat_assoc_klass = cat_assoc_name.constantize
  cat_attr_id = "#{cat_attr}_id"

  # replace rel_attr with cat_attr in attrs
  pc_attrs = attrs.each_with_object({}) do |(k, v), h|
    h[k == rel_attr ? cat_attr_id : k] = v
  end

  pc_name = "pc_#{name}".to_sym

  gen_mcfly_lookup(pc_name, pc_attrs, options + { private: true, to_hash: false })

  lpi = attrs.keys.index rel_attr

  raise "should not include #{cat_attr}" if attrs.member?(cat_attr)
  raise "need #{rel_attr} argument" unless lpi

  # cache if mode is not nil
  to_hash = options.fetch(:to_hash, false)

  # cache if mode is not explicitly set to nil or cache is true
  cache = options.fetch(:cache) { options.fetch(:mode, :first) }

  delorean_options = {
    # private: options.fetch(:private, false),
    cache: cache.present?, # convert to bool
    sig: attrs.length + 1
  }

  delorean_fn name, delorean_options do |ts, *args|
    # Example: rel is a Gemini::SecurityInstrument instance.
    rel = args[lpi]
    raise "#{rel_attr} can't be nil" unless rel

    args[lpi] = cat_assoc_klass.
                  mcfly_pt(ts).
                  select(cat_attr_id).
                  find_by(rel_attr => rel).
                  send(cat_attr_id)

    q = send(pc_name, ts, *args)

    if to_hash
      next hash_if_necessary(
        q,
        options.fetch(:to_hash, false)
      )
    end

    openstruct_if_necessary(q, options[:private])
  end
end

#hash_if_necessary(q, to_hash) ⇒ Object



21
22
23
24
25
# File 'lib/marty/mcfly_model.rb', line 21

def hash_if_necessary(q, to_hash)
  return make_hash(q) if to_hash && q.is_a?(ActiveRecord::Base)

  q
end

#mcfly_lookup(name, options = {}, &block) ⇒ Object



73
74
75
# File 'lib/marty/mcfly_model.rb', line 73

def mcfly_lookup(name, options = {}, &block)
  base_mcfly_lookup(name, options, &block)
end

#openstruct_if_necessary(q, private_lookup) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/marty/mcfly_model.rb', line 9

def openstruct_if_necessary(q, private_lookup)
  return q if private_lookup
  return q unless q.is_a?(ActiveRecord::Base)

  warning = 'Mcfly::Model#openstruct_if_necessary is deprecated.' \
    "Please use 'to_hash: true' or 'private: true' option instead"

  Rails.logger.warn warning

  make_openstruct(q)
end