{"id":16746695,"url":"https://github.com/mkon/d12n","last_synced_at":"2025-04-10T13:43:32.960Z","repository":{"id":33135216,"uuid":"110142161","full_name":"mkon/d12n","owner":"mkon","description":"Very basic attribute delocalization","archived":false,"fork":false,"pushed_at":"2024-03-12T23:50:07.000Z","size":56,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-03-15T10:11:47.454Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mkon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2017-11-09T16:53:42.000Z","updated_at":"2024-04-17T16:49:01.918Z","dependencies_parsed_at":"2023-01-14T23:33:54.777Z","dependency_job_id":"49810cac-6fe6-4b49-a031-39179f215b7e","html_url":"https://github.com/mkon/d12n","commit_stats":{"total_commits":56,"total_committers":6,"mean_commits":9.333333333333334,"dds":0.5714285714285714,"last_synced_commit":"6b39b0a2086e36e5c58a9b9b50ee2e0f709e2d92"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkon%2Fd12n","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkon%2Fd12n/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkon%2Fd12n/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mkon%2Fd12n/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mkon","download_url":"https://codeload.github.com/mkon/d12n/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248226355,"owners_count":21068187,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T02:08:09.130Z","updated_at":"2025-04-10T13:43:32.941Z","avatar_url":"https://github.com/mkon.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# D12n\n\n[![Gem Version](https://badge.fury.io/rb/d12n.svg)](https://badge.fury.io/rb/d12n)\n[![Push \u0026 PR](https://github.com/mkon/d12n/actions/workflows/main.yml/badge.svg)](https://github.com/mkon/d12n/actions/workflows/main.yml)\n\nD12n (=delocalization) can be used to cast model attributes from localized format to an internal BigDecimal or Integer\nrepresentation. This is done by adding an additional attribute to the model which keeps the original value the user has\nentered. This has the advantage that the original user input can be validated and rendered back to the user during\nvalidation errors.\n\n## Usage\n\n```ruby\ngem 'd12n'\n```\n\nSample model\n\n```ruby\nclass Dummy\n  include D12n::ModelSupport\n\n  attr_accessor :amount\n  d12n_attribute :amount\nend\n```\n\n```ruby\nd = Dummy.new\n\nd.amount = 1_234.56\nd.local_amount # \"1,234.56\"\n\nd.local_amount = '3,456.78'\nd.amount.class # BigDecimal\nd.amount.to_f # 3456.78\n\nd.local_amount = 'invalid'\nd.amount.to_f # 3456.78, did not change\n```\n\n## Configuration\n\n```ruby\n# the default, no need to set it\n# It will use the settings from your I18n locale (number.format) to parse and generate the formatted number\n# It is heavily recommended to use this\nD12n.config.strategy = D12n::Strategy::Default\n\n# Hard coded strategy using decimal point\nD12n.config.strategy = D12n::Strategy::DecimalPoint\n\n# Hard coded strategy using decimal comma\nD12n.config.strategy = D12n::Strategy::DecimalComma\n```\n\n### Custom method prefix\n\n```ruby\nd12n_attribute :amount, prefix: 'localized'\n```\n\nWould give you `localized_amount` instead of `local_amount`.\n\n### Internal integer representation with a factor\n\nIf your internal representation is for example in cents, but the local format should be EUR with decimal point\nyou can use this option:\n\n```ruby\nd12n_attribute :amount, factor: 100\n```\n\n```ruby\nd = Dummy.new\n\nd.amount = 1_234\nd.local_amount # \"12,34\"\n\nd.local_amount = '3,456.78'\nd.amount.class # Integer\nd.amount # 345_678\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmkon%2Fd12n","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmkon%2Fd12n","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmkon%2Fd12n/lists"}