{"id":24698660,"url":"https://github.com/faradayio/charisma","last_synced_at":"2025-03-22T03:42:06.788Z","repository":{"id":59152008,"uuid":"1509427","full_name":"faradayio/charisma","owner":"faradayio","description":"Define attribute curation and display strategies for your rich Ruby classes","archived":false,"fork":false,"pushed_at":"2012-09-06T19:16:32.000Z","size":203,"stargazers_count":3,"open_issues_count":2,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-13T04:59:53.968Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/faradayio.png","metadata":{"files":{"readme":"README.markdown","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2011-03-22T01:18:34.000Z","updated_at":"2020-09-10T18:52:55.000Z","dependencies_parsed_at":"2022-09-13T11:00:58.276Z","dependency_job_id":null,"html_url":"https://github.com/faradayio/charisma","commit_stats":null,"previous_names":["brighterplanet/charisma"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faradayio%2Fcharisma","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faradayio%2Fcharisma/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faradayio%2Fcharisma/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/faradayio%2Fcharisma/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/faradayio","download_url":"https://codeload.github.com/faradayio/charisma/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244902936,"owners_count":20529114,"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-01-27T04:29:42.124Z","updated_at":"2025-03-22T03:42:06.764Z","avatar_url":"https://github.com/faradayio.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Charisma\n\nCharisma provides a *superficiality framework* for Ruby objects. You can use it to:\n\n* Provide a *curation strategy* for your class that defines which of its attributes are *superfically important.*\n* Define *metadata* on these characteristics, such as measurements and units.\n* Facilitate *appropriate presentation* of these characteristics (i.e., intelligent `#to_s`).\n\nCharisma is all about how your objects look to the outside world. We use it within [CM1](http://carbon.brighterplanet.com) to facilitate the characterization of the flights, cars, etc. that we're performing carbon calculations on.\n\nSee also: [RDocs](http://rubydoc.info/github/brighterplanet/charisma/master/frames).\n\n[![Build Status](https://secure.travis-ci.org/brighterplanet/charisma.png)](http://travis-ci.org/brighterplanet/charisma)\n\n## Example\n\n``` ruby\nclass Spaceship \u003c SuperModel::Base # or ActiveRecord, or whatever\n  attributes :window_count, :name, :size, :weight\n  belongs_to :make, :class_name =\u003e 'SpaceshipMake', :primary_key =\u003e 'name'\n  belongs_to :fuel, :class_name =\u003e 'SpaceshipFuel', :primary_key =\u003e 'name'\n  belongs_to :destination, :class_name =\u003e 'Planet', :primary_key =\u003e 'name'\n  \n  include Charisma\n  characterize do\n    has :make, :display_with =\u003e :name\n    has :fuel\n    has :window_count do |window_count|\n      \"#{window_count} windows\"\n    end\n    has :size, :measures =\u003e :length # uses Charisma::Measurements::Length\n    has :weight, :measures =\u003e RelativeAstronomicMass\n    has :name\n    has :destination\n  end\nend\n```\n``` irb\nirb(main):001:0\u003e amaroq = Spaceship.new :size =\u003e 100, :window_count =\u003e 3, :make =\u003e SpaceshipMake.create(:name =\u003e 'Ford')\n =\u003e #\u003cSpaceship:0xacd9ac4 ...\u003e\nirb(main):002:0\u003e amaroq.characteristics[:size].to_s\n =\u003e \"100 m\"\nirb(main):003:0\u003e amaroq.characteristics[:size].feet\n =\u003e 328.08399000000003\nirb(main):004:0\u003e amaroq.characteristics[:window_count].to_s\n =\u003e \"3 windows\"\nirb(main):005:0\u003e amaroq.characteristics[:make].to_s\n =\u003e \"Ford\"\n```\n\n## Characterizing your class\n\nCharisma uses a DSL within a `characterize` block to define your class's attribute curation strategy:\n\n``` ruby\nclass Foo\n  # ...\n  include Charisma\n  characterize do\n    # Characteristics go here\n  end\nend\n```\n\n### Simple (scalar) characteristics\n\nHow do you define characteristics? The simplest way looks like this:\n\n``` ruby\ncharacterize do\n  has :color\nend\n```\n\nCharisma will obtain the `color` characteristic for an instance by calling its `#color` method.\n\n``` irb\nirb(main):001:0\u003e Foo.new(:color =\u003e 'Blue').characteristics[:color].to_s\n =\u003e Blue\n```\n\n### Fancy characteristics\n\nOf course, this isn't extremely useful. Things get more interesting when you want the characteristic to present itself usefully:\n\n``` ruby\ncharacterize do\n  has :color do |color|\n    \"##{Color.find_by_name(color).hex}\"\n  end\nend\n```\n``` irb\nirb(main):001:0\u003e Foo.new(:color =\u003e 'black').characteristics[:color].to_s\n =\u003e #000000\n```\n\nOr perhaps you're using an association:\n\n``` ruby\ncharacterize do\n  has :color, :display_with =\u003e :hex # If unspecified, Charisma will try #as_characteristic and #to_s on the associated object, in that order \nend\n```\n``` irb\nirb(main):001:0\u003e Foo.new(:color =\u003e Color.find_by_name('black')).characteristics[:color].to_s\n =\u003e #000000\n```\n\n### Measured characteristics\n\nSome characteristics represent measured values like *length*. You can specify that like so:\n\n``` ruby\ncharacterize do\n  has :size, :measures =\u003e Length \nend\n```\n``` ruby\nclass Length \u003c Charisma::Measurement # Don't need to inherit if you want to DIY\n  units :metres =\u003e 'm' \nend\n```\n``` irb\nirb(main):001:0\u003e Foo.new(:size =\u003e 10).characteristics[:size].to_s\n =\u003e 10 m\n```\n\nCharisma has some [convenient measurements](https://github.com/brighterplanet/charisma/tree/master/lib/charisma/measurement) baked in:\n\n``` ruby\ncharacterize do\n  has :size, :measures =\u003e Charisma::Measurement::Length \nend\n```\n\nBuilt-in measurements can be referenced with a shortcut:\n\n``` ruby\ncharacterize do\n  has :size, :measures =\u003e :length \nend\n```\n\nCharisma uses [Conversions](https://github.com/seamusabshere/conversions) to provide useful unit conversion methods:\n\n``` irb\nirb(main):001:0\u003e Foo.new(:size =\u003e 10).characteristics[:size].feet\n =\u003e 32.808399000000003\n```\n\n## JSON\n\nCharisma now provides `#as_json` for characteristics---convert to actual JSON with your favorite JSON library's `#to_json`.\n\n## Copyright\n\nCopyright (c) 2011 Andy Rossmeissl. See LICENSE for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaradayio%2Fcharisma","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffaradayio%2Fcharisma","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffaradayio%2Fcharisma/lists"}