Class: Keycloak::Model::BaseRepresentation

Inherits:
Object
  • Object
show all
Defined in:
lib/keycloak/model/base_representation.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload = {}) ⇒ BaseRepresentation

Returns a new instance of BaseRepresentation.



63
64
65
66
67
68
69
# File 'lib/keycloak/model/base_representation.rb', line 63

def initialize(payload = {})
  payload.each do |key, value|
    if self.class.attributes.include?(key.to_sym) || self.class.attributes.include?(key.to_s.camelize(:lower).to_sym)
      send("#{key}=", value)
    end
  end
end

Class Method Details

.attributesObject



54
55
56
# File 'lib/keycloak/model/base_representation.rb', line 54

def attributes
  @attributes ||= Concurrent::Array.new
end

.define_reader(name, type) ⇒ Object



44
45
46
# File 'lib/keycloak/model/base_representation.rb', line 44

def define_reader(name, type)
  attr_reader name
end

.define_writer(name, type) ⇒ Object



48
49
50
51
52
# File 'lib/keycloak/model/base_representation.rb', line 48

def define_writer(name, type)
  define_method "#{name}=" do |value|
    instance_variable_set("@#{name}", self.class.type_resolve(value, type))
  end
end

.field(name, type = :any) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/keycloak/model/base_representation.rb', line 11

def field(name, type = :any)
  define_reader name, type
  define_writer name, type
  snake_case_name = name.to_s.underscore
  if name.to_s != snake_case_name
    alias_method snake_case_name, name
    alias_method "#{snake_case_name}=", "#{name}="
  end

  name_sym = name.to_sym
  attributes << name_sym
  registry[name_sym] = type
end

.fields(*names) ⇒ Object



5
6
7
8
9
# File 'lib/keycloak/model/base_representation.rb', line 5

def fields(*names)
  names.each do |name|
    field name
  end
end

.registryObject



58
59
60
# File 'lib/keycloak/model/base_representation.rb', line 58

def registry
  @registry ||= Concurrent::Hash.new
end

.type_resolve(value, type) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/keycloak/model/base_representation.rb', line 25

def type_resolve(value, type)
  case
  when type == :any
    value
  when type == Integer
    value.to_i
  when type == String
    value.to_s
  when type == Float
    value.to_f
  when type.is_a?(Scalar::ArrayType)
    value.map { |e| type_resolve(e, type.type) }
  when type < BaseRepresentation
    value.is_a?(type) ? value : type.new(value)
  else
    type.new value
  end
end

Instance Method Details

#as_json(*options) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/keycloak/model/base_representation.rb', line 71

def as_json(*options)
  h = {}
  self.class.attributes.each do |attr|
    v = instance_variable_get("@#{attr}")
    h[attr] = v unless v.nil?
  end
  h.as_json(*options)
end

#to_json(*options) ⇒ Object



80
81
82
# File 'lib/keycloak/model/base_representation.rb', line 80

def to_json(*options)
  as_json(*options).to_json
end