Class: ArTemp
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- ArTemp
- Defined in:
- app/models/ar_temp.rb
Overview
Schema information
Table name: ar_temp : Table used for temporary data saving.
ar_temp table has three important fields:
key: String
data: String containing data set saved as yaml
order: String holding value to be osed for sorting selected refords
Class Method Summary collapse
-
.clear(key) ⇒ Object
Remove all documents with specified key from ar_temp table.
-
.prepare(key:, clear: nil) ⇒ Object
Prepare ar_temp for data.
-
.reorder_by(key, *new_order) ⇒ Object
Order data by new key.
Instance Method Summary collapse
-
#[](field) ⇒ Object
Redefine [] method to act similar as send method.
-
#[]=(field, value) ⇒ Object
Redefine [] method to act similar as send method.
-
#initialize(attributes = nil) ⇒ ArTemp
constructor
Initilize object.
-
#method_missing(m, *args, &block) ⇒ Object
Method missing will return value if value defined by m parameter is saved to.
-
#respond_to?(a = nil, b = nil) ⇒ Boolean
Respond_to should always return true.
-
#send(field, value = nil) ⇒ Object
Redefine send method.
-
#to_s ⇒ Object
For debugging.
-
#to_yaml ⇒ Object
Convert internal data to yaml.
Constructor Details
#initialize(attributes = nil) ⇒ ArTemp
Initilize object
47 48 49 50 51 52 53 54 55 |
# File 'app/models/ar_temp.rb', line 47 def initialize(attributes = nil) super() @internals = {} attributes = attributes.nil? ? {} : attributes.stringify_keys self.key = attributes.delete('key') if attributes['key'] self.active = attributes.delete('active') if attributes['active'] self.order = attributes.delete('order') if attributes['order'] attributes.each { |k, v| @internals[k.to_s] = v } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
Method missing will return value if value defined by m parameter is saved to
111 112 113 114 115 116 117 |
# File 'app/models/ar_temp.rb', line 111 def method_missing(m, *args, &block) #:nodoc: m = m.to_s return @internals[m] unless m.match('=') m.chomp!('=') @internals[m] = args.first end |
Class Method Details
.clear(key) ⇒ Object
Remove all documents with specified key from ar_temp table
122 123 124 |
# File 'app/models/ar_temp.rb', line 122 def self.clear(key) where(key: key).delete_all end |
.prepare(key:, clear: nil) ⇒ Object
Prepare ar_temp for data. It first checks if data associated with the key is to be deleted and then yields block code.
Returns: Query for the data associated with the key
132 133 134 135 136 137 138 |
# File 'app/models/ar_temp.rb', line 132 def self.prepare(key:, clear: nil) unless %w(no false 0).include?(clear.to_s.strip.downcase) clear(key) yield end where(key: key) end |
.reorder_by(key, *new_order) ⇒ Object
Order data by new key. Will update order field with values from new field
143 144 145 146 147 148 |
# File 'app/models/ar_temp.rb', line 143 def self.reorder_by(key, *new_order) where(key: key).each do |record| record.order = new_order.map(&:to_s).join('') record.save end end |
Instance Method Details
#[](field) ⇒ Object
Redefine [] method to act similar as send method
82 83 84 |
# File 'app/models/ar_temp.rb', line 82 def [](field) @internals[field.to_s] end |
#[]=(field, value) ⇒ Object
Redefine [] method to act similar as send method
89 90 91 |
# File 'app/models/ar_temp.rb', line 89 def []=(field, value) @internals[field.to_s] = value end |
#respond_to?(a = nil, b = nil) ⇒ Boolean
Respond_to should always return true.
60 61 62 |
# File 'app/models/ar_temp.rb', line 60 def respond_to?(a = nil, b = nil) true end |
#send(field, value = nil) ⇒ Object
Redefine send method. Send is used to assign or access value
67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/ar_temp.rb', line 67 def send(field, value = nil) return super(field) if field.is_a? Symbol field = field.to_s if field.match('=') field.chomp!('=') @internals[field] = value else @internals[field] end end |
#to_s ⇒ Object
For debugging
103 104 105 |
# File 'app/models/ar_temp.rb', line 103 def to_s "ArTemp: @key=#{key} @data=#{data}" end |
#to_yaml ⇒ Object
Convert internal data to yaml
96 97 98 |
# File 'app/models/ar_temp.rb', line 96 def to_yaml @internals.to_yaml end |