How to declare array inside array in yaml file - snakeyaml

for example : rate:
{floor: '1', functionId: BDEB1,
baseRates: { baseRateAgreementLevel: baseRateAgreementLevel,name: LIBOR GBP 1 Month, value: 0.1}
}
Above rate is a array and baserate is another array which is inside rate array
Getting error "Cannot create property" while declaring array inside array in snake yaml file.

I'm guessing you meant to write:
rate:
floor: '1'
functionId: BDEB1
baseRates:
baseRateAgreementLevel: baseRateAgreementLevel
name: LIBOR GBP 1 Month
value: 0.1
Is this what you wanted? Take a look here for a json equivalent representation.

Related

How to split an array of objects into subarrays depending on the field value in Rails and Mongodb

I want to get several arrays of objects aggregated by months (and years) in a their property value.
I have class Request like this:
class Request
include Mongoid::Document
include MongoidDocument::Updated
field :name, type: String
field :start_date, type: DateTime
#...
end
And I want the resulting array of multiple hashes with
{month: m_value, year: y_value, request: requests_with_m_value_as_month_and_y_value_as_year_in_start_date_field}
as element of array
Can someone help me with this?
You can use Aggregation Pipeline to get the data in the right shape back from MongoDB:
db.requests.aggregate([
{
$group: {
_id: {
year: {
$year: "$start_date"
},
month: {
$month: "$start_date"
}
},
requests: {
$push: "$$ROOT"
}
}
},
{
$project: {
_id: 0,
year: "$_id.year",
month: "$_id.month",
requests: "$requests"
}
}
])
Obviously, this is using just the REPL and you will have to translate it to the DSL provided by Mongoid. Based on what I could find it should be possible to just get the underlying collection and call aggregate on it:
Request.collection.aggregate([...])
Now you just need to take the query and convert it into something that Mongoid will accept. I think you just need to add a bunch of quotes around the object keys but I don't the environment set up to try that myself.

Create model instances from array

I have a 2D array of data and need to create new Measurement model instances. What is best way to do that?
Simplified array:
=> [{"Date"=>"02/03/2017 11:46:11",
"Location"=>"Sissach",
"Serial Number"=>460631,
"Air Humiditiy (%)"=>27.5,
"Air Temperature (°C)"=>17.4},
{"Date"=>"02/03/2017 11:46:21",
"Location"=>"Sissach",
"Serial Number"=>460632,
"Air Humiditiy (%)"=>27.2,
"Air Temperature (°C)"=>17.7}}]
Any gem for auto convert data to database type of data ? Thanks for your time.
Considering your model Measurement has the next structure:
Measurement(
id: integer,
date: datetime,
location: string,
serial_number: integer,
humidity: float,
temperature: float,
created_at: datetime,
updated_at: datetime
)
You can easily iterate over your array of hashes and on each element, create a new Measurement record, like:
[
{ "date"=>"02/03/2017 11:46:11", "location"=>"Sissach", "serial_number"=>460631, "humidity"=>27.5, "temperature"=>17.4 },
{ "date"=>"02/03/2017 11:46:21", "location"=>"Sissach", "serial_number"=>460632, "humidity"=>27.2, "temperature"=>17.7 }
].each do |measurement|
Measurement.create(
location: measurement['location'],
serial_number: measurement['serial_number'],
date: measurement['date'],
humidity: measurement['humidity'],
temperature: measurement['temperature']
)
end
Or as #7urkm3n pointed out, just by passing the array of hashes it would work the same way.
Consider the block in case you need to do any extra operation, or if you want to initialize first a model object and then check if the record trying to be saved has success.

Swift: Sort Array of Object Property While Keeping Order Intact

as the long name of this question suggests, I am trying to sort an array of objects by those object's properties, but I am having issues with the order in which the array is returned. Basically, my database structure has each date, in order, and then entries under ascending numbers under that date's directory. Below is a visual representation of the structure:
Balances
-11/7/17
-0
-name: "Name 1 Here"
-1
-name: "Name 2 Here"
-2
-name: "Name 3 Here"
-11/8/17
-0
-name: "Name 1 Here"
-1
-name: "Name 2 Here"
-2
-name: "Name 3 Here"
-11/9/17
-0
-name: "Name 1 Here"
-1
-name: "Name 2 Here"
-2
-name: "Name 3 Here"
When I retrieve the data from the database through a SingleValueEvent and add it to an array of custom objects, the data in the array for some reason gets out of order, and doesn't stay in the exact order as the data is stored in the database. To solve this problem, I tried to use the following code to order the array:
self.tableArray = self.tableArray.sorted(by: { $0.date < $1.date })
self.tableArray.sort(by: { (object1, object2) -> Bool in
if object1.date != object2.date {
return object1.date < object2.date
} else {
return object1.date < object2.date
}
})
The array returned after using either of the two lines of code is sorted by each object's date, but the order of each of the items under the date in the database is screwed up. I know this is kind of confusing, which is why I have included a visual representation below which I hope will help.
Original Array:
Item 1:
date: 11/14/17
name: Name 1
Item 2:
date: 11/13/17
name: Name 1
Item 3:
date: 11/13/17
name: Name 2
Item 4:
date: 11/7/17
name: Name 1
Sorted Array:
Item 1:
date: 11/7/17
name: Name 1
Item 2:
date: 11/13/17
name: Name 2
Item 3:
date: 11/13/17
name: Name 1
Item 4:
date: 11/14/17
name: Name 1
Thanks!
There could be a few reasons, the way it is serialised or sorted in storage - without specific insight into if you're using CoreData, Real, SQLite or other...
TL; DR, if you want to ensure the order is exactly as per what you want, and it looks like you have multiple sorting criteria, define that criteria as an attribute. You cannot always guarantee persistence will maintain the ordering, so build it into the schema.
I.e.
{name, date}
Add an extra attribute
{name, date, order}.
The order property can be globally unique or just unique within items of that date.
You can either sort twice (with the second sort being stable), or better yet define a comparison that first compares by date, and then by the other.
self.tableArray.sort(by: { (object1, object2) -> Bool in
if object1.date != object2.date {
return object1.date < object2.date
} else {
return object1.order < object2.order
}
})

How to declare an object with un-named primitives in swagger?

I need to represent the following array in swagger-2.0, but am unsure how to do it, since I cannot figure out how to declare 'un-named' properties.
For example, here's what I need to define:
coords: [
[
37.782984,
-122.420973
],
[
37.772309,
-122.418555
],
...
]
How can I define these array-entries in swagger ?
If you model it as an array of arrays with two items, it should look like this:
parameters:
- name: coords
in: body
schema:
type: array
items:
type: array
items:
type: number
maxItems: 2
minItems: 2

How to add values in nested hash in ruby's way

Here is a nested hash. We want to add all the value of "subtotal" together in ruby's way. Please be noted that the key of "0" and "1342119042142" could be any other unknown strings (number of keys is at least one. Could be more than one) when performing the addition.
{"0"=>{"lease_item_id"=>"3",
"subtotal"=>"100"},
"1342119042142"=>{"lease_item_id"=>"1",
"subtotal"=>"100",
"_destroy"=>"false"}}}
Thanks.
Like this:
set up hash:
s = {"0"=>{"lease_item_id"=>"3", "subtotal"=>"100"},
"1342119042142"=>{"lease_item_id"=>"1", "subtotal"=>"100","_destroy"=>"false"}}
calculate total:
total = s.inject(0) { |i, j| i + j.last["subtotal"].to_i}
Explanation: Look here for documentation. Basically inject is given an initial value (in the above code it is 0) and it passes the given value to the given block, where it gets set to what is returned from the block in each iteration. So in the above code, initially it is 0, on the first iteration it is set to 0 + 100 and now is equal to 100, and on the second [and final] iteration it is set to 100 + 100, 200.
Assuming h is your hash and the subtotal can be decimal value:
h.values.sum{|x| x['subtotal'].to_f}
hash = {"0"=>{"lease_item_id"=>"3", "subtotal"=>"100"}, "1342119042142"=>{"lease_item_id"=>"1", "subtotal"=>"100", "_destroy"=>"false"}}
sum = hash.values.reduce(0){|sum,inner| sum + inner["subtotal"].to_i }

Resources