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, &block) ⇒ Attribute

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

def initialize(
  name,
  type,
  description,
  read: true,
  write: true,
  required: false,
  example: nil,
  &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

  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

#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



40
41
42
43
# File 'lib/jsonapionify/api/attribute.rb', line 40

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

#allowObject



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

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

#example(*args) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/jsonapionify/api/attribute.rb', line 119

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

#read?Boolean

Returns:



111
112
113
# File 'lib/jsonapionify/api/attribute.rb', line 111

def read?
  !!@read
end

#required_for_action?(action_name, context) ⇒ Boolean

Returns:



106
107
108
109
# File 'lib/jsonapionify/api/attribute.rb', line 106

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jsonapionify/api/attribute.rb', line 77

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

Returns:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jsonapionify/api/attribute.rb', line 45

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

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jsonapionify/api/attribute.rb', line 60

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

Returns:



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

def write?
  !!@write
end