main
1# frozen_string_literal: true
2
3module Scim
4 module Kit
5 module V2
6 # Represents a SCIM Schema
7 class Schema
8 include Templatable
9
10 attr_reader :id, :name, :attributes
11 attr_accessor :meta, :description
12
13 def initialize(id:, name:, location:)
14 @id = id
15 @name = name
16 @description = name
17 @meta = Meta.new('Schema', location)
18 @meta.created = @meta.last_modified = @meta.version = nil
19 @attributes = []
20 yield self if block_given?
21 end
22
23 def add_attribute(name:, type: :string)
24 attribute = AttributeType.new(name: name, type: type, schema: self)
25 yield attribute if block_given?
26 attributes << attribute
27 end
28
29 def core?
30 id.include?(Schemas::CORE) || id.include?(Messages::CORE)
31 end
32
33 class << self
34 def build(**args)
35 item = new(**args)
36 yield item
37 item
38 end
39
40 def from(hash)
41 Schema.new(
42 id: hash[:id],
43 name: hash[:name],
44 location: hash[:location]
45 ) do |x|
46 x.meta = Meta.from(hash[:meta])
47 hash[:attributes].each do |y|
48 x.attributes << parse_attribute_type(y)
49 end
50 end
51 end
52
53 def parse(json)
54 from(JSON.parse(json, symbolize_names: true))
55 end
56
57 private
58
59 def parse_attribute_type(hash)
60 attribute_type = AttributeType.from(hash)
61 hash[:subAttributes]&.each do |sub_attr_hash|
62 attribute_type.attributes << parse_attribute_type(sub_attr_hash)
63 end
64 attribute_type
65 end
66 end
67 end
68 end
69 end
70end