<commit_before># CodeBox::CodeAttribute

Lets you define attributes as codes, instead keys (ids). For simple option storage saving a string code is often more simple an conveniant the storing an artificial id-key referencing a special code object.
CodeBox lets you access define codes as strings and access the associated code objects in various ways.

## Installation

Add this line to your application's Gemfile:

    gem 'code-box'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install code-box

## Usage

### Specifying attributes as codes

There are cases you want to store 'named codes' instead artificial keys.
Codes make sense for stable references and better readability of the raw data.

There are several options to specify an attribute as a code:
  1. The code value is used for I18n translation (e.g. nationality_code: 'GER' -> nationality: 'Germany' (when locale is 'en')).
  1. The code value is used to lookup a specific code object (code objects are not persisten - at least not AR persisted).
  1. The code value is a foreign key on a specific AR code object (code objects are persisted).

#### Lookup through I18n

Example

    class Person
      iclude CodeBox::CodeAttribute

      attr_accessor  :nationality_code

      code_attribute :nationality
    end

The include will create the following method in Person:

  `#nationality` Will return the nationality looked up through I18n on key: `activerecord.values.person.nationality_code.de: Germany`, where de would 'de' the nationality code (Note: The 'activerecord' keyelement is named to accroding AR localization).



#### Lookup through code object

Example

    class Person
      iclude CodeBox::CodeAttribute

      attr_accessor :nationality_code

      code_attribute :nationality, :lookup_type => :lookup
    end

    class Code::Nationality
      attr_accessor :code, :name

      def self.lookup(code)
        return the correct Code::Nationality for the passed code
      end
    end

The include will create the following method in Person:

  `#nationality` Will return the nationality looked through lookup on the associated code object.



#### Lookup through associated AR Code Object

The code value is interpreted as a foreign key on an associated AR Code object.

    class Person < ActiveRecord::Base
      iclude CodeBox::CodeAttribute

      code_attribute :nationality, :lookup_type => :activerecord
    end

    class Code::Nationality < ActiveRecord::Base
      # has attribute 'code' of type string
    end

The include and code specification will create the following methods in Person:

  `#nationality` - will return the nationality looked up through AR association on the associated code object - implemented through below AR association:

      belongs_to :nationality,
        :class_name  => 'Codes::Nationality',
        :foreign_key => :nationality_code,
        :primary_key => :code



## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
<commit_msg>Improved readme.<commit_after># CodeBox::CodeAttribute

Lets you define attributes as codes, instead keys (ids). For simple option storage saving a string code is often more simple an conveniant the storing an artificial id-key referencing a special code object.
CodeBox lets you access define codes as strings and access the associated code objects in various ways.

## Installation

Add this line to your application's Gemfile:

    gem 'code-box'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install code-box

## Usage

### Specifying attributes as codes

There are cases you want to store 'named codes' instead artificial keys.
Codes make sense for stable references and better readability of the raw data.

There are several options to specify an attribute as a code:
  1. The code value is used for I18n translation (e.g. nationality_code: 'GER' -> nationality: 'Germany' (when locale is 'en')).
  1. The code value is used to lookup a specific code object (code objects are not persisten - at least not AR persisted).
  1. The code value is a foreign key on a specific AR code object (code objects are persisted).

#### Lookup through I18n

Example

    class Person
      iclude CodeBox::CodeAttribute

      attr_accessor  :nationality_code

      code_attribute :nationality
    end

The include will create the following method in Person:

  `#nationality` Will return the nationality looked up through I18n on key: `activerecord.values.person.nationality_code.de: Germany`, where de would 'de' the nationality code (Note: The 'activerecord' keyelement is named to according AR localization pattern).



#### Lookup through code object

Example

    class Person
      iclude CodeBox::CodeAttribute

      attr_accessor :nationality_code

      code_attribute :nationality, :lookup_type => :lookup
    end

    class Code::Nationality
      attr_accessor :code, :name

      def self.lookup(code)
        return the correct Code::Nationality for the passed code
      end
    end

The include will create the following method in Person:

  `#nationality` Will return the nationality looked through lookup on the associated code object.



#### Lookup through associated AR Code Object

The code value is interpreted as a foreign key on an associated AR Code object.

    class Person < ActiveRecord::Base
      iclude CodeBox::CodeAttribute

      code_attribute :nationality, :lookup_type => :activerecord
    end

    class Code::Nationality < ActiveRecord::Base
      # has attribute 'code' of type string
    end

The include and code specification will create the following methods in Person:

  `#nationality` - will return the nationality looked up through AR association on the associated code object - implemented through below AR association:

      belongs_to :nationality,
        :class_name  => 'Codes::Nationality',
        :foreign_key => :nationality_code,
        :primary_key => :code



## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

{'fraction_non_alphanumeric': 0.06180217015253971, 'fraction_numerical': 0.0044032080515804374, 'mean_word_length': 3.676470588235294, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, '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': '1932741', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1594, 'n_words': 798}