Class: SurveyorWarehouse::ResponseRow
- Inherits:
-
Struct
- Object
- Struct
- SurveyorWarehouse::ResponseRow
- Defined in:
- lib/surveyor_warehouse/response_row.rb
Instance Attribute Summary collapse
-
#access_code ⇒ Object
Returns the value of attribute access_code.
-
#id ⇒ Object
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
Instance Attribute Details
#access_code ⇒ Object
Returns the value of attribute access_code
5 6 7 |
# File 'lib/surveyor_warehouse/response_row.rb', line 5 def access_code @access_code end |
#id ⇒ Object
Returns the value of attribute id
5 6 7 |
# File 'lib/surveyor_warehouse/response_row.rb', line 5 def id @id end |
#name ⇒ Object
Returns the value of attribute name
5 6 7 |
# File 'lib/surveyor_warehouse/response_row.rb', line 5 def name @name end |
Instance Method Details
#insert! ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 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 |
# File 'lib/surveyor_warehouse/response_row.rb', line 14 def insert! column_and_values = {} col_and_val = @responses.map do |r| column = r.question.data_export_identifier.split('.')[1] value_field = case r.answer.response_class when 'datetime'; 'datetime_value' when 'date'; 'date_value' when 'time'; 'time_value' when 'float'; 'float_value' when 'integer'; 'integer_value' when 'string'; 'string_value' when 'text'; 'text_value' end value = value_field.present? ? r.send(value_field) : r.answer.try(:reference_identifier) [column.to_sym, value] end mDB = SurveyorWarehouse::DB.connection schema = mDB.schema(name.to_sym).inject({}) { |attrs, (cname, cattrs)| attrs.merge(cname => cattrs) } column_and_values = col_and_val.inject({}) do |accum, (col, val)| if schema[col][:db_type] =~ /\[\]/ accum[col] = Sequel.pg_array((accum[col] || []) << val) else accum[col] = val end accum end column_and_values.merge!('id' => id, 'access_code' => access_code) ds = mDB[name] ds.insert(column_and_values) end |
#responses ⇒ Object
6 7 8 |
# File 'lib/surveyor_warehouse/response_row.rb', line 6 def responses @responses ||= [] end |