Giter Club home page Giter Club logo

ehon's Introduction

Ehon

Build Status Code Climate Gem Version

Ehon is a simple enum library.

Installation

Add this line to your application's Gemfile:

gem 'ehon'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ehon

Usage

You can create your enum class easily.

class DeviceType
  include Ehon

  default user_agent_regexp: //

  UNKNOWN = enum 0, name: 'unknown'
  IOS     = enum 1, name: 'iOS',     user_agent_regexp: /(iPhone|iPad|iPod)/
  ANDROID = enum 2, name: 'Android', user_agent_regexp: /Android/
end

Using DeviceType class.

DeviceType::IOS.id #=> 1
DeviceType::ANDROID.user_agent_regexp #=> /Android/

Finding.

You can find your item from enum class with following 2 way.

DeviceType[1, 2] #=> [#<DeviceType:...(iOS)>, #<DeviceType:...(Android)>]
DeviceType.find(name: 'iOS') #=> #<DeviceType:...(iOS)>

and you also list all items using #all method

DeviceType.all #=> [iOS, Android, unknown]

Custom methods.

You can define any method for your enum class.

class DeviceType
  def ios?
    self.name == "iOS"
  end

  def android?
    self.name == "Android"
  end

  def unknown?
    self.name == "unknown"
  end

  def valid_user_agent?(user_agent)
    !!(self.user_agent_regexp =~ user_agent)
  end
end
DeviceType::IOS.ios? #=> true
DeviceType::IOS.android? #=> false

user_agent = 'Mozilla/5.0 (Linux; Android 4.4.4; KYV33 Build/xxxx)'
DeviceType::ANDROID.valid_user_agent?(user_agent) #=> true

Contributing

  1. Fork it ( http://github.com/hekk/ehon/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

ehon's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.