Class: DiscourseApi::API::Params
- Inherits:
-
Object
- Object
- DiscourseApi::API::Params
- Defined in:
- lib/discourse_api/api/params.rb
Instance Method Summary collapse
- #default(args) ⇒ Object
-
#initialize(args) ⇒ Params
constructor
A new instance of Params.
- #optional(*keys) ⇒ Object
- #required(*keys) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(args) ⇒ Params
Returns a new instance of Params.
8 9 10 11 12 13 14 |
# File 'lib/discourse_api/api/params.rb', line 8 def initialize(args) raise ArgumentError.new("Required to be initialized with a Hash") unless args.is_a? Hash @args = args @required = [] @optional = [] @defaults = {} end |
Instance Method Details
#default(args) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/discourse_api/api/params.rb', line 26 def default(args) args.each do |k,v| @defaults[k] = v end self end |
#optional(*keys) ⇒ Object
21 22 23 24 |
# File 'lib/discourse_api/api/params.rb', line 21 def optional(*keys) @optional.concat(keys) self end |
#required(*keys) ⇒ Object
16 17 18 19 |
# File 'lib/discourse_api/api/params.rb', line 16 def required(*keys) @required.concat(keys) self end |
#to_h ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/discourse_api/api/params.rb', line 33 def to_h h = {} @required.each do |k| h[k] = @args[k] raise ArgumentError.new("#{k} is required but not specified") unless h[k] end @optional.each do |k| h[k] = @args[k] if @args.include?(k) end @defaults.each do |k,v| @args.key?(k) ? h[k] = @args[k] : h[k] = v end h end |