Class: ArMemory

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/ar_memory.rb

Overview

Schema information

Table name: ar_memory : Collection name used when form does not belong to database model.

Which is not collection at all. ArMemory model is used for entering data on forms where data will not be saved to database but will instead be saved only to memory as temporary variable and processed by custom made methods. For example, define start and end date when making a timeline report.

Example (as used in forms):

table: ar_memory
title: Enter time period

 form:
   actions:
     1:
       type: ajax
       method: post
       controller: reports
       action: do_some_report
       caption: Run report

   fields:
     10:
       name: date_start
       type: date_picker
       caption: Start date
     20:
       name: date_end
       type: date_picker
       caption: End date

And suppose your report saved data to file named public/report.pdf. Put this line at the end of do_some_report method:

render inline: { :window_report => '/report.pdf' }.to_json, formats: 'js'

As result report.pdf file will be opened in new browser window.

Instance Method Summary collapse

Constructor Details

#initialize(parms = {}) ⇒ ArMemory

Initilize object



70
71
72
73
74
# File 'app/models/ar_memory.rb', line 70

def initialize(parms = {})
  @internals = {}
  parms.each { |key, value| @internals[key.to_s] = value } if parms
  super
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



161
162
163
164
165
166
167
168
169
170
# File 'app/models/ar_memory.rb', line 161

def method_missing(m, *args, &block) #:nodoc:
  @internals ||= {}
  m = m.to_s
  if m.match('=')
    m = m.chomp('=')
    @internals[m] = args.first
  else
    @internals[m]
  end   
end

Instance Method Details

#[](field) ⇒ Object

Redefine [] method to act similar as send method



130
131
132
133
# File 'app/models/ar_memory.rb', line 130

def [](field)
  return nil unless @internals
  @internals[field.to_s]
end

#[]=(field, value) ⇒ Object

Redefine [] method to act similar as send method



138
139
140
141
# File 'app/models/ar_memory.rb', line 138

def []=(field, value)
  @internals ||= {}
  @internals[field.to_s] = value
end

#read_yamlObject

Converts data field to @internals hash



95
96
97
# File 'app/models/ar_memory.rb', line 95

def read_yaml
  @internals = YAML.load(data) || {}
end

#respond_to?(m) ⇒ Boolean

Respond_to should always return true.

Returns:

  • (Boolean)


109
110
111
# File 'app/models/ar_memory.rb', line 109

def respond_to?(m)
  true
end

#save_yamlObject

Convert @internals hash to data field



102
103
104
# File 'app/models/ar_memory.rb', line 102

def save_yaml
  self.data = @internals.to_yaml
end

#send(field, value = nil) ⇒ Object

Redefine send method. Send is used to assign or access value by agile controller.



116
117
118
119
120
121
122
123
124
125
# File 'app/models/ar_memory.rb', line 116

def send(field, value = nil)
  field = field.to_s
  if field.match('=')
    field.chomp!('=')
    @internals ||= {}
    @internals[field] = value
  else
    @internals[field]
  end
end

#to_sObject

For debugging purposes



146
147
148
# File 'app/models/ar_memory.rb', line 146

def to_s
  " ArMemory: @internals=#{@internals.size} #{@internals}"
end