Posts
Getting Started With Gradle
To use gradle, download the binaries and add the bin directory from the extracted directory to the environment PATH. Then, create the directory structure below:
settings.gradle app build.gradle src main java com pockettheories Main.java The settings.gradle file contains the name of the project and the include directive has the name of the directory containing build.gradle:
rootProject.name = 'gradled' include('app') Within the sub-project directory (app), the build.gradle file is the equivalent of Maven’s pom.
Posts
Java Driver for MongoDB
MongoDB provides two official drivers for Java - the sync driver and the reactive streams driver. The sync driver is used for most purposes.
Not every MongoDB driver version has been tested with every MongoDB server version. The compatibility matrix for the drivers, the server version, and the Java runtime version is at: https://docs.mongodb.com/drivers/java/sync/current/compatibility/
Next, if you are using Maven, add the driver dependency to the pom.xml, as follows:
<dependency> <groupId>org.
Posts
Spring Data Mongodb
Spring Data for MongoDB is an Object-Document Mapper library. To use Spring Data for MongoDB, add the dependencies for Spring Data and the MongoDB Driver.
<dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>4.4.1</version> </dependency> </dependencies> Define the POJO class for the data model:
package com.pockettheories; public class Person { public String id; public String firstName; public String lastName; public Person(String firstName, String lastName) {this.firstName=firstName; this.lastName=lastName;} } Then, in the Main class, use the MongoTemplate class, which implements the interface MongoOperations.
Posts
Spring Boot Setup
To create a Spring Boot web API application, start by creating the directory structure for the Maven project.
/pom.xml /src/main/java/com/example/Main.java Within the pom.xml file, set the groupId, artifactId, and version for the project.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>demoSpring1</artifactId> <version>1.0-SNAPSHOT</version> </project> Specify the Java version for the source code and JVM target. This can be done using either properties, as follows:
<properties> <java.version>17</java.version> <maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.target>${java.version}</maven.compiler.target> </properties> …or by defining the configuration for the maven-compiler-plugin:
Posts
Rails Override Default Scaffold Templates
To changing the Rails scaffold templates, we would find the location of the railties gem with bundle show railties and get to the path lib/rails/generators/erb/scaffold/ templates to find the templates to modify, as done in an older blog post.
However, this changes the templates for all rails projects from that point forward. To override the scaffolding templates for a single project, create the directory lib/templates/erb/scaffold and copy the railties templates into the directory to modify.
Posts
Macos Java Home Intellij Jdk Mac
Installing OpenJDK on MacOS is easy with HomeBrew. If you haven’t got HomeBrew installed, install HomeBrew by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/nitin.katkam/.zprofile Then, install OpenJDK with:
brew install java Define environment variables with:
echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc echo 'export JAVA_HOME=/opt/homebrew/opt/openjdk/libexec/openjdk.jdk/Contents/Home' Download Apache Maven from: https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz and define the M2_HOME and PATH variables to the extracted path and to the bin subdirectory.
The /opt/homebrew/opt/openjdk/ seems like the Java Home directory but because IntelliJ looks for a different JDK directory structure, it complains with a “The selected directory points to a JRE not a JDK”, so we instead specify the path in the .
Posts
Using Couchbase Sdk for Java
Start CouchBase in a Docker container:
sudo apt update sudo apt-get install -y ca-certificates curl gnupg lsb-release curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt update sudo apt-get install docker-ce docker-ce-cli containerd.io -y sudo docker run -d --name couchbase1 -p 8091-8096:8091-8096 -p 11210-11211:11210-11211 couchbase sudo docker logs couchbase1 Configure the CouchBase server by going to the URL http://localhost:8091
Posts
Rails Dropdown in Form Template References
The Rails scaffolded views do not generate a dropdown for selecting referenced data. Fortunately, this can be changed by editing the Rails scaffolding templates.
First, locate the railties gem:
find $(gem env home)/gems -name 'railties-[0-9].[0-9].[0-9].[0-9]' cd $(find $(gem env home)/gems -name 'railties-[0-9].[0-9].[0-9].[0-9]' | head -1)/lib/rails/generators/erb/scaffold/templates In the _form.html.erb.tt , add this into the attributes.each loop:
<% elsif attribute.reference? -%> <%%= form.label :<%= attribute.column_name %>, style: "display: block" %> <%%= form.collection_select :<%= attribute.
Posts
MongoDB REST With Ruby on Rails
Some NoSQL databases provide an HTTP REST API to interact with the database. MongoDB, however, has a binary network protocol similar to Postgresql, MySQL, and other databases. In this post, I build a tiny REST API with Ruby on Rails.
Routes With the objective of supporting only basic CRUD operations, the application needs to handle GET, POST, PATCH, and DELETE at a single end-point. However, if the application should be able to handle anything beyond the basic CRUD operations (Eg.
Posts
Using Active Support Outside Rails
The ActiveSupport gem gives us the ability to do things like 3.days.ago and to have Ruby return a date-time object.
To get this functionality outside Rails, require ‘active_support/all’, or to specifically load only the extensions for integers, require 'active_support/core_ext/integer'