2

I need your guidence about environmental vaariable in ruby.

================================================

I have some code that convert version of device intedtifier => device name.. in my code, path:

Config-initilizer-android.rb

module Android
  @conversion_table_of_model_id_and_device_name = {
    'Anroid2,3'   => 'Gingerbread',
    'Android3,0'   => 'Honeycomb',
    'Android4,1'   => 'ICS',
    'Android4,2'   => 'JB',
}

in the future, if there's another name of android eg.'KitKat'. i want to add it using environment variable not use the hard code. how can i to do it? is it possible to that? where should i put it?

regards. agstwn

agstwn21
  • 123
  • 1
  • 14

3 Answers3

1

With Rails 4.1 you can use secrets.yml, check out this link

If you have earlier version, try figaro gem

Sergey Kishenin
  • 5,099
  • 3
  • 30
  • 50
1

What blocks you from using ENV constant?

module Android
  @conversion_table_of_model_id_and_device_name = {
    'Anroid2,3'   => 'Gingerbread',
    'Android3,0'   => 'Honeycomb',
    'Android4,1'   => 'ICS',
    'Android4,2'   => 'JB',
    'Android4,3'   => ENV[ 'ANDROID_OS_NAME' ]
}
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
  • i'm just using it model, when i model. so when i input in database like this `android ( android2,3)` => os ( device model name) – agstwn21 Apr 14 '14 at 08:06
  • 1
    @agstwn21 I don't understood you, please update the quesion with a code – Малъ Скрылевъ Apr 14 '14 at 08:08
  • sorry,i push 'enter' by accident. =============== I'm using a helper and model, `Android.conversion_table_of_model_id_and_device_name[model_id]` and in the model i just call it. `device_model_name = Android.convert_model_id_to_device_name(device_model)` there's 2 type in ui when input. 1. when i input in device menu, like this android2,3 it will become gingerbread 2.when i input in db, like this android (android2,3) in the ui will become android (gingerbread) i want to add the records by using environmental variable, i dont need to hard code if the new feature/os has come. @Малъ Скрылевъ – agstwn21 Apr 14 '14 at 08:32
  • is it ok if i do this `ENV['MODEL_IDENTTIFIER'] => ENV['DEVICE_NAME']` @446276 – agstwn21 Apr 14 '14 at 08:55
0

You have to remember that ENV vars are still variables, meaning you can use them in a similar way. I'd propose using something like the new secrets.yml in Rails 4.1, or something like config spartan to create a yml structure of the data you need:

#config/secrets.yml
development:
  android:
    2.3: "gingerbread"
    3.0: "Honeycomb"
    4.1: "ICS"
    4.2: "JB"
    4.4: "KK"


module Android
    @conversion_table_of_model_id_and_device_name
    me = {}
    Rails.application.secrets.android.each do |k,v|
        me[k.to_sym] = v
    end
end
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • 1
    thanks for your answer, but i'm using rails 3.0.20 i just to add record by using env variable if in future there's a new version of android os @rich-peck – agstwn21 Apr 14 '14 at 08:37
  • Okay it's up to you! You have to remember an ENV variable is on the OS -- so if you change hosting providers, etc, your ENV vars will not persist – Richard Peck Apr 14 '14 at 08:38
  • ow i see, so when i change my provider the variable will be lost,right? @rick-peck – agstwn21 Apr 14 '14 at 08:42
  • is there another way to add records by using env variable, by running in the console and put the records in the ui and will change to device name i'm a new guy in ruby.. @rich-peck – agstwn21 Apr 14 '14 at 08:49
  • There are several options -- you could create an initializer & set your own variables / constants (http://stackoverflow.com/questions/1450285/how-to-define-custom-configuration-variables-in-rails). You could create data table for them; But I'd recommend [config-spartan](https://github.com/cjbottaro/config_spartan) to store the elements you need – Richard Peck Apr 14 '14 at 08:55