Commit 2cbaa80
Changed files (4)
lib
scim
lib/scim/kit/v2/attributable.rb
@@ -7,14 +7,21 @@ module Scim
module Attributable
include Enumerable
+ # Returns a hash of the generated dynamic attributes
+ # @return [Hash] the dynamic attributes keys by their name
def dynamic_attributes
@dynamic_attributes ||= {}.with_indifferent_access
end
+ # Defines dynamic attributes on the resource for the types provided
+ # @param resource [Scim::Kit::V2::Resource] the resource to attach dynamic attributes to.
+ # @param types [Array<Scim::Kit::V2::AttributeType>] the array of types
def define_attributes_for(resource, types)
types.each { |x| attribute(x, resource) }
end
+ # Assigns attribute values via the provided hash.
+ # @param attributes [Hash] The name/values to assign.
def assign_attributes(attributes = {})
attributes.each do |key, value|
next if key.to_sym == :schemas
@@ -27,10 +34,16 @@ module Scim
end
end
+ # Returns the attribute identified by the name.
+ # @param name [String] the name of the attribute to return
+ # @return [Scim::Kit::V2::Attribute] the attribute or {Scim::Kit::V2::UnknownAttribute}
def attribute_for(name)
dynamic_attributes[name.to_s.underscore] || UnknownAttribute.new(name)
end
+ # Returns the value associated with the attribute name
+ # @param name [String] the name of the attribute
+ # @return [Object] the value assigned to the attribute
def read_attribute(name)
attribute = attribute_for(name)
return attribute._value if attribute._type.multi_valued
@@ -38,6 +51,9 @@ module Scim
attribute._type.complex? ? attribute : attribute._value
end
+ # Assigns the value to the attribute with the given name
+ # @param name [String] the name of the attribute
+ # @param value [Object] the value to assign to the attribute
def write_attribute(name, value)
if value.is_a?(Hash)
attribute_for(name)&.assign_attributes(value)
@@ -46,6 +62,8 @@ module Scim
end
end
+ # yields each attribute to the provided block
+ # @param [Block] the block to yield each attribute to.
def each
dynamic_attributes.each do |_name, attribute|
yield attribute
lib/scim/kit/v2/resource.rb
@@ -27,6 +27,10 @@ module Scim
yield self if block_given?
end
+ # Returns the current mode.
+ #
+ # @param type [Symbol] The mode `:server` or `:client`.
+ # @return [Boolean] Returns true if the resource matches the # type of mode
def mode?(type)
case type.to_sym
when :server
@@ -36,6 +40,8 @@ module Scim
end
end
+ # Returns the name of the jbuilder template file.
+ # @return [String] the name of the jbuilder template.
def template_name
'resource.json.jbuilder'
end
lib/scim/kit/templatable.rb
@@ -4,22 +4,35 @@ module Scim
module Kit
# Implement methods necessary to generate json from jbuilder templates.
module Templatable
+ # Returns the JSON representation of the item.
+ # @param options [Hash] the hash of options to forward to jbuilder
+ # return [String] the json string
def to_json(options = {})
render(self, options)
end
+ # Returns the hash representation of the JSON
+ # @return [Hash] the hash representation of the items JSON.
def as_json(_options = nil)
to_h
end
+ # Returns the hash representation of the JSON
+ # @return [Hash] the hash representation of the items JSON.
def to_h
JSON.parse(to_json, symbolize_names: true).with_indifferent_access
end
+ # Renders the model to JSON.
+ # @param model [Object] the model to render.
+ # @param options [Hash] the hash of options to pass to jbuilder.
+ # @return [String] the JSON.
def render(model, options)
Template.new(model).to_json(options)
end
+ # Returns the file name of the jbuilder template.
+ # @return [String] name of the jbuilder template.
def template_name
"#{self.class.name.split('::').last.underscore}.json.jbuilder"
end
scim-kit.gemspec
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
end
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.5.0'
+ spec.metadata["yard.run"] = "yri"
spec.add_dependency 'activemodel', '>= 5.2.0'
spec.add_dependency 'net-hippie', '~> 0.2'