Class: JSONAPIonify::Api::Attribute

Inherits:
Object
  • Object
show all
Extended by:
JSONAPIonify::Autoload
Includes:
Documentation
Defined in:
lib/jsonapionify/api/attribute.rb

Defined Under Namespace

Modules: Documentation

Instance Attribute Summary collapse

Instance Method Summary collapse

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



12
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
# File 'lib/jsonapionify/api/attribute.rb', line 12

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             = 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

#blockObject (readonly)

Returns the value of attribute block.



10
11
12
# File 'lib/jsonapionify/api/attribute.rb', line 10

def block
  @block
end

#descriptionObject (readonly)

Returns the value of attribute description.



10
11
12
# File 'lib/jsonapionify/api/attribute.rb', line 10

def description
  @description
end

#hiddenObject (readonly)

Returns the value of attribute hidden.



10
11
12
# File 'lib/jsonapionify/api/attribute.rb', line 10

def hidden
  @hidden
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/jsonapionify/api/attribute.rb', line 10

def name
  @name
end

#readObject (readonly)

Returns the value of attribute read.



10
11
12
# File 'lib/jsonapionify/api/attribute.rb', line 10

def read
  @read
end

#requiredObject (readonly)

Returns the value of attribute required.



10
11
12
# File 'lib/jsonapionify/api/attribute.rb', line 10

def required
  @required
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/jsonapionify/api/attribute.rb', line 10

def type
  @type
end

#writeObject (readonly)

Returns the value of attribute write.



10
11
12
# File 'lib/jsonapionify/api/attribute.rb', line 10

def write
  @write
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
45
# File 'lib/jsonapionify/api/attribute.rb', line 42

def ==(other)
  self.class == other.class &&
    self.name == other.name
end

#allowObject



137
138
139
140
141
142
# File 'lib/jsonapionify/api/attribute.rb', line 137

def allow
  Array.new.tap do |ary|
    ary << 'read' if read?
    ary << 'write' if write?
  end
end

#example(*args) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/jsonapionify/api/attribute.rb', line 126

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



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

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



118
119
120
# File 'lib/jsonapionify/api/attribute.rb', line 118

def read?
  !!@read
end

#required_for_action?(action_name, context) ⇒ Boolean



113
114
115
116
# File 'lib/jsonapionify/api/attribute.rb', line 113

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



84
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
# File 'lib/jsonapionify/api/attribute.rb', line 84

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.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.message
    }
  )
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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jsonapionify/api/attribute.rb', line 52

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jsonapionify/api/attribute.rb', line 67

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



122
123
124
# File 'lib/jsonapionify/api/attribute.rb', line 122

def write?
  !!@write
end