{"id":28509103,"url":"https://github.com/atomicobject/diy","last_synced_at":"2025-07-02T23:31:27.572Z","repository":{"id":740134,"uuid":"390840","full_name":"atomicobject/diy","owner":"atomicobject","description":"Constructor-based dependency injection container using YAML input.","archived":false,"fork":false,"pushed_at":"2024-04-11T19:14:18.000Z","size":611,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-06-06T07:03:47.877Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"atomicobject.github.com","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/atomicobject.png","metadata":{"files":{"readme":"README.rdoc","changelog":"History.txt","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2009-11-30T22:39:30.000Z","updated_at":"2024-04-11T19:13:53.000Z","dependencies_parsed_at":"2022-07-18T12:48:04.419Z","dependency_job_id":null,"html_url":"https://github.com/atomicobject/diy","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/atomicobject/diy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicobject%2Fdiy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicobject%2Fdiy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicobject%2Fdiy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicobject%2Fdiy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/atomicobject","download_url":"https://codeload.github.com/atomicobject/diy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/atomicobject%2Fdiy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263232552,"owners_count":23434692,"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":"2025-06-08T22:07:28.261Z","updated_at":"2025-07-02T23:31:27.541Z","avatar_url":"https://github.com/atomicobject.png","language":"Ruby","readme":"== DIY\n\n* http://atomicobject.github.com/diy\n\n== Desciption\n\nDIY (Dependency Injection in YAML) is a simple dependency injection library\nwhich focuses on declarative composition of objects through constructor injection.\n\n== Install\n\n* gem install diy\n\n== Synopsis\n\n=== Common Usage\n\nAuthor a YAML file that describes your objects and how they fit together.\nThis means you're building a Hash whose keys are the object names, and whose\nvalues are Hashes that define the object. \n\nThe following context defines an automobile engine:\n\ncontext.yml:\n  ---\n  engine:\n    compose: throttle, block\n  throttle:\n    compose: cable, pedal\n  block:\n  cable:\n  pedal:\n\nIn your code, use DIY to load the YAML, then access its parts:\n\n  context = DIY::Context.from_file('context.yml')\n  context[:engine]  =\u003e \u003cEngine:0x81eb0\u003e \n\nThis approach assumes:\n\n* You've got classes for Engine, Throttle, Block, Cable and Pedal\n* They're defined in engine.rb, throttle.rb, etc\n* The library files are in your load-path\n* Engine and Throttle both have a constructor that accepts a Hash.  The Hash \n  will contain keys 'throttle', 'block' (for Engine) and 'cable, 'pedal' (for Throttle)\n  and the values will be references to their respective objects.\n* Block, Cable and Pedal all have default constructors that accept no arguments\n\nSample code for Engine's constructor:\n\n  class Engine\n    def initialize(components)\n      @throttle = components['throttle']\n      @block = components['block']\n    end\n  end\n\nWriting code like that is repetetive; that's why we created the Constructor gem, which lets you\nspecify object components using the \"constructor\" class method:\n\nUsing constructor, you can write Engine like this:\n\n  class Engine\n    constructor :throttle, :block\n  end\n\n=== Special Cases\n\nIf your object has a lot of components (or they have big names) you can specify an array of component names\nas opposed to a comma-separated list:\n\n  engine:\n    compose:\n    - throttle\n    - block\n\nSometimes you won't be able to rely on DIY's basic assumptions about class names and library files.\n\n* You can specify the 'class' option\n* You can specify the 'library' option.  If you do not, the library is inferred from the class name.\n  (Eg, My::Train::Station will be sought in \"my/train/station.rb\"\n\n  engine:\n    class: FourHorse::Base\n    library: general_engines/base\n    compose: throttle, block\n\nIf the Hash coming into your constructor needs to have some keys that do not exactly match the official\nobject names, you can specify them one-by-one:\n\n  engine:\n    the_throttle: throttle\n    the_block: block\n\n=== Non-singleton objects\n\nNon-singletons are named objects that provide a new instance every time you ask for them.\nBy default, DIY considers all objects to be singletons.  To override, use the \"singleton\" setting and\nset it to false:\n\n  foo:\n    singleton: false\n\n=== Sub-Contexts\n\nSub-contexts are useful for creating isolated object networks that may need to be instantiated\nzero or many times in your application.  Objects defined in subcontexts can reference \"upward\" to\ntheir surroundings, as well as objects in the subcontext itself.\n\nIf you wanted to be able to make more than one Engine from the preceding examples, you might try:\n\n  ---\n  epa_regulations:\n\n  +automotive_plant:\n    engine:\n      compose: block, throttle, epa_regulations\n    block:\n    throttle:\n\nEach time you delve into the automotive_plant, you get a solar system of the defined objects.\nIn this context, the objects are singleton-like.  The next time you invoke the subcontext, however,\nyou'll be working with a fresh set of objects... another solar system with the same layout, so to speak.\n\nSubcontexts are not initialized until you call upon them, which you do using the \"within\" method:\n\n  context = DIY::Context.from_file('context.yml')\n  context.within('automotive_plant') do |plant|\n    puts plant[:engine]\n  end\n\n=== Direct Class References\n\nOccasionally you will have a class at your disposal that you'd like to provide directly as components\nto other objects (as opposed to getting _instances_ of that class, you want to reference the class itself, eg,\nto use its factory methods).  Enter the \"use_class_directly\" flag:\n\n  ---\n  customer_order_finder:\n    class: CustomerOrder\n    use_class_directly: true\n\nThis can be handy in Rails when you'd like to use some of class methods on an ActiveRecord subclass, but \nyou'd like to avoid direct ActiveRecord class usage in your code.  In this case, the customer_order_finder\nis actually the CustomerOrder class, and so, it has methods like \"find\" and \"destroy_all\".\n\n=== Namespace Convenience\n\nIf you find yourself writing context entries like this:\n\n  ---\n  engine:\n    class: Car::Parts::Engine\n  throttle:\n    class: Car::Parts::Block\n  cable:\n    class: Car::Parts::Cable\n\nYou can set the \"assumed\" module for a group of objects like this:\n\n  ---\n  using_namespace Car Parts:\n    engine:\n\n    throttle:\n\n    block:\n\n=== Preventing auto-requiring of library files\n\nNormally, DIY will \"require\" the library for an object just before it instantiates the object.\nIf this is not desired (in Rails, auto-require can lead to library double-load issues), you\ncan deactivate auto-require.  There is a global default setting (handled in code) and\na per-object override (handled in the context YAML):\n\n  DIY::Context.auto_require = false\n\n  ---\n  engine:\n    auto_require: false\n\n=== Factories\n\nIt is possible to create factories automatically with DIY:\n\n  ---\n  car_dealer:\n    compose: car_factory\n\n  car_factory:\n    builds: car\n\nThen you can use the factory to easily build objects:\n\n  context = DIY::Context.from_file('context.yml')\n  context[:car_factory].create  =\u003e \u003cCar:0x81eb0\u003e\n\n=== Method Directive\n\nThis introduces the concept of first class methods. An object can now be constructed with a method object\nbound to a particular object in the diy context.\n\n  ---\n  trinket_builder:\n\n  method build_trinket:\n      object: trinket_builder\n      method: build\n\n\n== Authors\n\n* David Crosby (crosby@atomicobject.com)\n* © 2007-2024 {Atomic Object}[http://www.atomicobject.com]\n* More Atomic Object {open source}[http://www.atomicobject.com/pages/Software+Commons] projects\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomicobject%2Fdiy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fatomicobject%2Fdiy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fatomicobject%2Fdiy/lists"}