Class: Scorecard::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/scorecard/parameters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, prefixes = [:gameable, :user]) ⇒ Parameters

Returns a new instance of Parameters.



4
5
6
# File 'lib/scorecard/parameters.rb', line 4

def initialize(options, prefixes = [:gameable, :user])
  @options, @prefixes = options.clone, prefixes
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/scorecard/parameters.rb', line 2

def options
  @options
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



2
3
4
# File 'lib/scorecard/parameters.rb', line 2

def prefixes
  @prefixes
end

Instance Method Details

#contractObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/scorecard/parameters.rb', line 20

def contract
  prefixes.collect(&:to_s).each do |prefix|
    next unless options["#{prefix}_type"]

    klass = options.delete("#{prefix}_type").constantize
    options[prefix] = klass.find options.delete("#{prefix}_id")
  end

  options.symbolize_keys
end

#expandObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/scorecard/parameters.rb', line 8

def expand
  prefixes.collect(&:to_sym).each do |prefix|
    next unless options[prefix]

    options["#{prefix}_id"]   = options[prefix].id
    options["#{prefix}_type"] = options[prefix].class.name
    options.delete prefix
  end

  options.stringify_keys
end