Sha256: 6ce0ebbad3471fe9615b63360fcfa37febb72b26bd7a1768a93a5722f249c107

Contents?: true

Size: 991 Bytes

Versions: 1

Compression:

Stored size: 991 Bytes

Contents

module FlexibleEnum
  class NameConfigurator < AbstractConfigurator
    def apply
      configurator = self

      add_instance_method("#{attribute_name}_name") do
        value = send(configurator.attribute_name)
        configurator.name_for(value)
      end

      add_class_method("human_#{attribute_name}") do |value|
        configurator.human_name_for(value)
      end

      add_instance_method("human_#{attribute_name}") do
        value = send(configurator.attribute_name)
        configurator.human_name_for(value)
      end
    end

    def name_for(value)
      if value
        element_info(value).first.to_s
      else
        nil
      end
    end

    def human_name_for(value)
      if value
        element_name, element_config = element_info(value)
        element_config[:human_name] || element_name.to_s.humanize
      else
        nil
      end
    end

    private

    def element_info(value)
      elements.select{|e,c| c[:value] == value }.first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flexible_enum-0.2.2 lib/flexible_enum/name_configurator.rb