Class: JSONAPIonify::Api::Attribute
- Inherits:
-
Object
- Object
- JSONAPIonify::Api::Attribute
- Extended by:
- JSONAPIonify::Autoload
- Includes:
- Documentation
- Defined in:
- lib/jsonapionify/api/attribute.rb
Defined Under Namespace
Modules: Documentation
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#hidden ⇒ Object
readonly
Returns the value of attribute hidden.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#read ⇒ Object
readonly
Returns the value of attribute read.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#write ⇒ Object
readonly
Returns the value of attribute write.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #allow ⇒ Object
- #example(*args) ⇒ Object
- #hidden_for_action?(action_name) ⇒ Boolean
-
#initialize(name, type, description, read: true, write: true, required: false, example: nil, hidden: false, &block) ⇒ Attribute
constructor
A new instance of Attribute.
- #read? ⇒ Boolean
- #required_for_action?(action_name, context) ⇒ Boolean
- #resolve(instance, context, example_id: nil) ⇒ Object
- #supports_read_for_action?(action_name, context) ⇒ Boolean
- #supports_write_for_action?(action_name, context) ⇒ Boolean
- #write? ⇒ Boolean
Methods included from JSONAPIonify::Autoload
autoload_all, eager_load!, unloaded
Methods included from Documentation
#documentation_object, #options_json_for_action
Constructor Details
#initialize(name, type, description, read: true, write: true, required: false, example: nil, hidden: false, &block) ⇒ Attribute
Returns a new instance of Attribute.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/jsonapionify/api/attribute.rb', line 13 def initialize( name, type, description, read: true, write: true, required: false, example: nil, hidden: false, &block ) unless type.is_a? JSONAPIonify::Types::BaseType raise TypeError, "#{type} is not a valid JSON type" end @name = name.to_sym @type = type&.freeze @description = description&.freeze @example = example&.freeze @read = read&.freeze @write = (!block && write)&.freeze @required = required&.freeze @block = block&.freeze @writeable_actions = write @readable_actions = read @hidden = !!hidden && (hidden == true || Array.wrap(hidden)) freeze end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
11 12 13 |
# File 'lib/jsonapionify/api/attribute.rb', line 11 def block @block end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
11 12 13 |
# File 'lib/jsonapionify/api/attribute.rb', line 11 def description @description end |
#hidden ⇒ Object (readonly)
Returns the value of attribute hidden.
11 12 13 |
# File 'lib/jsonapionify/api/attribute.rb', line 11 def hidden @hidden end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/jsonapionify/api/attribute.rb', line 11 def name @name end |
#read ⇒ Object (readonly)
Returns the value of attribute read.
11 12 13 |
# File 'lib/jsonapionify/api/attribute.rb', line 11 def read @read end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
11 12 13 |
# File 'lib/jsonapionify/api/attribute.rb', line 11 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
11 12 13 |
# File 'lib/jsonapionify/api/attribute.rb', line 11 def type @type end |
#write ⇒ Object (readonly)
Returns the value of attribute write.
11 12 13 |
# File 'lib/jsonapionify/api/attribute.rb', line 11 def write @write end |
Instance Method Details
#==(other) ⇒ Object
43 44 45 46 |
# File 'lib/jsonapionify/api/attribute.rb', line 43 def ==(other) self.class == other.class && self.name == other.name end |
#allow ⇒ Object
138 139 140 141 142 143 |
# File 'lib/jsonapionify/api/attribute.rb', line 138 def allow Array.new.tap do |ary| ary << 'read' if read? ary << 'write' if write? end end |
#example(*args) ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/jsonapionify/api/attribute.rb', line 127 def example(*args) case @example when Proc type.dump @example.unstrict.call(*args) when nil type.dump type.sample(name) else type.dump @example end end |
#hidden_for_action?(action_name) ⇒ Boolean
48 49 50 51 |
# File 'lib/jsonapionify/api/attribute.rb', line 48 def hidden_for_action?(action_name) return false if hidden == false Array.wrap(hidden).any? { |h| h == true || h.to_s == action_name.to_s } end |
#read? ⇒ Boolean
119 120 121 |
# File 'lib/jsonapionify/api/attribute.rb', line 119 def read? !!@read end |
#required_for_action?(action_name, context) ⇒ Boolean
114 115 116 117 |
# File 'lib/jsonapionify/api/attribute.rb', line 114 def required_for_action?(action_name, context) supports_write_for_action?(action_name, context) && (required === true || Array.wrap(required).include?(action_name)) end |
#resolve(instance, context, example_id: nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/jsonapionify/api/attribute.rb', line 85 def resolve(instance, context, example_id: nil) if context.respond_to?(:_is_example_) && context._is_example_ == true return example(example_id) end block = self.block || proc { |attr, i| i.send attr } type.dump block.unstrict.destructure.call(self.name, instance, context) rescue JSONAPIonify::Types::DumpError => ex error_block = context.resource.class.error_definitions[:attribute_type_error] context.errors.evaluate( name, error_block: error_block, backtrace: ex.backtrace, runtime_block: proc { detail ex. } ) rescue JSONAPIonify::Types::NotNullError => ex error_block = context.resource.class.error_definitions[:attribute_cannot_be_null] context.errors.evaluate( name, error_block: error_block, backtrace: ex.backtrace, runtime_block: proc {} ) nil end |
#supports_read_for_action?(action_name, context) ⇒ Boolean
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/jsonapionify/api/attribute.rb', line 53 def supports_read_for_action?(action_name, context) case (setting = @readable_actions) when TrueClass, FalseClass setting when Hash !!JSONAPIonify::Continuation.new(setting).check(action_name, context) { true } when Array setting.map(&:to_sym).include? action_name when Symbol, String setting.to_sym === action_name else false end end |
#supports_write_for_action?(action_name, context) ⇒ Boolean
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/jsonapionify/api/attribute.rb', line 68 def supports_write_for_action?(action_name, context) action = context.resource.class.actions.find { |a| a.name == action_name } return false unless %{POST PUT PATCH}.include? action.request_method case (setting = @writeable_actions) when TrueClass, FalseClass setting when Hash !!JSONAPIonify::Continuation.new(setting).check(action_name, context) { true } when Array setting.map(&:to_sym).include? action_name when Symbol, String setting.to_sym === action_name else false end end |
#write? ⇒ Boolean
123 124 125 |
# File 'lib/jsonapionify/api/attribute.rb', line 123 def write? !!@write end |