Class: JSONAPIonify::Api::ParamOptions
- Inherits:
-
Object
- Object
- JSONAPIonify::Api::ParamOptions
- Extended by:
- Structure::Helpers::MemberNames
- Defined in:
- lib/jsonapionify/api/param_options.rb
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#keypath ⇒ Object
readonly
Returns the value of attribute keypath.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#sticky ⇒ Object
readonly
Returns the value of attribute sticky.
Class Method Summary collapse
- .hash_to_keypaths(hash) ⇒ Object
- .invalid_parameters(hash, keypaths) ⇒ Object
- .keypath_to_string(*paths) ⇒ Object
- .missing_parameters(hash, keypaths) ⇒ Object
- .reserved?(value) ⇒ Boolean
- .valid?(value) ⇒ Boolean
Instance Method Summary collapse
- #default ⇒ Object
- #default_value ⇒ Object
- #default_value?(value) ⇒ Boolean
- #extract_value(params) ⇒ Object
- #has_default? ⇒ Boolean
-
#initialize(*keys, default: nil, actions: nil, required: false, sticky: false) ⇒ ParamOptions
constructor
A new instance of ParamOptions.
- #string ⇒ Object
- #with_value(value) ⇒ Object
Methods included from Structure::Helpers::MemberNames
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
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
47 48 49 |
# File 'lib/jsonapionify/api/param_options.rb', line 47 def actions @actions end |
#keypath ⇒ Object (readonly)
Returns the value of attribute keypath.
47 48 49 |
# File 'lib/jsonapionify/api/param_options.rb', line 47 def keypath @keypath end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
47 48 49 |
# File 'lib/jsonapionify/api/param_options.rb', line 47 def required @required end |
#sticky ⇒ Object (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
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
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
#default ⇒ Object
65 66 67 |
# File 'lib/jsonapionify/api/param_options.rb', line 65 def default with_value @default end |
#default_value ⇒ Object
69 70 71 |
# File 'lib/jsonapionify/api/param_options.rb', line 69 def default_value @default end |
#default_value?(value) ⇒ Boolean
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
79 80 81 |
# File 'lib/jsonapionify/api/param_options.rb', line 79 def has_default? @default.present? end |
#string ⇒ Object
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 |