http://blog.pemedeiros.com/Ghost 0.7Tue, 23 May 2017 22:22:35 GMT60In elixir-lang we have a primitive called atom. Atoms are nothing more than a constant string that behaves very differently compared to strings. When we declare an atom literal in our code, the Erlang VM checks if it is already in memory and reuses it. On the other side when]]>http://blog.pemedeiros.com/elixir-atoms-are-everywhere/47e104d8-a37a-4427-af7b-1fe5358aba74Wed, 17 May 2017 04:01:40 GMT

In elixir-lang we have a primitive called atom. Atoms are nothing more than a constant string that behaves very differently compared to strings. When we declare an atom literal in our code, the Erlang VM checks if it is already in memory and reuses it. On the other side when a code with strings are evaluated it allocates a new string in memory to be passed on. It works just like ruby's symbols.

To show how it really works let's write some code and see how it performs using a tool called observer. On elixir terminal we can just fire :observer.start() and go to the tab called memory allocation. So for some fun we can write a simple comprehension code that creates a really huge list full of string with the same value.

iex(1)> for _ <- 1..1_000_000, do: "string"

After executing this code we can see that we created a huge list with a million strings with the value string and we can notice the the memory of our erlang code just grew a lot. On my machine I just allocated about 225 mb.

If we run the code again but with atoms instead of strings we can see that we have a very different reality.

iex(1)> for _ <- 1..1_000_000, do: :string

Just 60 mb was allocated. It happens because we still had to allocate the list that was generated during the comprehension command.

The main difference is that with strings we really allocated one million strings and we put them into a list. And with atoms we just allocated the value once and we put the reference of this value a million times into the list. Which is a sweet difference.

Atoms Limitations

Using atoms to reuse memory is nice, but there's a limitation. The Erlang vm stores the Atom inside a table internally and has a default limit of 1048576 atoms per instance. This value can be configured, but in the end it is not an optimization that we want to make.

Other cool uses

Elixir atoms are not meant to use as constant. In fact they are used to name somethings on the language, such as

1. Boolean Values

Erlang there is no boolean values like other languages. But to represent boolean values it utilizes two special atoms that are true and false. Because erlang atoms are just words that starts with lowercase letters this representation suits well.

1> 1 == 1.
true

But on elixir expands the usage of true and false to be used also without the colon (:).

2. Module Names

Module names are also a representation of an atom. just to see the way we call erlang code on elixir. For example using the erlang math module to calculate the square root.

iex(1)> :math.sqrt(16)
4.0

We always call our functions by using atoms to to name the modules. We can see that on elixir by calling the funciton is_atom/1 on any elixir module such as GenServer. Notice also that on elixir if a token starts with upcase it is translated directly to atom, so it's possible to declare an atom with upcase value without using colons.

Now that we know that module names are just atoms, we can define modules with a lowercase name just by using an atom with a colon.

defmodule :hello do
def say_to(name \\ "World") do
"Hello #{name}"
end
end

iex(1)> :hello.say_to("John")
"Hello John"

Conclusion

Atoms uses very little memory and are unique across all the erlang node, using it to name modules and defining booleans values are a really smart approach. But they have very limited usage, it's not recommended to use atoms on the go for everything but for values that defines codes they are useful.

]]>This post is about how it feels to learn JavaScript in 2016. JavaScript sure got a lot better over the time but we are a little close to the edge of insanity IMO.

]]>http://blog.pemedeiros.com/weekly-picks-2/ce0ef64b-baf7-4f37-a343-8d2d99f9f86cSun, 09 Oct 2016 15:58:46 GMTThis post is about how it feels to learn JavaScript in 2016. JavaScript sure got a lot better over the time but we are a little close to the edge of insanity IMO.

Code wars now supports Elixir This is the best news ever! I'm a great enthusiastic from Elixir and codewars. Gonna make some of their katas on elixir and contribute with new ones for sure http://www.codewars.com/

]]>This is the start of my weekly picks. In which I will post everything internet that I see at the internet. Course it is just the most intersting things.

I'm a huge enthusiast of GraphQL concepts but I hadn't had time to play around with it. But I liked this

]]>http://blog.pemedeiros.com/weekly-picks/e644975a-d7f5-4341-8cec-95f2991ba24fSun, 02 Oct 2016 10:03:10 GMTThis is the start of my weekly picks. In which I will post everything internet that I see at the internet. Course it is just the most intersting things.

As everybody knows I'm in love with elixir-lang. Elixir lang is the best thing that I saw in the programming world for a long time. I've been following the elixir-core mail list for a long time (guess since the very beginning). And now I have kudos about this discussion. Deprecating the single quotes erlang string for a sigils may facilitate things especially for newcomers https://groups.google.com/forum/?utmmedium=email&utmsource=footer#!msg/elixir-lang-core/_pAjPEayLLI/G66JQEWvBAAJ

Last but not least There's mruby. A not new but still exciting way to put ruby code into embedded or minimal software. I've download, played a little with it and of course I created some small integration with a golang code. I may look better into this in the future. http://mruby.org/

]]>Recently I tried to compile erlang from source on my El Captain. Have done it before a bunch of times, but I wanted to test some new features on their new 19-rc1 release so I downloaded and tried to follow the readme. One thing though that is really mandatory for]]>http://blog.pemedeiros.com/compile-erlang-from-source-montain-lion/0489ce08-2ff2-4b7d-9112-5ff917b611f9Thu, 16 Jun 2016 11:02:05 GMTRecently I tried to compile erlang from source on my El Captain. Have done it before a bunch of times, but I wanted to test some new features on their new 19-rc1 release so I downloaded and tried to follow the readme. One thing though that is really mandatory for me, is the I must compilation with all the apps that erlang ships. Which turned out to be a little headache.

First step: configure

At first we need to configure our environment to check everything that we can do. Luckily Erlang has a nice command line tool that helps we check our environment and set all environment variables before we get started. So after download the source code, just type $ ./otp_build autoconf and we will have a nice set of configuration option to start with.

After that command we will gain a 'configure' command that is the command that will actually check all our dependencies. So just run $ ./configure and wait for it.

At the end of the execution we may have a couple of problems. In my case I have with two libraries, crypto and odbc with erros like ODBC library - link check failed. For crypto to works fine we need to install and link openssl properly. Using brew just makes sure you have the latest version of openssl

$ brew update

$ brew install openssl

Then we need to link open ssl. Once we may have some linked files that conflicts with us we need to pass the --force option

$ brew link openssl --force

Crypto must be fine by now. We then need to configure odbc which I found a bit trickier, once it was not a problem in the past. First install unixodbc then some installation paths by using odbc_config.

Those two configurations paths we will be using to configure our installation again with the flags CFLAGS and LDFLAGS So just type on the configure phase something similar to this (depending on your output):

After a coupe of minutes your environment must be ok for the newest erlang version compiled directly from source. So now you just need to continue with the basic make and sudo make install.

]]>On OTP (Erlang included) we have the concept of a GenServer, that mostly stands for generic server. On Elixir we also have two kind of servers that are not that generic: Agent and Task. Both are implemented using GenServer, but more specific. Agents were created to be a server focused]]>http://blog.pemedeiros.com/elixir-agents/4b6d4a78-0c5d-440a-bbbe-c3d8eef6aa82Tue, 08 Mar 2016 10:00:25 GMTOn OTP (Erlang included) we have the concept of a GenServer, that mostly stands for generic server. On Elixir we also have two kind of servers that are not that generic: Agent and Task. Both are implemented using GenServer, but more specific. Agents were created to be a server focused on store data and Task to compute data.

Recently, I've decided to take a better look on Agents and I must admit that I started to like it. I'm use then to play around with some small projects, instead of store data in a full featured DB, it's been a good way to play around.

Agent 101

Agent's a simple process that stores and retrieves data given function. For example.

As we can see we start a process giving it a name (people) and when we need to do any operation on our agent named :people, all we need to do is pass the atom as the first argument, which can also be a PID of the process.

Now that we have the basics of Agents, let's move on creating a new Agent which is unique for our application. let's call this module PeopleStore. Turns out that Module names are atoms on elixir, so we can easily use then as the name of our Agent.

On Enum.find function we used a shortcut function operator, which is just a syntax sugar for fn x -> x.name == name end. It's visible how it can be simple for small things, it uses &N to determine the argument position of the callback function.

Now with our PeopleStore we can store people and search for people based on name. I'm using it sometimes to fake some test data, It's really useful. And being a GenServer we known It won't have concurrency issues, once an Agent will only do one thing at the time.

The game has its ups and downs, but you can never lose focus of your individual goals and you can't let yourself be beat because of lack of effort.

Michael Jordan

Michael Jordan is nothing more than one of the best players that ever lived. Based on that focus is also really important for our software. When we build software most of the times we most focus on do one thing right and guaranty that it doesn't fall apart.

That's a trick that can be used with ExUnit to focus on a specific test so we can develop a feature and modify a code without running all the tests (which can take some time depending on the size of your app).

For that ExUnit comes out of the box with the ability to add tags to your code so it's possible choose what code can be executed. In this case I like to always add the tag :focus on the test that I mos interested on at the moment.

]]>Ruby 2.3.0 has been released (yey!) and with it one of the coolest features of all time. The safe operator.

The safe operator is just some simpler way to do something that we already have in rails. But now this feature is in the core language.

Now show

]]>http://blog.pemedeiros.com/ruby-2-3-0-new-features/9e0067dc-6d2b-4526-8e20-091b3d1e34c8Fri, 08 Jan 2016 11:53:07 GMTRuby 2.3.0 has been released (yey!) and with it one of the coolest features of all time. The safe operator.

The safe operator is just some simpler way to do something that we already have in rails. But now this feature is in the core language.

Now show me the code. On rails is pretty common to have errors like this

There's no error raised if one uses the try operator. That's pretty cool. Unfortunately It is limited to the rails stack. Now with the new ruby 2.3.0 is possible to use the safe operator which was attributed to the & key word.

Now the same behaviour we can extend with

> @current_user.trophy.&title
# nil

Now is much safer to write code in plain ruby.

]]>When I started working with rails. Rails was on version 2 uploading a file was always kind of a dificult task. There was paperclip which was pretty neat to work with. It facilitates a lot the way to upload a file into a rails app. Shortly after I met carrierwave]]>http://blog.pemedeiros.com/rails-4-2-and-refile-file-upload/f50b9847-fc64-4225-ac98-1ec0f646dafcTue, 28 Apr 2015 16:54:35 GMTWhen I started working with rails. Rails was on version 2 uploading a file was always kind of a dificult task. There was paperclip which was pretty neat to work with. It facilitates a lot the way to upload a file into a rails app. Shortly after I met carrierwave which I prefered because of the support it had to store multiple images and pre processed files with an Uploader declaration.

New times has come, and wiht it I discovery a new guy that does the same job and has a really nice support for rails 4.2. Meet refile. Just like paperclip and carrierwave it provides full support to manipulate images, validates types and facilitates data trasnfer with support to cloud servers like amazon s3.

To get started with refile first let's create a scaffold for post and later we will attach an image to the post.

]]>I've recent started a new nodejs project. I'm a person that likes to experiment a lot. And I heard a lot of buzz around using Mongoose with the MEAN stack. So I've decided to give it a try. I already shipped a project with mongodb using rails and mongoid and]]>http://blog.pemedeiros.com/mongoose-and-stuff/48873a0d-3ed9-430e-a7ee-7e447cde690fWed, 10 Dec 2014 16:27:00 GMT

I've recent started a new nodejs project. I'm a person that likes to experiment a lot. And I heard a lot of buzz around using Mongoose with the MEAN stack. So I've decided to give it a try. I already shipped a project with mongodb using rails and mongoid and I loved it. What I loved the most, was the way that mongoid integrates really well with all the rails stack.

Well nodejs is not like rails, while rails is a fullstack framework nodejs is a platform. So some features will be unfair to compare. I also choose to write the code for this new project with coffeescript. Which is a very nice language that compiles to javascript code. So let's get started.

I always create a json file that has all my configurations of my server. So I can easily switch across environments. Using ports, and different styles.

We've got our file set and up for mongoose now is time to use mongoose for his only and one propose. Be a simple Object Document Model or ODM. Which will handle persistence, validations and a schema definition. To illustrate it better let's first write some tests for our app. To write the tests I'm using mocha, so let's start.

What happened there? It's simple, We build a document after the documentation of mongoose and we saved it. The save method expect a function which the first parameter is an error if it happens. So we called assert inverting the err message type it returns. If we run our tests it will fail. So it's time to create the code to pass it.

For that mongoose relies on a schema that has to be defined for our model. With this schema we will provide kind of a contract that our model will have to write to our mongo database.

Now our model is created, if we run our tests it will pass nicely. This is because we declared the mongoose schema with the content that we needed to save it properly. There's something strange in our schema declaration. We declared a json with title and content fields to be a String. But when I declared pubdate field I didn't pass a type like String, but a object. It is the kind of the flexibility that mongoose give us. If you need to have some operations, such as validations or default value. We can just pass an object with type being the actually type of the field and add what we need to validates, or set default values for our field. More information about the SchemaTypes can be found here in the documentation.

If we try to a field that is not declared at our schemas when we receive a callback it will not be show at the object that is written at the database. And it will also not be available at our database.

On the example above if we call post.foo we will have undefined. This is a nice feature to have in our System. And is definitely the power of having a Schema mechanism on our hands. But if you got to have the flexibility of mongodb in a field, just need to use the type Schema.Types.Mixed. Within this type you can write anything at that field and mongoose will persist it, even nested objects or arrays.

Time to validate

Build schemas for our models in mongodb is a nice feature. But the best part of having a schema is the ability to handle validations. For example, let's say neither our content or title can be blank. To solve this problem with mongoose all we need to do is add an object to the key to the field name and a object with the type of the schema and with the validation option.

Just with that required true, mongoose handle the field and does not save the object if the title is not valid for this operations. Returning an error at the save callback function. One can also pass a string instead of true to the required validation. That way when the error is returned it will return the string of your choice. Just be careful to pass the value PATH under brackets {PATH} to indicate the field name.

At the mongoose docs we can see others handful validations that are nice to use, such as match to restrict via Regex the field that will be saved and the min and max for restrict the range of a number.

To have those buildin validations are great. But the greatest thing is to have custom validations. One can Apply a validation just by declaring an Array in which the first parameter will be function that will handle the validation and the other parameter will be the text of the error message case this function returns false.

This is nice way to handle error before acually writing at our database.

And finaly another good thing that I like about mongoose is the ability to create nested schemas. Everyone knows that mongodb has no support for relation between documents the same way that SQL databases does. But It's possible to embed a document inside another. And we can do that with mongoose just declare our nested schema inside another schema. To show it let's first create a Comment schema for our BlogPost.

Mongoose is not the only thing that can handle mongodb operations in node the native mongo driver is pretty good. But of course it lacks schemas definition, and of course model validations which in my opinion is the killer feature of mongoose.

]]>One of the best features of elixir language are the tools that ships with it. One of then Is Mix. Mix is build tool that helps create, setup and run an elixir project. To demonstrate how mix do all this, let's create a simple project for a popular Kata named]]>http://blog.pemedeiros.com/mix-build-tool/2990b25c-bfea-4af1-b026-a1267c461978Mon, 15 Sep 2014 06:36:22 GMTOne of the best features of elixir language are the tools that ships with it. One of then Is Mix. Mix is build tool that helps create, setup and run an elixir project. To demonstrate how mix do all this, let's create a simple project for a popular Kata named FizzBuzz.

The rules of FizzBuzz are:
The program must output a line from 1 to a given number with three exceptions:

If the value of that line is divided by 3 then the output must be Fizz.

If the value of that line is divided by 5 then the output must be Buzz.

If the value of that line is divided by 3 and by 5 the output of that line must be FizzBuzz.

Now that we have all the rules. Let's start by creating a new app. The fist step is to type mix new fizz_buzz at the terminal we will see this output:

Our app is now created, we can now enter at the fizz_buzz folder and see the file structure for a typical elixir app. The three most important files are the mix.exs, test/fizz_buzz_test.exs and lib/fizz_buzz.ex. mix.exs is the file that configures your project. It has 2 main public functions application and project. The project function must return a list with the project information just as the app name, the app version and its dependencies. The application function is where you put the information that will be used to generate an .app file.

The lib/fizz_buzz.ex is the file generated that is intended to be the main file of our app. It is created by default and only has an empty module to be implemented.

# fizz_buzz.ex
defmodule FizzBuzz do
end

The test/fizz_buzz_test.exs file is where you put your tests. In fact every file that lies inside the test will be executed as a test file. By default when we create a project, mix creates a simple test that tests if 1 + 1 is equals 2.

# fizz_buzz_test.exs
defmodule FizzBuzzTest do
use ExUnit.Case
test "the truth" do
assert 1 + 1 == 2
end
end

You can run this test with mix test command at the root of the project. We will see that our test will be pass. Try changing the value of the sum from 2 to 3 and see what happens.

Let's start to implement our FizzBuzz by writing some tests. First Let's create a test. Guess the first thing that we got to do is to create the first test from 1 to 3 where it will output this:

Aha! I bet you already realized what I did. In elixir the last expressions is the return value of the function. So what our function does is just returning the expected output for our test. if we run mix test we will see that everything is passing and we are very happy. This is part of the TDD philosophy, first we write the test and it should fail, then we write a small piece of software so the test can pass. Then finally we refactor the code. We already known what we want so we can go straight to our code.

defmodule FizzBuzz do
def compute(number) when number > 1 do
compute(number - 1) <> output_line(number)
end
def compute(1) do
output_line(1)
end
defp output_line(number) do
if rem(number, 3) == 0 do
"Fizz\n"
else
"#{number}\n"
end
end
end

We can see that at our function compute/1 definition we are defining some rules for two specific cases. In Elixir what makes a function signature is its names and its arity (number of values you can pass to a function). But we can define some rules for a function to execute, just like we did with when. Note that we are using recursion instead of using a conventional loop, it's a common thing at functional languages to use recursion calls. Run mix test and our test pass. Let's write the test to print until 5 so we can see the Buzz word at screen.

If we try to run this test it will fail. This is because we have not implemented a rule of what happens when the number is divided by 5. Since we have refactored our code before, we can just modify our output_line/1 function and just add some simple rule there and everything will pass! yey!

defp output_line(number) do
if rem(number, 3) == 0 do
"Fizz\n"
else
if rem(number, 5) == 0 do
"Buzz\n"
else
"#{number}\n"
end
end
end

Now that everything is passing we can try to refactor that code. I really don't like the inner block thing that we are producing for every new rule we must explicitly create a new if block and close it. Let's try another conditional that is more clear to use.

And our tests are passing again. By this time we must already solve the problem. You can try by creating a string that goes from 1 to 100 and will see that our tests will pass. At this point we have our mix application completely done. We can run test on it. Now let's try to run our code at the console. If we type iex -S mix we will enter at an interactive Elixir console and we will have access to all our code thought here. Let's try by typing FizzBuzz.compute(5). The output of our FizzBuzz challenge is done there. And you can try any other value that you want.

This was a quick introduction to mix, a build tool that ships with Elixir. There's a lot to go from here. But having a such nice tool to create a project with a full functional test suite is already great.

]]>Elixir is a dynamic, functional language that runs on top of the Erlang VM. It has very friendly syntax and it is meta-programming aware. I've been playing around with Elixir since version 0.5 and for our happiness, this week the version 1.0 of the language was finally released.]]>http://blog.pemedeiros.com/elixir-very-simple-and-direct-getting-started-guide/05133b05-d06b-4dcc-a47e-42c21553fd8eSat, 13 Sep 2014 02:53:00 GMTElixir is a dynamic, functional language that runs on top of the Erlang VM. It has very friendly syntax and it is meta-programming aware. I've been playing around with Elixir since version 0.5 and for our happiness, this week the version 1.0 of the language was finally released. From the things that I most like about the language I've got to say that the easy tools to build applications in a convenience way is the feature that I like the most. So let's get start and see the basics of running some code and create a simple application.

In the official web site there's a page that show all the currently possibilities to install elixir in your machine. There are packages for Mac (port, brew), gentoo, arch, fedora and more. I prefer to install with the precompiled packages but for that one must isntall erlang first. Here we can see an easy way to setup our environment to run Erlang and Elixir on our machine. The current version of Elixir requires Erlang 17 or superior. So be sure to install the right version of Erlang first.

Once we setup our environment properly We have four new executables in our environment elixir that can run our elixir code. elixirc to compile the code to a .beam extension to run at the Erlang VM. mix to create and manage our app with the properly steps and dependencies. And lastly iex, and iterative tool to run and evaluate elixir code on the fly. To get start jus type > iex in your terminal and you can start running some code.

iex > name = "John"
"John"
iex > "Hello #{name}"
"Hello John"

With this code we could start our Hello World. iex evaluate every line of code and output the result for each of then. In this example we can see a string interpolation, a nice feature of elixir. By using #{} we can represent represent any value inside a string without concatenating it explicitly. You can try doing some simple operations in the terminal. such as sum two integer with + or concatenate two strings with <>.

One thing about Elixir and the Erlang VM. Is that there's no mutability. In Erlang if you assign a value to a variable you can't reassign it later. as if you can see bellow, it shows an error if you try to assign the value again. That happens because the = operator is not an assignment operator but a matching operator.

At the first time we do X = 1 erlang see if X is matched to any value. If it is not matched it will bind 1 to X. Once X is bind to 1 if you try to do the match again it will see that 1 is not 2 and an error will occur. Elixir is a little bit different from that. Duo the fact that elixir is build on top of the Erlang VM immutability will maintain, but with Elixir it is possible to reassign a value to another variable more than once.

iex> x = 1
1
iex> x = 2
2

One example that we illustrates great the immutability aspect of elixir is how it handles Lists. If we create a list [1,2,3] and than we want to perform an operation at the List, like update a value from it. What Elixir will do is create another list with the new values and return it just like the docs explain. That way we are assigning a new array with the updated values to the variable a.

With the basics up and ready let's do some code that can be compiled and executed. Basically, Elixir organize everything in modules. The modules contain names functions that can have public or private assessors. Let's write a hello world file names hello.ex

# hello.ex
defmodule HelloWorld do
def say_hello(name) do
IO.puts("Hello #{name}")
end
end
HelloWorld.say_hello("John")

Now that we have our file let's run the code. To run this just type on command line elixir hello.ex and you will see the Hello World message popup. But wait, Didn't I said that elixir compiles code to run on Erlang VM. For that we have the command elixirc. If you run elixirc hello.ex it will produce the compiled file Elixir.HelloWorld.beam, which it is just the binary that will load your code into the Erlang VM. Once generated if you enter into iex from that folder you can type HelloWorld.say_hello("Paul") and your code will run. This is because it is already compiled into a .beam file.

This is the quickstart of the language itself. With this basic structure to start to understand how an Elixir app is build. The next step to go from here is to understand how mix can make our life easy by integrating some tests to our code and by help with dependency management.

]]>I was doing some rails stuff lately and needed some authentication module for my application (such as every other application that I ever used). So I was going to use devise as I always use to bootstrap my authentication module.

But as a unfortunately surprise. The current version of devise

]]>http://blog.pemedeiros.com/authlogic-with-rails-4-2/fe289250-7289-48ad-9447-e1525947ad3dThu, 11 Sep 2014 07:13:57 GMTI was doing some rails stuff lately and needed some authentication module for my application (such as every other application that I ever used). So I was going to use devise as I always use to bootstrap my authentication module.

But as a unfortunately surprise. The current version of devise doesn't work with the new version of rails. So I had to go to other fast alternative. Searching over the web I came across authlogic. I remember that I was using authlogic at my previous job with one specific web, and it worked ok. For my surprise it worked really well with Rails 4.2.0.beta. The only thing that I found confusing was the documentation. It's not as clear as the devise one. But here we go step by step.

First thing I did was put the gem at the gem file.

gem 'authlogic', '~> 3.4.2'

Then we run bundle install to install dependencies and we are ready to create our model. One can create a model with any field that is needed just remember to include some fields that are necessary for authlogic to operate. such as login, email, crypted_password, password_salt and persistence_token. You can choose to have only login or email. But if you have both fields authlogic will lookup for both. This is an example of my model migration.

Those last fields are from the documentation module MagicColumns. If you have then at your athlogic model they will be used by the library, but you can simply ignore then.

Then is time to tell that your model itself will use authlogic. For that authlogic provides a method named acts_as_authentic which accepts a block of code for configuration. I really had a hard time to find more information about all the possible options it accepts but the one that I really wanted to use was easy to find. That was how to set the crypto provider to use bcrypt.

Authlogic expect us to create a Session model so it can use it to create and restore our session object for that I just placed over the models folder a class named UserSession that inherits from Authlogic::Session::Base

class UserSession < Authlogic::Session::Base
end

Then It was time to create a controller and a view that would handle the authentication actions. I created then a controller just for that with 3 actions new, create and destroy, just like the rails resource way. and created our form to handle the login properly.

We can place that helper method in any part of our app to verify if the user is logged in. and to permit access to certain actions.

Conclusion

It was not hard at all to setup authlogic I can happily use it during all my app development. But to tell the truth I can't wait to go back to devise just for the good documentation and examples. That is the thing that authlogic lacks the most. But it is an good and easy library to setup.

]]>Recently I’m just starting to relearning c++ so I can do dig in some legacy code at the company I work for. And for that I decided to start with the basics step by step and take on some notes about all the process of relearning it.

I’m

]]>http://blog.pemedeiros.com/back-to-c/92b4e3c7-6e38-4ef4-acc2-d3fc356c3844Sun, 07 Sep 2014 02:26:06 GMTRecently I’m just starting to relearning c++ so I can do dig in some legacy code at the company I work for. And for that I decided to start with the basics step by step and take on some notes about all the process of relearning it.

I’m a native C++ programmer. What that means is that C++ is my first programming language that I learned for good, and was by coding c++ that I’ve learned to love to programing and is the thing that I do everyday, doesn’t matter if it is for my day job work, I’m always writing some piece of code for whatever reasons. Unfortunately, C++ was not so good to supply my old necessities anymore like it used to. I wanted to get the job done fast and to complete my goals as fast as I can with an elegant code. That was when I learned Java, a.k.a The biggest mistake of my life. Java has a error proof way that is nice for some people that are already into C++, but it was not I was really looking for. It seems that It took a lot of the verbose of C++, simplified a little with not a worth gain. It was when I met ruby, javascript, node, python and programming friendly languages like that.

Those kind of scripting languages were a game changer for me. I could do what I wanted as fast as I could and with such expressiveness that I could not find either in C++ or in Java. So I left C++ behind for sometimes. I really wish I could left Java behind too, but I always had to code something or another in Java because the IT world, specially in Brazil, basically breaths Java.

During the time I was looking for expressiveness and fast development, I ended up going away from some system development and away from some root concepts that I was missing somehow. So In the last years I ended up reading some articles about programming in C++ again and I was looking for what was new in other languages, like Scala, Go, Rust. I had to say. I really liked what Rust and Go are promising. But I just can’t introduce any of those languages to the table of the companies that are not wiling to try new things just yet. So I have to dig in deeper on what they are using and propose some changes based on what they are actually doing. So I’m relearning to fly on C++ code.

I bet that the hardest thing is going to be how to make a 100% portable code. I never was a biggest windows fan. All my development stack are based on *nix systems. But one thing that I learned is that C++ is widely used on Windows development. Here where I live mostly of the people that are programming on C++ are using Windows for development. That scares me a little, but that’s just how it is. I did not decided yet how I’m going to do that. But I’m starting by reading some basic C++ concepts and hopeful I’m gonna keep updating my status on that.

And of course I’m planning on write some more on c++ development.

]]>If you are programming javascript in a while. Or just started nodejs and are having some trouble with the famous callbacks hell that javascript provides. Well that's the price to use a lot a asynchronous functions. just like the code below

]]>http://blog.pemedeiros.com/promises-on-javascript/22714676-30bc-4359-b889-274e8a40d8acTue, 22 Jul 2014 07:22:41 GMTIf you are programming javascript in a while. Or just started nodejs and are having some trouble with the famous callbacks hell that javascript provides. Well that's the price to use a lot a asynchronous functions. just like the code below

There are up to 6 levels of indentation on that code. and it can potentially grown. This is because we are just doing 2 database calls and 1 network call that has to be treated in a synchronous way. That is something that sucks about javascript callbacks. Sure the code is not perfectly planned. But It's just an example of how it could have grown wrong doing just something that any system nowadays has to do. Which is handle multiple IO calls. For that problems like that Promises were created.

For an example of how handle promises I'm gonna use the Q library which is maintained currently by Kris Kowal. This library is highly used at many places that has a javascript interpreter, like nodejs and all the major web browsers.

To use the example above with it our code will became really more clear.

Just like that our code became so much more readable. With the then statement we pass a callback that will eventually be called if our expression executes successfully, in this case the IO calls. And we chain all the callbacks by returning a new promises that will perform another IO call that will call the next then callback in the stack.

But wait, what about the error handles. What happened to them?

They can be easily replace with just one last call at the end of the stack of promises handles, which is the catch call.

]]>Working with the web for a considerable time. I've came across the following though: "You don't do an GET anymore. I'ts 'Ugly' to do so because it generates an ugly URL". That came especially from some juniors devs that I've contacted in recent years.

But to tell the true. It

]]>http://blog.pemedeiros.com/the-utility-of-http-get-method/97de0ba3-bb29-4d6f-9d8b-a70ff5d7084eFri, 25 Apr 2014 17:01:55 GMTWorking with the web for a considerable time. I've came across the following though: "You don't do an GET anymore. I'ts 'Ugly' to do so because it generates an ugly URL". That came especially from some juniors devs that I've contacted in recent years.

But to tell the true. It is a little bit of ugly to pass a lot of parameters vi the get attribute to url. But it has a good and meaningful propose. It is made to... wait for it... to get the data from somewhere.

As it is pointed by some authors in the REST/Hypermedia/Webservice literature. When you perform the an HTTP request using the GET parameter at the header. You must not interfere with the universe that the service you are fetching the data from lives in. That's why when you enter some news sites you are fetching the content with it.

The Get method must be safe. And of course it must also be idempotent. Which means it must not cause any side effects to the system. It only exists to fetch the data by a meaning url. For example the url /books says something. It says that it will fetch some books. But let's says I'm developing some library system. And of course I need to apply some filters to the /books url. In that case It's really ok to pass parameters at the url like /books?genre=fiction. It has a meaning that I want to fetch some books of my system And wanna to apply the filter that says, I wanna books with the genre fiction. It's not so ugly like the guy I quoted at the beginning of my post, because it has some meaning.

But let's say That I wanna to fetch the cover of a specific book. In that case you should use the cover parameter in the URL and not in the segment expansion. Which means that your url can be /books/13/cover. That means that I will fetch the cover of the book with the id 13. The expansion path must be used to represent some kind of segment of the resource. In that case. the cover of an specific book.