Sha256: c0bd6360d496b6110c4ea3aff2783d6d3288a2c454e80592ec43acf5993c74a4

Contents?: true

Size: 784 Bytes

Versions: 11

Compression:

Stored size: 784 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
        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.14 lib/journey/resource/enums.rb
embark-journey-0.0.13 lib/journey/resource/enums.rb
embark-journey-0.0.12 lib/journey/resource/enums.rb
embark-journey-0.0.11 lib/journey/resource/enums.rb
embark-journey-0.0.10 lib/journey/resource/enums.rb
embark-journey-0.0.9 lib/journey/resource/enums.rb
embark-journey-0.0.8 lib/journey/resource/enums.rb
embark-journey-0.0.7 lib/journey/resource/enums.rb
embark-journey-0.0.6 lib/journey/resource/enums.rb
embark-journey-0.0.5 lib/journey/resource/enums.rb
embark-journey-0.0.4 lib/journey/resource/enums.rb