Module: Mcfly::Model::ClassMethods
- Defined in:
- lib/marty/mcfly_model.rb
Instance Method Summary collapse
- #base_mcfly_lookup(meth, name, options = {}, &block) ⇒ Object
- #cached_mcfly_lookup(name, options = {}, &block) ⇒ Object
- #gen_mcfly_lookup(name, attrs, options = {}) ⇒ Object
-
#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.
- #hash_if_necessary(q, private) ⇒ Object
- #mcfly_lookup(name, options = {}, &block) ⇒ Object
Instance Method Details
#base_mcfly_lookup(meth, name, options = {}, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/marty/mcfly_model.rb', line 13 def base_mcfly_lookup(meth, name, = {}, &block) priv = [:private] send(meth, name, ) do |ts, *args| raise "time cannot be nil" if ts.nil? ts = Mcfly.normalize_infinity(ts) q = self.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) && [:mode] == :first hash_if_necessary(q, priv) end end |
#cached_mcfly_lookup(name, options = {}, &block) ⇒ Object
35 36 37 |
# File 'lib/marty/mcfly_model.rb', line 35 def cached_mcfly_lookup(name, = {}, &block) base_mcfly_lookup(:cached_delorean_fn, name, , &block) end |
#gen_mcfly_lookup(name, attrs, options = {}) ⇒ Object
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 |
# File 'lib/marty/mcfly_model.rb', line 43 def gen_mcfly_lookup(name, attrs, ={}) raise "bad options #{.keys}" unless (.keys - [:mode, :cache, :private]).empty? mode = .fetch(:mode, :first) # if mode is nil, don't cache -- i.e. don't cache AR queries cache = mode && [:cache] # the older mode=:all is not supported (it's bogus) raise "bad mode #{mode}" unless [nil, :first].member?(mode) assoc = Set.new(self.reflect_on_all_associations.map(&:name)) qstr = attrs.map {|k, v| k = "#{k}_id" if assoc.member?(k) v ? "(#{k} = ? OR #{k} IS NULL)" : "(#{k} = ?)" }.join(" AND ") if Hash === attrs order = attrs.select {|k, v| v}.keys.reverse.map { |k| k = "#{k}_id" if assoc.member?(k) "#{k} NULLS LAST" }.join(", ") attrs = attrs.keys else raise "bad attrs" unless Array === attrs end fn = cache ? :cached_delorean_fn : :delorean_fn base_mcfly_lookup(fn, name, + {sig: attrs.length+1, mode: mode}) do |t, *attr_list| attr_list_ids = attr_list.each_with_index.map {|x, i| assoc.member?(attrs[i]) ? (attr_list[i] && attr_list[i].id) : attr_list[i] } q = self.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
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 |
# File 'lib/marty/mcfly_model.rb', line 112 def gen_mcfly_lookup_cat(name, catrel, attrs, ={}) 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({}) {|(k, v), h| h[k == rel_attr ? cat_attr_id : k] = v } pc_name = "pc_#{name}".to_sym gen_mcfly_lookup(pc_name, pc_attrs, + {private: true}) 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 fn = .fetch(:mode, :first) ? :cached_delorean_fn : :delorean_fn priv = [:private] send(fn, name, sig: attrs.length+1) 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 = self.send(pc_name, ts, *args) hash_if_necessary(q, priv) end end |
#hash_if_necessary(q, private) ⇒ Object
9 10 11 |
# File 'lib/marty/mcfly_model.rb', line 9 def hash_if_necessary(q, private) !private && q.is_a?(ActiveRecord::Base) ? make_openstruct(q) : q end |
#mcfly_lookup(name, options = {}, &block) ⇒ Object
39 40 41 |
# File 'lib/marty/mcfly_model.rb', line 39 def mcfly_lookup(name, = {}, &block) base_mcfly_lookup(:delorean_fn, name, , &block) end |