npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-computed-enum

v0.1.1

Published

Creates and manges Enums using Ember computed properties.

Downloads

12

Readme

ember-computed-enum

ember-computed-enum translates an field containing a "magic number" into it's human-readable analogue, and provides named properties to easily check for any given state.

Installation

ember install ember-computed-enum

Usage

The addon can be used in two different ways.

For full functionality, use the addon...

as a Mixin (with enumNameIsValue properties)

import { computedEnumMixin } from 'ember-computed-enum';

myEnumValues = ['foo', 'bar', 'baz'];

MyClassWithAnEnum = Ember.Object.extend(
  computedEnumMixin('myEnum', 'enumValRaw', myEnumValues),
  {
    // ... your other properties
  }
);

o = MyClassWithAnEnum.create({ enumValRaw: 0 });
o.get('enumValRaw'); // -> 0
o.get('myEnum');  // -> 'foo'

o.get('myEnumIsFoo');  // -> true
o.get('myEnumIsBar');  // -> false
o.get('myEnumIsBaz');  // -> false

In cases where the 'enumNameIsValue` properties aren't needed, the addon can also be used...

as a simple property (without enumNameIsValue properties)

import { computedEnum } from 'ember-computed-enum';

myEnumValues = ['foo', 'bar', 'baz'];

MyClassWithAnEnum = Ember.Object.extend({
  myEnum: computedEnum('enumValRaw', myEnumValues)
});


o = MyClassWithAnEnum.create({ enumValRaw: 0 });
o.get('enumValRaw'); // -> 0
o.get('myEnum');  // -> 'foo'

o.get('myEnumIsFoo');  // -> undefined
o.get('myEnumIsBar');  // -> undefined
o.get('myEnumIsBaz');  // -> undefined

Models

The addon works with any Ember Objects, including ember-data models. To use with Ember models, store the raw value of the enum in one field:

// app/models/person.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  genderCode: DS.attr('number')
});

Then add a computed enum that references the raw value:

// app/models/person.js

import DS from 'ember-data';

import { computedEnumMixin } from 'ember-computed-enum';

export default DS.Model.extend(
  computedEnumMixin('gender', 'genderCode', ['male', 'female', 'other']),
  {
    name: DS.attr('string'),
    genderCode: DS.attr('number')
  }
);

If you want to assign the raw value to a different fieldname than that assigned by the API, you can create a simple Serializer. For example, if our API returns a gender code in a field named gender, we could make Person.gender contain the decoded value and store the raw, coded version by creating a serializer like this:

app/serializers/person.js

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
  attrs: {
    genderCode: 'gender'  // map the API's "gender" field to the "genderCode" field on the model
  }
});

Custom Enum Values

By default, ember-computed-enum assumes enum values are zero-indexed integers (0, 1, 2...), but other values can be used by passing in an object mapping enum names to values. For example, if "gender" were coded as 'm', 'f', and 'o', your model might look like:

// app/models/person.js

import DS from 'ember-data';

import { computedEnumMixin } from 'ember-computed-enum';

genderEnum = {'male': 'm', 'female', 'f', 'other': 'o'};

export default DS.Model.extend(
  computedEnumMixin('gender', 'genderCode', genderEnum),
  {
    name: DS.attr('string'),
    genderCode: DS.attr('number')
  }
);

The rest of this README outlines the details of collaborating on this Ember addon.

Development Installation

  • git clone <repository-url> this repository
  • cd ember-computed-enum
  • npm install
  • bower install

Running Tests

  • npm test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test
  • ember test --server

Building

  • ember build

For more information on using ember-cli, visit https://ember-cli.com/.