module Choices::Rails

Public Class Methods

included(base) click to toggle source
# File lib/choices/rails.rb, line 5
def self.included(base)
  base.class_eval do
    def initialize_with_choices(*args, &block)
      initialize_without_choices(*args, &block)
      @choices = Hashie::Mash.new
    end
    
    alias :initialize_without_choices :initialize
    alias :initialize :initialize_with_choices
  end
end

Public Instance Methods

from_file(name) click to toggle source
# File lib/choices/rails.rb, line 17
def from_file(name)
  root = self.respond_to?(:root) ? self.root : Rails.root
  file = root + 'config' + name
  
  settings = Choices.load_settings(file, Rails.respond_to?(:env) ? Rails.env : RAILS_ENV)
  @choices.update settings
  
  settings.each do |key, value|
    old_value = self.respond_to?(key) ? self.send(key) : nil

    if "Rails::OrderedOptions" == old_value.class.name
      # convert from Array to a real Hash
      old_value = old_value.inject({}) {|h,(k,v)| h[k]=v; h }
    end

    if Hash === value and Hash === old_value
      # don't overwrite existing Hash values; deep update them
      value = Hashie::Mash.new(old_value).update value
    end

    self.send("#{key}=", value)
  end
end
initialize_with_choices(*args, &block) click to toggle source
# File lib/choices/rails.rb, line 7
def initialize_with_choices(*args, &block)
  initialize_without_choices(*args, &block)
  @choices = Hashie::Mash.new
end