Sha256: 17415bb66ba8dc09beb535c902de24b12d77ea658c12bb81728468d83e53e321

Contents?: true

Size: 799 Bytes

Versions: 11

Compression:

Stored size: 799 Bytes

Contents

require 'active_support/concern'

module Journey::Resource::Enums
  extend ActiveSupport::Concern

  included do
    def self.enum(attr, collection=[])
      collection_const_name = attr.to_s.pluralize.upcase.to_sym
      const_set collection_const_name, collection.freeze      
      define_method "#{attr}_values" do
        self.class.const_get(collection_const_name)
      end

      instance_eval do
        # TODO deprecated?
        attr_accessor :"#{attr}_index"
      end

      define_method attr do
        value = attributes[attr.to_s].presence
        if value.is_a?(Fixnum)
          send("#{attr}_values")[value]
        else
          value
        end
      end

      define_method "#{attr}=" do |value|
        attributes[attr.to_s] = value.presence
      end
    end

  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
embark-journey-0.0.26 lib/journey/resource/enums.rb
embark-journey-0.0.25 lib/journey/resource/enums.rb
embark-journey-0.0.24 lib/journey/resource/enums.rb
embark-journey-0.0.23 lib/journey/resource/enums.rb
embark-journey-0.0.22 lib/journey/resource/enums.rb
embark-journey-0.0.21 lib/journey/resource/enums.rb
embark-journey-0.0.20 lib/journey/resource/enums.rb
embark-journey-0.0.19 lib/journey/resource/enums.rb
embark-journey-0.0.18 lib/journey/resource/enums.rb
embark-journey-0.0.17 lib/journey/resource/enums.rb
embark-journey-0.0.16 lib/journey/resource/enums.rb