Class: JSONAPIonify::Api::ParamOptions

Inherits:
Object
  • Object
show all
Extended by:
Structure::Helpers::MemberNames
Defined in:
lib/jsonapionify/api/param_options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Structure::Helpers::MemberNames

valid?

Constructor Details

#initialize(*keys, default: nil, actions: nil, required: false, sticky: false) ⇒ ParamOptions

Returns a new instance of ParamOptions.



49
50
51
52
53
54
55
# File 'lib/jsonapionify/api/param_options.rb', line 49

def initialize(*keys, default: nil, actions: nil, required: false, sticky: false)
  @keypath  = keys
  @sticky   = sticky
  @actions  = Array.wrap(actions)
  @default  = default.to_s
  @required = required
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



47
48
49
# File 'lib/jsonapionify/api/param_options.rb', line 47

def actions
  @actions
end

#keypathObject (readonly)

Returns the value of attribute keypath.



47
48
49
# File 'lib/jsonapionify/api/param_options.rb', line 47

def keypath
  @keypath
end

#requiredObject (readonly)

Returns the value of attribute required.



47
48
49
# File 'lib/jsonapionify/api/param_options.rb', line 47

def required
  @required
end

#stickyObject (readonly)

Returns the value of attribute sticky.



47
48
49
# File 'lib/jsonapionify/api/param_options.rb', line 47

def sticky
  @sticky
end

Class Method Details

.hash_to_keypaths(hash) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jsonapionify/api/param_options.rb', line 13

def self.hash_to_keypaths(hash)
  hash.each_with_object([]) do |(k, v), key_paths|
    pather = lambda do |v, current_path|
      if v.is_a? Hash
        v.each do |k, v|
          pather.call(v, [*current_path, k])
        end
      else
        key_paths << current_path.map(&:to_sym)
      end
    end
    pather.call(v, [k])
  end
end

.invalid_parameters(hash, keypaths) ⇒ Object



33
34
35
36
37
38
# File 'lib/jsonapionify/api/param_options.rb', line 33

def self.invalid_parameters(hash, keypaths)
  invalid_key_paths = hash_to_keypaths(hash) - keypaths
  invalid_key_paths.map do |paths|
    keypath_to_string(*paths)
  end
end

.keypath_to_string(*paths) ⇒ Object



28
29
30
31
# File 'lib/jsonapionify/api/param_options.rb', line 28

def self.keypath_to_string(*paths)
  first_path, *rest = paths
  "#{first_path}#{rest.map { |path| "[#{path}]" }.join}"
end

.missing_parameters(hash, keypaths) ⇒ Object



40
41
42
43
44
45
# File 'lib/jsonapionify/api/param_options.rb', line 40

def self.missing_parameters(hash, keypaths)
  missing_key_paths = keypaths - hash_to_keypaths(hash)
  missing_key_paths.map do |paths|
    keypath_to_string(*paths)
  end
end

.reserved?(value) ⇒ Boolean

Returns:



5
6
7
# File 'lib/jsonapionify/api/param_options.rb', line 5

def self.reserved?(value)
  %w{sort include}.include? value
end

.valid?(value) ⇒ Boolean

Returns:



9
10
11
# File 'lib/jsonapionify/api/param_options.rb', line 9

def self.valid?(value)
  super(value) && value =~ /[^\u0061-\u007A]/
end

Instance Method Details

#defaultObject



65
66
67
# File 'lib/jsonapionify/api/param_options.rb', line 65

def default
  with_value @default
end

#default_valueObject



69
70
71
# File 'lib/jsonapionify/api/param_options.rb', line 69

def default_value
  @default
end

#default_value?(value) ⇒ Boolean

Returns:



83
84
85
# File 'lib/jsonapionify/api/param_options.rb', line 83

def default_value?(value)
  @default == value
end

#extract_value(params) ⇒ Object



73
74
75
76
77
# File 'lib/jsonapionify/api/param_options.rb', line 73

def extract_value(params)
  keypath.reduce(params) do |p, key|
    p[key.to_s]
  end
end

#has_default?Boolean

Returns:



79
80
81
# File 'lib/jsonapionify/api/param_options.rb', line 79

def has_default?
  @default.present?
end

#stringObject



87
88
89
# File 'lib/jsonapionify/api/param_options.rb', line 87

def string
  self.class.keypath_to_string(*@keypath)
end

#with_value(value) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/jsonapionify/api/param_options.rb', line 57

def with_value(value)
  Hash.new.tap do |hash|
    keypath[0..-2].reduce(hash) do |current_hash, key|
      current_hash[key.to_s] = {}
    end[keypath.last.to_s] = value
  end
end