# HoboFields API In order for the API examples to run we need a connection to a database. You can ignore this if you're just looking for documentation. {.hidden} Our test requires rails: {.hidden} >> require 'rubygems' >> require 'activesupport' >> require 'activerecord' >> require 'action_controller' {.hidden} We need a database connection for this test: {.hidden} >> mysql_adapter = defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql' >> mysql_user = 'root'; mysql_password = '' >> mysql_login = "-u #{mysql_user} --password='#{mysql_password}'" >> mysql_database = "hobofields_doctest" >> system "mysqladmin #{mysql_login} --force drop #{mysql_database} 2> /dev/null" >> system("mysqladmin #{mysql_login} create #{mysql_database}") or raise "could not create database" >> ActiveRecord::Base.establish_connection(:adapter => mysql_adapter, :database => mysql_database, :host => "localhost", :username => mysql_user, :password => mysql_password) {.hidden} Some load path manipulation you shouldn't need: {.hidden} >> $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), '../../hobofields/lib') >> $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), '../../hobosupport/lib') {.hidden} Require the module we're testing: {.hidden} >> require 'hobosupport' >> require 'hobofields' ## Example Models Let's define some example models that we can use to demonstrate the API. With HoboFields we define the model's fields, with their name and type, directly in the model like so: >> class Advert < ActiveRecord::Base fields do title :string body :text contact_address :email_address end end (Note: `:email_address` is an example of a "Rich Type" provided by HoboFields -- more on those later) The migration generator uses this information to create a migration. The following creates and runs the migration so we're ready to go. >> up, down = HoboFields::MigrationGenerator.run >> ActiveRecord::Migration.class_eval up We're now ready to start demonstrating the API ## The Basics The main feature of HoboFields, aside from the migration generator, is the ability to declare rich types for your fields. For example, you can declare that a field is an email address, and the field will be automatically validated for correct email address syntax. ### Field Types Field values are returned as the type you specify. >> a = Advert.new :body => "This is the body" >> a.body.class => HoboFields::Text This also works after a round-trip to the database >> a.save >> b = Advert.find(a.id) >> b.body.class => HoboFields::Text HoboFields::Text is a simple subclass of string. It's a "wrapper type", by which we mean you pass the underlying value to the constructor. >> t = HoboFields::Text.new("hello") => "hello" >> t.class => HoboFields::Text If you define your own rich types, they need to support a one argument constructor in the same way. Although the body of our advert is really just a string, it's very useful that it has a different type. For example, the view layer in Hobo Rapid would use this information to render a `