Class: Bluepine::Attributes::Attribute Abstract

Inherits:
Object
  • Object
show all
Includes:
Bluepine::Assertions, Serializers::Serializable, Validators::Normalizable, Validators::Validatable
Defined in:
lib/bluepine/attributes/attribute.rb

Overview

This class is abstract.

An abstract Attribute based class.

Constant Summary

Constants included from Bluepine::Assertions

Bluepine::Assertions::Error, Bluepine::Assertions::KeyError, Bluepine::Assertions::SubsetError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validators::Validatable

#validators

Methods included from Validators::Normalizable

#normalize

Methods included from Serializers::Serializable

#serialize

Methods included from Bluepine::Assertions

#assert, #assert_in, #assert_kind_of, #assert_not, #assert_subset_of, included

Constructor Details

#initialize(name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



18
19
20
21
# File 'lib/bluepine/attributes/attribute.rb', line 18

def initialize(name, options = {})
  @name    = name
  @options = self.class.options.merge(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/bluepine/attributes/attribute.rb', line 13

def name
  @name
end

Instance Method Details

#attributesObject



95
96
97
# File 'lib/bluepine/attributes/attribute.rb', line 95

def attributes
  {}
end

#defaultObject



112
113
114
# File 'lib/bluepine/attributes/attribute.rb', line 112

def default
  @options[:default]
end

#deprecatedObject

deprecated attribute should be listed in schema



100
101
102
# File 'lib/bluepine/attributes/attribute.rb', line 100

def deprecated
  @options.fetch(:deprecated, false)
end

#descriptionObject



116
117
118
# File 'lib/bluepine/attributes/attribute.rb', line 116

def description
  @options[:description]
end

#formatObject



83
84
85
# File 'lib/bluepine/attributes/attribute.rb', line 83

def format
  @options[:format]
end

#ifObject



67
68
69
# File 'lib/bluepine/attributes/attribute.rb', line 67

def if
  @options[:if]
end

#inObject



63
64
65
# File 'lib/bluepine/attributes/attribute.rb', line 63

def in
  @options[:in]
end

#matchObject



51
52
53
# File 'lib/bluepine/attributes/attribute.rb', line 51

def match
  @options[:match]
end

#methodObject



55
56
57
# File 'lib/bluepine/attributes/attribute.rb', line 55

def method
  @options[:method] || @name
end

#native_typeObject



79
80
81
# File 'lib/bluepine/attributes/attribute.rb', line 79

def native_type
  type
end

#nullObject



75
76
77
# File 'lib/bluepine/attributes/attribute.rb', line 75

def null
  @options.fetch(:null, false)
end

#ofObject



59
60
61
# File 'lib/bluepine/attributes/attribute.rb', line 59

def of
  @options[:of]
end

#optionsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bluepine/attributes/attribute.rb', line 23

def options
  @options.merge({
    name:         @name,
    match:        match,
    method:       method,
    type:         type,
    native_type:  native_type,
    of:           of,
    in:           send(:in),
    if:           @options[:if],
    unless:       @options[:unless],
    null:         null,
    spec:         spec,
    spec_uri:     spec_uri,
    format:       format,
    private:      private,
    deprecated:   deprecated,
    required:     required,
    default:      default,
    description:  description,
    attributes:   attributes.values&.map(&:options),
  })
end

#privateObject



104
105
106
# File 'lib/bluepine/attributes/attribute.rb', line 104

def private
  @options.fetch(:private, false)
end

#requiredObject



108
109
110
# File 'lib/bluepine/attributes/attribute.rb', line 108

def required
  @options.fetch(:required, false)
end

#serializable?Boolean

Should not be listed in schema or serialize this attribute

Returns:

  • (Boolean)


121
122
123
# File 'lib/bluepine/attributes/attribute.rb', line 121

def serializable?
  !private
end

#specObject



87
88
89
# File 'lib/bluepine/attributes/attribute.rb', line 87

def spec
  nil
end

#spec_uriObject



91
92
93
# File 'lib/bluepine/attributes/attribute.rb', line 91

def spec_uri
  nil
end

#typeObject



47
48
49
# File 'lib/bluepine/attributes/attribute.rb', line 47

def type
  self.class.name.demodulize.chomp("Attribute").underscore
end

#unlessObject



71
72
73
# File 'lib/bluepine/attributes/attribute.rb', line 71

def unless
  @options[:unless]
end

#value(value) ⇒ Object



125
126
127
# File 'lib/bluepine/attributes/attribute.rb', line 125

def value(value)
  value.nil? ? default : value
end