<commit_before># SimpleRecord - ActiveRecord for SimpleDB

An ActiveRecord interface for SimpleDB.  Can be used as a drop in replacement for ActiveRecord in rails.

## Discussion Group

[http://groups.google.com/group/simple-record]

## Issue Tracking

[http://appoxy.lighthouseapp.com/projects/38366-simplerecord/overview]

## Getting Started

1. Install gems

ONE TIME: Be sure to add the new gemcutter source:

    gem install gemcutter
    gem tumble

THEN (you should have http://gemcutter.org in your sources and it MUST be above gems.rubyforge.org):

    gem install simple_record

2. Create a model

        require 'simple_record'
    
        class MyModel < SimpleRecord::Base
           has_attributes :name
           has_ints :age
        end

More about ModelAttributes below.

3. Setup environment

        AWS_ACCESS_KEY_ID='XXXX'
        AWS_SECRET_ACCESS_KEY='YYYY'
        SimpleRecord.establish_connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)

4. Go to town

        # Store a model object to SimpleDB
        mm = MyModel.new
        mm.name = "Travis"
        mm.age = 32
        mm.save
        id = mm.id

        # Get an object from SimpleDB
        mm2 = MyModel.find(id)
        puts 'got=' + mm2.name + ' and he/she is ' + mm.age.to_s + ' years old'
        # Or more advanced queries? mms = MyModel?.find(:all, ["age=?", 32], :order=>"name", :limit=>10)


## Attributes and modifiers for models

NOTE: All objects will automatically have :id, :created, :updated attributes.

### has_attributes

Add string attributes.

    class MyModel < SimpleRecord::Base
      has_attributes :name
    end

### has_ints, has_dates and has_booleans

Lets simple_record know that certain attributes defined in has_attributes should be treated as integers, dates or booleans. This is required because SimpleDB only has strings so SimpleRecord needs to know how to convert, pad, offset, etc.

    class MyModel < SimpleRecord::Base
      has_attributes :name
      has_ints :age, :height
      has_dates :birthday
      has_booleans :is_nerd
    end

### belongs_to

Creates a many-to-one relationship. Can only have one per belongs_to call.

    class MyModel < SimpleRecord::Base
        belongs_to :school
        has_attributes :name
        has_ints :age, :height
        has_dates :birthday
        has_booleans :is_nerd
    end

Which requires another class called 'School' or you can specify the class explicitly with:

    belongs_to :school, :class_name => "Institution"

### set_table_name or set_domain_name

If you want to use a custom domain for a model object, you can specify it with set_table_name (or set_domain_name).

    class SomeModel < SimpleRecord::Base
        set_table_name :different_model
    end


## Querying

Querying is similar to ActiveRecord for the most part.

To find all objects that match conditions returned in an Array:

    Company.find(:all, :conditions => ["created_at > ?", 10.days.ago], :order=>"name", :limit=>50)

To find a single object:

    Company.find(:first, :conditions => ["name = ? AND division = ? AND created_at > ?", "Appoxy", "West", 10.days.ago ])

To count objects:

    Company.find(:count, :conditions => ["name = ? AND division = ? AND created_at > ?", "Appoxy", "West", 10.days.ago ])

You can also the dynamic method style, for instance the line below is the same as the Company.find(:first....) line above:

    Company.find_by_name_and_division("Appoxy", "West")

To find all:

    Company.find_all_by_name_and_division("Appoxy", "West")

There are so many different combinations of the above for querying that I can't put them all here,
but this should get you started.

## Configuration

### Domain Prefix

To set a global prefix across all your models, use:

    SimpleRecord::Base.set_domain_prefix("myprefix_")

### Connection Modes

There are 3 different connection modes:

* per_request (default) - opens and closes a new connection to simpledb for every simpledb request. Not the best performance, but it's safe and can handle many concurrent requests at the same time (unlike single mode).<commit_msg>Updated readme.<commit_after># SimpleRecord - ActiveRecord for SimpleDB

An ActiveRecord interface for SimpleDB.  Can be used as a drop in replacement for ActiveRecord in rails.

## Discussion Group

[http://groups.google.com/group/simple-record]

## Issue Tracking

[http://appoxy.lighthouseapp.com/projects/38366-simplerecord/overview]

## Getting Started

1. Install gems

ONE TIME: Be sure to add the new gemcutter source:

    gem install gemcutter
    gem tumble

THEN (you should have http://gemcutter.org in your sources and it MUST be above gems.rubyforge.org):

    gem install simple_record

2. Create a model

        require 'simple_record'
    
        class MyModel < SimpleRecord::Base
           has_attributes :name
           has_ints :age
        end

More about ModelAttributes below.

3. Setup environment

        AWS_ACCESS_KEY_ID='XXXX'
        AWS_SECRET_ACCESS_KEY='YYYY'
        SimpleRecord.establish_connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)

4. Go to town

        # Store a model object to SimpleDB
        mm = MyModel.new
        mm.name = "Travis"
        mm.age = 32
        mm.save
        id = mm.id

        # Get an object from SimpleDB
        mm2 = MyModel.find(id)
        puts 'got=' + mm2.name + ' and he/she is ' + mm.age.to_s + ' years old'
        # Or more advanced queries? mms = MyModel?.find(:all, ["age=?", 32], :order=>"name", :limit=>10)


## Attributes and modifiers for models

NOTE: All objects will automatically have :id, :created, :updated attributes.

### has_attributes

Add string attributes.

    class MyModel < SimpleRecord::Base
      has_attributes :name
    end

### has_ints, has_dates and has_booleans

Lets simple_record know that certain attributes defined in has_attributes should be treated as integers, dates or booleans. This is required because SimpleDB only has strings so SimpleRecord needs to know how to convert, pad, offset, etc.

    class MyModel < SimpleRecord::Base
      has_attributes :name
      has_ints :age, :height
      has_dates :birthday
      has_booleans :is_nerd
    end

### belongs_to

Creates a many-to-one relationship. Can only have one per belongs_to call.

    class MyModel < SimpleRecord::Base
        belongs_to :school
        has_attributes :name
        has_ints :age, :height
        has_dates :birthday
        has_booleans :is_nerd
    end

Which requires another class called 'School' or you can specify the class explicitly with:

    belongs_to :school, :class_name => "Institution"

### set_table_name or set_domain_name

If you want to use a custom domain for a model object, you can specify it with set_table_name (or set_domain_name).

    class SomeModel < SimpleRecord::Base
        set_table_name :different_model
    end


## Querying

Querying is similar to ActiveRecord for the most part.

To find all objects that match conditions returned in an Array:

    Company.find(:all, :conditions => ["created_at > ?", 10.days.ago], :order=>"name", :limit=>50)

To find a single object:

    Company.find(:first, :conditions => ["name = ? AND division = ? AND created_at > ?", "Appoxy", "West", 10.days.ago ])

To count objects:

    Company.find(:count, :conditions => ["name = ? AND division = ? AND created_at > ?", "Appoxy", "West", 10.days.ago ])

You can also the dynamic method style, for instance the line below is the same as the Company.find(:first....) line above:

    Company.find_by_name_and_division("Appoxy", "West")

To find all:

    Company.find_all_by_name_and_division("Appoxy", "West")

There are so many different combinations of the above for querying that I can't put them all here,
but this should get you started.

You can get more ideas from here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html. Not everything is supported
but a lot is.

## Configuration

### Domain Prefix

To set a global prefix across all your models, use:

    SimpleRecord::Base.set_domain_prefix("myprefix_")

### Connection Modes

There are 3 different connection modes:

* per_request (default) - opens and closes a new connection to simpledb for every simpledb request. Not the best performance, but it's safe and can handle many concurrent requests at the same time (unlike single mode).
{'fraction_non_alphanumeric': 0.08409309021113244, 'fraction_numerical': 0.006238003838771593, 'mean_word_length': 3.3786764705882355, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3572662', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2409, 'n_words': 912}