Sha256: 945c151669948867b42433fbe204e33d67bcc448546b28eb41b307350ba57469
Contents?: true
Size: 1.08 KB
Versions: 3
Compression:
Stored size: 1.08 KB
Contents
require 'active_support/concern' module Journey::Resource::EnumSets extend ActiveSupport::Concern included do def self.enum_set(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 define_method attr do arr = attributes[attr.to_s].presence || [] arr.map do |member| if member.is_a?(Fixnum) self.class.const_get(collection_const_name)[member] else member end end end define_method "add_#{attr}" do |value| attr_values = send("#{attr}_values") value_index = if value.is_a?(Fixnum) value else attr_values.index(value) end if (0..attr_values.size-1).include? value_index (attributes[attr.to_s] ||= []) << value_index else raise "Invalid enum '#{value}' for '#{attr}'" end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
embark-journey-0.0.17 | lib/journey/resource/enum_sets.rb |
embark-journey-0.0.16 | lib/journey/resource/enum_sets.rb |
embark-journey-0.0.15 | lib/journey/resource/enum_sets.rb |