{"id":20904667,"url":"https://github.com/sage/class_kit","last_synced_at":"2025-05-13T05:30:43.715Z","repository":{"id":10382759,"uuid":"65566838","full_name":"Sage/class_kit","owner":"Sage","description":null,"archived":false,"fork":false,"pushed_at":"2025-02-28T11:13:36.000Z","size":81,"stargazers_count":2,"open_issues_count":3,"forks_count":2,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-04-18T18:46:49.156Z","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/Sage.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2016-08-12T16:20:01.000Z","updated_at":"2025-04-11T11:51:01.000Z","dependencies_parsed_at":"2025-01-03T17:31:41.716Z","dependency_job_id":"16776422-16f7-4a4a-9e0c-899de979f5c3","html_url":"https://github.com/Sage/class_kit","commit_stats":{"total_commits":38,"total_committers":6,"mean_commits":6.333333333333333,"dds":0.3157894736842105,"last_synced_commit":"86db59222cd9367939aaf92d8131f3999d4ee22a"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fclass_kit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fclass_kit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fclass_kit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fclass_kit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sage","download_url":"https://codeload.github.com/Sage/class_kit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253882687,"owners_count":21978535,"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-18T13:18:26.636Z","updated_at":"2025-05-13T05:30:43.704Z","avatar_url":"https://github.com/Sage.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ClassKit\n\n[![RSpec](https://github.com/Sage/class_kit/actions/workflows/rspec.yml/badge.svg)](https://github.com/Sage/class_kit/actions/workflows/rspec.yml)\n[![Maintainability](https://api.codeclimate.com/v1/badges/0bc83e414eed8759a0e8/maintainability)](https://codeclimate.com/github/Sage/class_kit/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/0bc83e414eed8759a0e8/test_coverage)](https://codeclimate.com/github/Sage/class_kit/test_coverage)\n[![Gem Version](https://badge.fury.io/rb/class_kit.svg)](https://badge.fury.io/rb/class_kit)\n\nWelcome to your ClassKit! ClassKit is a toolkit for working with entities \u0026 classes.\n\n## Installation\n\nAdd this line to your application's Gemfile: \n\n```ruby\ngem 'class_kit'\n```\n\nAnd then execute:\n\n```bash\n$ bundle\n```\n\nOr install it yourself as:\n\n```bash\n$ gem install class_kit\n```\n\n## Usage\n\n### Creating an entity\n\n```ruby\nclass Contact\n  extend ClassKit\n\n  attr_accessor_type :landline, type: String\n  attr_accessor_type :mobile, type: String\n  attr_accessor_type :email, type: String\nend\n\nclass Address\n  extend ClassKit\n\n  attr_accessor_type :line1, type: String\n  attr_accessor_type :line2\n  attr_accessor_type :postcode\nend\n\nclass Employee\n  extend ClassKit\n\n  attr_accessor_type :name, type: String\n  attr_accessor_type :dob, type: Date\n  attr_accessor_type :address, type: Address, auto_init: true\n  attr_accessor_type :contacts, type: Array, collection_type: Contact, auto_init: true\nend\n```\n\nClassKit entities can be created by implementing the `extend ClassKit` extend into the entity class and then using the `attr_accessor_type` method to register attributes as above inplace of the standard ruby `attr_accessor` method.\n\nClassKit entity attributes can use a name alias in order to parse to/from hashes/json with different key names, see the example below:\n\n```ruby\nclass Address\n  extend ClassKit\n\n  attr_accessor_type :line1, type: String, alias_name: :l1\n  attr_accessor_type :line2, alias_name: :l2\n  attr_accessor_type :postcode, alias_name: :pc\nend\n```\n\n```json\n{ \"l1\": \"23 the street\", \"l2\": \"the town\", \"pc\": \"ne1 4rt\" }\n```\n\nTo use alias names you must specify `use_alias = true` for the following helper methods:\n\n```ruby\nhelper.to_hash(entity, true)\nhelper.from_hash(hash: hash_object, klass: entity_klass, use_alias: true)\n\nhelper.to_json(entity, true)\nhelper.from_json(json: json_string, klass: entity_klass, use_alias: true)\n```\n\n### attr_accessor_type\n\nThis method is used to add typed attributes to a class.\n\nSupported standard types:\n\n- String\n- Integer\n- Float\n- BigDecimal\n- Date\n- DateTime\n- Time\n- :bool (true/false)\n- Regexp\n\nThe above supported standard types will attempt to parse any values passed to the attribute if the value is not of the same type as the attribute.\n\nExample:\n\n```\nentity.dob = '03-JUN-1980'\n```\n\nWould be parsed into a `Date` object and set to the attribute, so that subsequent calls to get the value from the attribute would return a `Date` object for the `entity.dob` value.\n\nAttributes can have any type specified, they are not limited to the standard types listed above, however only the standard types listed above will attempt to parse non type matching values when setting the attribute value.\n\nIf an invalid value is passed to an attribute it will raise a `ClassKit::Exceptions::InvalidAttributeValueError` exception.\n\nAttributes don't require the `type:` argument to be specified for attributes where type information is variable or not required.\n\n#### Arrays\n\nThe `attr_accessor_type` method allows attributes of type `Array` to be specified, these attributes can also specify the `collection_type:` argument to specify what the type the elements of the Array will be.\n\n#### Custom types\n\nClassKit offers the CustomType module, which can be included to define attributes as custom types, while controlling how the serialisation and deserialisation of its values are handled via the implementation of the included methods.\n\n#### Additional Arguments\n\nThe below are additional arguments that can be specified when using the `attr_accessor_type` method.\n\n- `allow_nil:` This argument is used to specify if an attribute is allowed to have nil set as it's value.\n- `auto_init:` This argument is used to specify an attribute should auto initialise it's value when nil. (Useful for Array's and Nested entities)\n- `default:` This argument is used to specify a default value that the attribute should be set to when nil.\n- `meta:` This argument is used to specify any additional meta information you want to attach to the attribute.\n\n### ClassKit::Helper\n\nThis helper class provides several useful helper methods for working with ClassKit entities.\n\n#### #is_class_kit?(object)\n\nThis method is called to determine if an object is a ClassKit entity or not.\n\n[Params]\n- `object` This is the object to check.\n\n[Return]\n\n`true` or `false`\n\nExample:\n\n```ruby\nhelper.is_class_kit?(obj)\n```\n\n#### #to_hash(object, use_alias)\n\nThis method is called to convert a ClassKit entity into a `Hash`.\n\n[Params]\n- `object` [Required] This is the ClassKit entity to convert.\n- `use_alias` [Optional] [Default=false] This is used to specify if attribute alias names should be used.\n\n[Return]\n\n`Hash`\n\nExample:\n\n```ruby\nhash = helper.to_hash(obj)\n```\n\n#### #from_hash(hash:,klass:, use_alias:)\n\nThis method is called to convert a `Hash` into a ClassKit entity.\n\n[Params]\n- `hash:` [Required] This is the `Hash` to convert.\n- `klass:` [Required] This is the class of the ClassKit entity you want to convert the `Hash` into.\n\u003e NOTE: It should be the fully qualified class name including modules\n- `use_alias` [Optional] [Default=false] This is used to specify if attribute alias names should be used.\n\n[Return]\n\nClassKit entity.\n\nExample:\n\n```ruby\nentity = helper.from_hash(hash: hsh, klass: Contact)\n```\n\n#### #to_json(object, use_alias)\n\nThis method is called to convert a ClassKit entity into JSON.\n\n[Params]\n- `object` This is the ClassKit entity to convert.\n- `use_alias` [Optional] [Default=false] This is used to specify if attribute alias names should be used.\n\n[Return]\n\nJSON string.\n\nExample:\n\n```ruby\njson_string = helper.to_json(obj)\n```\n\n#### #from_json(json:, klass:, use_alias:)\n\nThis method is called to convert a JSON string into a ClassKit entity.\n\n[Params]\n- `json:` This is the JSON string to convert.\n- `klass:` This is the class of the ClassKit entity you want to convert the JSON into.\n\u003e NOTE: It should be the fully qualified class name including modules\n- `use_alias` [Optional] [Default=false] This is used to specify if attribute alias names should be used.\n\n[Return]\n\nClassKit entity.\n\nExample:\n\n```ruby\nentity = helper.from_json(json: json_string, klass: Contact)\n```\n\nNOTE: This method will parse any nested Hashes that match attributes specified with a type: that is a ClassKit entity, as well as populating `Arrays` where the `collection_type:` has been specified as a ClassKit entity.\n\nExample:\n\n```json\n{\"name\":\"Joe Bloggs\",\"dob\":\"03-JUNE-1980\",\"address\":{\"line1\":\"25 The Street\",\"line2\":\"Home Town\",\"postcode\":\"NE3 5RT\"},\"contacts\":[{\"landline\":\"01235456789\",\"mobile\":\"0789456123\",\"email\":\"joe.bloggs@test.com\"}]}\n```\n\nWould be parsed into the ClassKit `Employee` class defined above along with the nested `Address` attribute and `Contact` array.\n\nAllowing the `Address` to be accessed via `entity.address.line1` etc and the `Contact` details to be accessed via `entity.contacts[0].landline` etc.\n\n\n### ClassKit::AttributeHelper\n\nThis helper class provides several useful methods for accessing the attribute details for a ClassKit entity.\n\n#### #get_attribute_type(klass:, name:)\n\nThis method is called to get the type of a specific attribute of a ClassKit entity.\n\n[Params]\n- `klass:` This is the ClassKit entity class that contains the attribute.\n- `name:` This is the name of the attribute to get the type for.\n\n[Return]\n\n`Type`\n\n#### #get_attribute(klass:, name:)\n\nThis method is called to get the details of a specific attribute of a ClassKit entity.\n\n[Params]\n- `klass:` This is the ClassKit entity class that contains the attribute.\n- `name:` This is the name of the attribute to get the details for.\n\n[Return]\n\n`Hash` of `{ name:, type:, collection_type:, allow_nil:, default:, auto_init:, meta: }`\n\n#### #get_attributes(klass)\n\nThis method is called to get an array of the Attribute details for a specified ClassKit entity.\n\n[Params]\n- `klass` This is the ClassKit entity class to get the attribute details for.\n\n[Return]\n\n`Array` of `{ name:, type:, collection_type:, allow_nil:, default:, auto_init:, meta: }`\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/sage/class_kit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThis gem is available as open source under the terms of the\n[MIT licence](LICENSE).\n\nCopyright (c) 2018 Sage Group Plc. All rights reserved.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsage%2Fclass_kit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsage%2Fclass_kit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsage%2Fclass_kit/lists"}