{"id":19136843,"url":"https://github.com/mdub/config_mapper","last_synced_at":"2025-05-06T20:11:19.556Z","repository":{"id":59152322,"uuid":"40121626","full_name":"mdub/config_mapper","owner":"mdub","description":"Maps config data onto plain old Ruby objects","archived":false,"fork":false,"pushed_at":"2023-02-28T07:55:29.000Z","size":137,"stargazers_count":6,"open_issues_count":2,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-13T13:03:34.949Z","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/mdub.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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,"publiccode":null,"codemeta":null}},"created_at":"2015-08-03T11:40:34.000Z","updated_at":"2023-02-28T07:25:44.000Z","dependencies_parsed_at":"2024-11-09T06:35:48.608Z","dependency_job_id":null,"html_url":"https://github.com/mdub/config_mapper","commit_stats":{"total_commits":173,"total_committers":6,"mean_commits":"28.833333333333332","dds":0.06936416184971095,"last_synced_commit":"f49146d3293436cde333e2f00bb05e8840807c8d"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdub%2Fconfig_mapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdub%2Fconfig_mapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdub%2Fconfig_mapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mdub%2Fconfig_mapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mdub","download_url":"https://codeload.github.com/mdub/config_mapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249664070,"owners_count":21308084,"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-11-09T06:35:40.952Z","updated_at":"2025-04-19T09:31:03.705Z","avatar_url":"https://github.com/mdub.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ConfigMapper\n\n[![Gem Version](https://badge.fury.io/rb/config_mapper.svg)](https://badge.fury.io/rb/config_mapper)\n[![Build Status](https://github.com/mdub/config_mapper/actions/workflows/test.yaml/badge.svg?branch=master)](https://github.com/mdub/config_mapper/actions/workflows/test.yaml)\n\nConfigMapper maps configuration data onto Ruby objects.\n\n\u003c!-- TOC depthFrom:2 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 --\u003e\n\n- [Usage](#usage)\n  - [Target object](#target-object)\n  - [Errors](#errors)\n- [ConfigStruct](#configstruct)\n  - [Attributes](#attributes)\n  - [Type validation/coercion](#type-validationcoercion)\n  - [Defaults](#defaults)\n  - [Semantic errors](#semantic-errors)\n- [License](#license)\n- [Contributing](#contributing)\n- [See also](#see-also)\n\n\u003c!-- /TOC --\u003e\n\n## Usage\n\nImagine you have some Ruby objects:\n\n```ruby\nclass Position\n\n  attr_reader :x\n  attr_reader :y\n\n  def x=(arg); @x = Integer(arg); end\n  def y=(arg); @y = Integer(arg); end\n\nend\n\nclass State\n\n  def initialize\n    @position = Position.new\n  end\n\n  attr_reader :position\n  attr_accessor :orientation\n\nend\n\nstate = State.new\n```\n\nand wish to populate/modify it, based on plain data:\n\n```ruby\nconfig_data = {\n  \"orientation\" =\u003e \"North\",\n  \"position\" =\u003e {\n    \"x\" =\u003e 2,\n    \"y\" =\u003e 4\n  }\n}\n```\n\nConfigMapper will help you out:\n\n```ruby\nrequire 'config_mapper'\n\nerrors = ConfigMapper.configure_with(config_data, state)\nstate.orientation              #=\u003e \"North\"\nstate.position.x               #=\u003e 2\n```\n\n### Target object\n\nGiven\n\n```ruby\nConfigMapper.configure_with(config_data, target)\n```\n\nthe `target` object is expected provide accessor-methods corresponding\nto the attributes that you want to make configurable.  For example, with:\n\n```ruby\nconfig_data = {\n  \"orientation\" =\u003e \"North\",\n  \"position\" =\u003e { \"x\" =\u003e 2, \"y\" =\u003e 4 }\n}\n```\n\nit should have a `orientiation=` method, and a `position` method that\nreturns a `Position` object, which should in turn have `x=` and `y=`\nmethods.\n\nConfigMapper cannot and will not _create_ objects for you.\n\n### Errors\n\n`ConfigMapper.configure_with` returns a Hash of errors encountered while mapping data onto objects.  The errors are Exceptions (typically ArgumentError or NoMethodError), keyed by the path to the offending data.  e.g.\n\n```ruby\nconfig_data = {\n  \"position\" =\u003e {\n    \"bogus\" =\u003e \"flibble\"\n  }\n}\n\nerrors = ConfigMapper.configure_with(config_data, state)\nerrors    #=\u003e { \".position.bogus\" =\u003e #\u003cNoMethodError\u003e }\n```\n\n## ConfigStruct\n\nConfigMapper works pretty well with plain old Ruby objects, but we\nprovide a base-class, `ConfigMapper::ConfigStruct`, with a DSL that\nmakes it even easier to declare configuration data-structures.\n\n### Attributes\n\nThe `attribute` method is similar to `attr_accessor`, defining both reader and writer methods for the named attribute.\n\n```ruby\nrequire \"config_mapper/config_struct\"\n\nclass State \u003c ConfigMapper::ConfigStruct\n\n  attribute :orientation\n\nend\n```\n\n### Type validation/coercion\n\nIf you specify a block when declaring an attribute, it will be invoked as part of the attribute's writer-method, to validate values when they are set. It should expect a single argument, and raise `ArgumentError` to signal invalid input. As the return value will be used as the value of the attribute, it's also an opportunity coerce values into canonical form.\n\n```ruby\nclass Server \u003c ConfigMapper::ConfigStruct\n\n  attribute :host do |arg|\n    unless arg =~ /^\\w+(\\.\\w+)+$/\n      raise ArgumentError, \"invalid hostname: #{arg}\"\n    end\n    arg\n  end\n\n  attribute :port do |arg|\n    Integer(arg)\n  end\n\nend\n```\n\nAlternatively, specify a \"validator\" as a second argument to `attribute`.  It should be an object that responds to `#call`, with the same semantics described above. Good choices include `Proc` or `Method` objects, or type-objects from the [dry-types](http://dry-rb.org/gems/dry-types/) project.\n\n```ruby\nclass Server \u003c ConfigMapper::ConfigStruct\n\n  attribute :host, Types::Strict::String.constrained(format: /^\\w+(\\.\\w+)+$/)\n  attribute :port, method(:Integer)\n\nend\n```\n\nFor convenience, primitive Ruby types such as `Integer` and `Float` can be used as shorthand for their namesake type-coercion methods on `Kernel`:\n\n```ruby\nclass Server \u003c ConfigMapper::ConfigStruct\n\n  attribute :port, Integer\n\nend\n```\n\n### Defaults\n\nAttributes can be given default values, e.g.\n\n```ruby\nclass Address \u003c ConfigMapper::ConfigStruct\n  attribute :host\n  attribute :port, :default =\u003e 80\n  attribute :path, :default =\u003e nil\nend\n```\n\nSpecify a default value of `nil` to mark an attribute as optional. Attributes without a default are treated as \"required\".\n\n### Sub-components\n\nThe `component` method defines a nested component object, itself a `ConfigStruct`.\n\n```ruby\nclass State \u003c ConfigMapper::ConfigStruct\n\n  component :position do\n    attribute :x\n    attribute :y\n  end\n\nend\n```\n\n`component_list` declares a nested list of configurable objects, indexed by position.\n\n```ruby\nclass Polygon \u003c ConfigMapper::ConfigStruct\n\n  component_list :points do\n    attribute :x\n    attribute :y\n  end\n\nend\n\n```\n\n`component_dict` declares a dictionary (map) of configurable objects, indexed by an arbitrary key.\n\n```ruby\nclass Cargo \u003c ConfigMapper::ConfigStruct\n\n  component_dict :packages do\n    attribute :contents\n    attribute :weight, Float\n  end\n\nend\n```\n\nIn both cases, new collection entries pop into existance the first time they are accessed.\n\n### Semantic errors\n\n`ConfigStruct#config_errors` returns errors for each unset mandatory attribute.\n\n```ruby\nstate = State.new\nstate.position.x = 3\nstate.position.y = 4\nstate.config_errors\n#=\u003e { \".orientation\" =\u003e #\u003cConfigMapper::ConfigStruct::NoValueProvided: no value provided\u003e }\n```\n\n`#config_errors` can be overridden to provide custom semantic validation.\n\n`ConfigStruct#configure_with` maps data into the object, and combines mapping errors and semantic errors (returned by `#config_errors`) into a single Hash:\n\n```ruby\ndata = {\n  \"position\" =\u003e { \"x\" =\u003e 3, \"y\" =\u003e \"fore\" },\n  \"bogus\" =\u003e \"foobar\"\n}\nstate.configure_with(data)\n#=\u003e {\n#=\u003e   \".orientation\" =\u003e \"no value provided\",\n#=\u003e   \".position.y\" =\u003e #\u003cArgumentError: invalid value for Integer(): \"fore\"\u003e,\n#=\u003e   \".bogus\" =\u003e #\u003cNoMethodError: undefined method `bogus=' for #\u003cState:0x007fc8e9b12a60\u003e\u003e\n#=\u003e }\n```\n\n`ConfigStruct.from_data` instantiates an object from data, raising an exception if errors are encountered:\n\n```ruby\nstate = State.from_data(data)\n```\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n## Contributing\n\nIt's on GitHub; you know the drill.\n\n## See also\n\n* [ConfigHound](https://github.com/mdub/config_hound) is a great way to\n  load raw config-data, before throwing it to ConfigMapper.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdub%2Fconfig_mapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmdub%2Fconfig_mapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmdub%2Fconfig_mapper/lists"}