Sonntag, 15. Dezember 2013

Life after Coderetreat 2013

Last Saturday I was on Coderetreat 2013 in Vienna. There we tried to implement rules of Conway's Game of Life. During the day I've got a lot of experience in TDD, pair programming and languages I haven't used for a while. One thing I've missed was final implementation of game.

One day after the event I've tried some ideas I'd got before. I've pushed my own implementation of game in my Github repository and created an instance of server on Heroku.

Montag, 9. Dezember 2013

RPI: extending swap

Last weekend I spent some time running my Scala/Play application on Raspberry PI. I got an OutOfMemoryError and had to slightly extend swap space:

 >sudo su  
 >echo "CONF_SWAPSIZE=512" > /etc/dphys-swapfile  
 >dphys-swapfile setup  
 >dphys-swapfile swapon  

Sonntag, 24. November 2013

Quotes from Coffeescript Application Development book

Above I posted some extractions from the book Coffeescript Application Development I found the most interesting.

Classes
Most of the time, you will define your methods in the main body of the class definition. This is the easiest and most logical place. However, there are a few occasions when you may wish to attach a method to an object prototype from somewhere else in the code. This may be for stylistic or metaprogramming reasons, or most likely, to attach methods to an object that you did not define. CoffeeScript provides the :: operator for easy access to an object's prototype.
 Variable arguments list
Appending ... to an argument name in the function definition tells CoffeeScript to accept any number of arguments. That variable will be assigned an array containing all the relevant values that were passed.
Check on existence
CoffeeScript has a helpful existential operator to deal with these situations correctly and cleanly. It is a simple ? symbol.
Destructuring assignments
We can even use destructuring assignment to perform in-place variable swaps:
[first, second] = ["cow", "milk"]
# Wait, or is it the other way around?
[first, second] = [second, first]
console.log "Don't put the #{second} before the #{first}."
Destructuring assignment can be used with more complicated structures too:
[drink, [alcohol, mixer]] = ["Screwdriver", ["vodka", "orange juice"]]
console.log "A #{drink} consists of #{alcohol} and #{mixer}."
Loops return array of results
One extremely useful feature of CoffeeScript loops is that they return a value—an array of the results of the inner statement for every item in the loop. If you've ever used Array.prototype.map to transform an array, you'll be happy to see that it's easy to achieve the same functionality in CoffeeScript.
Link on book in store

Review of CoffeScript Application Development book


CoffeeScript Application Development, the book I read lately, helped me to take overall look at the language I have recently used on a number of projects. Like many books about programming languages it is also divided into chapters which follow you from the very beginning — tools installation and basic syntax to more advanced topics like classes and asynchronous operations. The book also covers such things as debugging, using the language for server-side development and adopting it for usage with different JS-frameworks.

Each topic of a book goes with a number of examples clearly showing how to use specific CoffeeScript features and what code will look like after compilation into JavaScript. The only thing I found ineffective is a way how author presented the example application. Its progress was mixed within the material treatment. For me as a person who read a book without immediate trying and following examples by hand it has been difficult to get a sense of this examples. I would rather move this example application into separate appendix and make cross-links on related topics. Thus the material treatment could be more consistent and at the same time the example application could be more readable and perceptive.

I would definitely recommend this book for reading people who find annoying such things in JS as checking curly braces parenthesis, frequently testing variables on existence and maintaining code written using different code conventions.

After reading I have got obvious feeling that CoffeeScript makes code much clearer and understandable allowing to explain things in a very concise way in comparison with bare JavaScript and as the result it makes code better maintainable. As soon as ease of maintainability is one of the corner stones of software development in modern realities the profit from usage CoffeeScript excessively covers spending time on its learning and getting on usage.

You can find this book on the following link


Freitag, 13. September 2013

Yet another stack plugin for Flotchart

For my own project I need a plugin which can display at the same time stacked bars with the positive and negative values and usual line stacked charts with positive values.

There are two plugins which can do it:

  1. Stack plugin from Flot distribution
  2. Stack Positive Negative Bars by ericliao
Each of these plugins does a part of job. The first one can display line and bar charts only with positive values. The socone does not work with line charts.

That was the reason why I decided to write yet another plugin for Flot.

You may see here the results and sources on Github.



Feel free to use and contribute.

P.S.: I have an ide to update it to show stacked line diagrams with negative values also, but not sure that user can interpret it correctly :)

Montag, 26. August 2013

Nginx, remote_addr and Tomcat in reverse proxy configuration

Many people use Nginx as reverse proxy and their applications may depend on remote_addr request variable. The following steps help to correct it.

First of all check if Nginx contains necessary module be executing the command:

nginx -V

You should see this line in the output:

--with-http_realip_module

Then you need to add following lines to your Nginx configuration:

set_real_ip_from   111.111.111.111;
real_ip_header    X-Real-IP;
real_ip_recursive on;

And at the end add this to your Tomcat server.xml

 <Valve
   className="org.apache.catalina.valves.RemoteIpValve"
   internalProxies="111\.111\.111\.111"
   remoteIpHeader="x-forwarded-for"
   remoteIpProxiesHeader="x-forwarded-by"
   protocolHeader="x-forwarded-proto"
   />

How to configure valve you may read here. Do not forget to replace 111.111.111.111 with your server IP.

Good luck!

Dienstag, 23. Juli 2013

Ember.js plus Bootstrap Datetimepicker

If you need to use Bootstrap Datepicker component in your Ember.js application the code shown below will help you.
  1. First of all you have to create new View object:
  2. App.DatePickerView = Ember.TextField.extend
     _valueChanged: Em.observer 'value', -> 
      value = moment(@get('value'), 'DD.MM.YYYY HH:mm')
      @$().data("DateTimePicker").setDate(value)
      return
     didInsertElement: ->  
      @$().datetimepicker
       format: 'DD.MM.YYYY HH:mm'
      return  
    
  3. And then insert it into your template:
  4.  {{view App.DateField value=mydate}}  
    
That's it!

Samstag, 29. Juni 2013

Spring, JPA and Lazy fetch

Your task is to add a relation one-to-many to your entity class.

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    List<Account> accounts;
}

Take a look on FetchType. In this case (LAZY) you have to fetch the collection in same session as the entity object. But Spring closes a session before. All you need is to implement a method in service layer and annotate it @Transactional(propagation=Propagation.REQUIRED). In such case Spring does all the operations inside the method in one session. For initialization just call some method (e.g. size()) on collection.

P.S.: Do NOT forget to switch on annotations in your configuration.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       ">

    <!-- ... -->

    <tx:annotation-driven/>
    
    <!-- ... -->

</beans>

Sonntag, 9. Juni 2013

CoffeeScript compilation in Maven project

There are three available Maven plugins:
Brew works and has a lot of features, but unfortunately it is not supported yet.
Coffescript-maven-plugin works only with Java version 7, what in our case is not applicable.
Coffee-maven-plugin by talios has been recently updated und works fine with Maven 3 and Java 6.