Class: BillboardApi::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/billboard-api/config.rb

Overview

Config is a singleton instance which can be used in two ways: Config.instance.setting_name1 = 12 Config.instance.setting_name2 = 24 or the (shorter, depending on amount of settings) form with a block Config.configure do |config|

config.setting_name1= 12
config.setting_name2= 24

end

the accessors have possibility to override settings. This is useful for custom settings on a lower level

config.setting_name1(12)

> will return 12 all the time which does not make much sense…

config.setting_name(options) will return the not-nil value from options has or the setting stored in config.

Constant Summary collapse

REQUIRED_SETTINGS =
[:return_after_payment_url, :paypal_service_url, :paypal_notify_url, :paypal_receiver_email, :site_url]

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object

Allow access for arbitrary settings



46
47
48
49
50
51
52
53
54
55
# File 'lib/billboard-api/config.rb', line 46

def method_missing(m, *args)
  if !m.to_s.ends_with?('=') && args.length <= 1
    # return override or setting
    args.first || settings[m.to_s]
  elsif m.to_s.ends_with?('=') && args.length == 1
    settings[m.to_s[0...-1]] = args.first
  else
    super(m, args)
  end
end

Class Method Details

.configure {|instance| ... } ⇒ Object

Configure with a block will yield the instance of Config to the block.

Yields:

  • (instance)


73
74
75
76
# File 'lib/billboard-api/config.rb', line 73

def self.configure(&block)
  config = Config.instance
  yield(instance) if block_given?
end

Instance Method Details

#clear!Object



65
66
67
68
69
# File 'lib/billboard-api/config.rb', line 65

def clear!
  @settings = {}
  site_url = nil
  nil
end

#site_url(override = nil) ⇒ Object

Overwrite site urls since they are directly applied to the Order class’ class variable ‘site’



38
39
40
# File 'lib/billboard-api/config.rb', line 38

def site_url(override = nil)
  override || ActiveResource::Base.site
end

#site_url=(value) ⇒ Object



41
42
43
# File 'lib/billboard-api/config.rb', line 41

def site_url=(value)
  ActiveResource::Base.site= value
end

#validate!Object

Helper method to ensure all settings are present. Will raise a StandardError if Config is invalid.



59
60
61
62
63
# File 'lib/billboard-api/config.rb', line 59

def validate!
  REQUIRED_SETTINGS.each do |name|
    raise "Settings '#{name}' is required. Use 'Config.instance.#{name}= VALUE' to set a value" unless self.send(name) 
  end
end