Class: FuryDumper::Dumpers::Model
- Inherits:
-
Object
- Object
- FuryDumper::Dumpers::Model
- Defined in:
- lib/fury_dumper/dumpers/model.rb
Instance Attribute Summary collapse
-
#iteration ⇒ Object
readonly
Returns the value of attribute iteration.
-
#relation_items ⇒ Object
Returns the value of attribute relation_items.
-
#root_model ⇒ Object
Returns the value of attribute root_model.
-
#source_model ⇒ Object
readonly
Returns the value of attribute source_model.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #active_record_model ⇒ Object
- #all_non_scoped_models ⇒ Object
- #column_names ⇒ Object
- #copy ⇒ Object
- #fetch_complex_items ⇒ Object
- #fetch_equality_items_hash ⇒ Object
-
#initialize(source_model:, relation_items:, iteration: 0, root_model: nil) ⇒ Model
constructor
A new instance of Model.
- #next_iteration ⇒ Object
- #root_path ⇒ Object
- #sub_path?(other_model) ⇒ Boolean
- #to_active_record_relation ⇒ Object
- #to_full_str ⇒ Object
- #to_short_str ⇒ Object
Constructor Details
#initialize(source_model:, relation_items:, iteration: 0, root_model: nil) ⇒ Model
Returns a new instance of Model.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/fury_dumper/dumpers/model.rb', line 9 def initialize(source_model:, relation_items:, iteration: 0, root_model: nil) raise ArgumentError unless source_model.is_a?(String) raise ArgumentError unless relation_items.is_a?(RelationItems) raise ArgumentError unless iteration.is_a?(Numeric) raise ArgumentError unless root_model.is_a?(Model) || root_model.nil? @source_model = source_model @relation_items = relation_items @iteration = iteration @root_model = root_model @warnings = [] end |
Instance Attribute Details
#iteration ⇒ Object (readonly)
Returns the value of attribute iteration.
6 7 8 |
# File 'lib/fury_dumper/dumpers/model.rb', line 6 def iteration @iteration end |
#relation_items ⇒ Object
Returns the value of attribute relation_items.
7 8 9 |
# File 'lib/fury_dumper/dumpers/model.rb', line 7 def relation_items @relation_items end |
#root_model ⇒ Object
Returns the value of attribute root_model.
7 8 9 |
# File 'lib/fury_dumper/dumpers/model.rb', line 7 def root_model @root_model end |
#source_model ⇒ Object (readonly)
Returns the value of attribute source_model.
6 7 8 |
# File 'lib/fury_dumper/dumpers/model.rb', line 6 def source_model @source_model end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
6 7 8 |
# File 'lib/fury_dumper/dumpers/model.rb', line 6 def warnings @warnings end |
Instance Method Details
#==(other) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/fury_dumper/dumpers/model.rb', line 35 def ==(other) raise ArgumentError unless other.is_a?(Model) @source_model == other.source_model && @relation_items.eql?(other.relation_items) && sub_path?(other) end |
#active_record_model ⇒ Object
72 73 74 |
# File 'lib/fury_dumper/dumpers/model.rb', line 72 def active_record_model @source_model.constantize end |
#all_non_scoped_models ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/fury_dumper/dumpers/model.rb', line 100 def all_non_scoped_models active_record_model.reflect_on_all_associations.select { |rr| rr.scope.nil? }.map do |rr| rr.klass.to_s rescue NameError, LoadError => e @warnings << e next end.compact.uniq end |
#column_names ⇒ Object
31 32 33 |
# File 'lib/fury_dumper/dumpers/model.rb', line 31 def column_names active_record_model.columns.map(&:name) end |
#copy ⇒ Object
22 23 24 25 26 27 |
# File 'lib/fury_dumper/dumpers/model.rb', line 22 def copy self.class.new(source_model: source_model, relation_items: relation_items.copy, iteration: iteration, root_model: root_model) end |
#fetch_complex_items ⇒ Object
82 83 84 |
# File 'lib/fury_dumper/dumpers/model.rb', line 82 def fetch_complex_items @relation_items.complex_items.map(&:key).join(' AND ') end |
#fetch_equality_items_hash ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/fury_dumper/dumpers/model.rb', line 86 def fetch_equality_items_hash values = {} @relation_items.equality_items.each do |item| if active_record_model.column_names.include?(item.key) values[item.key] = item.values_for_key else @warnings << "Shit relation: #{@source_model}.#{item.key} does not exist" next end end values end |
#next_iteration ⇒ Object
126 127 128 |
# File 'lib/fury_dumper/dumpers/model.rb', line 126 def next_iteration @iteration + 1 end |
#root_path ⇒ Object
50 51 52 53 54 |
# File 'lib/fury_dumper/dumpers/model.rb', line 50 def root_path return [nil] if @root_model.nil? @root_model.root_path + [@root_model.source_model] end |
#sub_path?(other_model) ⇒ Boolean
43 44 45 46 47 48 |
# File 'lib/fury_dumper/dumpers/model.rb', line 43 def sub_path?(other_model) raise ArgumentError unless other_model.is_a?(Model) min_length = [root_path.length, other_model.root_path.length].min - 1 root_path[0..min_length] == other_model.root_path[0..min_length] end |
#to_active_record_relation ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/fury_dumper/dumpers/model.rb', line 109 def to_active_record_relation return @active_record_relation if @active_record_relation complex_values = fetch_complex_items equality_values = fetch_equality_items_hash @active_record_relation = active_record_model.where(equality_values) .where(complex_values) .limit(FuryDumper::Config.limit) unless FuryDumper::Config.fast? order_value = { primary_key => :desc } @active_record_relation = @active_record_relation.order(order_value) end @active_record_relation end |
#to_full_str ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fury_dumper/dumpers/model.rb', line 56 def to_full_str buffer = "MODEL #{@source_model} by #{root_model&.source_model.presence || '-'} WHERE " buffer += @relation_items.items.map do |item| if item.complex item.key elsif item.values_for_key.count > 10 "#{item.key} = #{item.values_for_key[0..10]} and #{item.values_for_key.count - 10} elements" else "#{item.key} = #{item.values_for_key}" end end.join(' AND ') buffer end |
#to_short_str ⇒ Object
78 79 80 |
# File 'lib/fury_dumper/dumpers/model.rb', line 78 def to_short_str "#{@source_model}.#{@relation_items.keys.join(' & ')}" end |