class Springcm::AttributeGroup

A configured attribute group in SpringCM.

Public Instance Methods

attributes() click to toggle source
# File lib/springcm-sdk/attribute_group.rb, line 40
def attributes
  attributes_config.map { |attr|
    if attr["Attributes"].is_a?(Array)
      attr["Attributes"]
    else
      [attr]
    end
  }.flatten
end
field(name) click to toggle source

Retrieve an attribute field by name.

# File lib/springcm-sdk/attribute_group.rb, line 19
def field(name)
  res = attributes.select { |attr|
    attr["Name"] == name
  }
  return nil if !res.any?
  # TODO: Assert only one result
  Attribute.new(res.first, @client)
end
set(name) click to toggle source

Retrieve an attribute set by name.

# File lib/springcm-sdk/attribute_group.rb, line 8
def set(name)
  res = attributes_config.select { |attr|
    attr["Attributes"].is_a?(Array) && attr["Name"] == name
  }
  return nil if !res.any?
  res.first["Attributes"].map { |attr|
    Attribute.new(attr, @client)
  }
end
set_for_field(name) click to toggle source
# File lib/springcm-sdk/attribute_group.rb, line 28
def set_for_field(name)
  if sets.map { |set| set["Name"] }.include?(name)
    return nil
  end
  sets.each { |set|
    if set["Attributes"].map { |attr| attr["Name"] }.include?(name)
      return set
    end
  }
  nil
end
sets() click to toggle source
# File lib/springcm-sdk/attribute_group.rb, line 50
def sets
  attributes_config.select { |attr|
    attr["Attributes"].is_a?(Array)
  }
end
template() click to toggle source
# File lib/springcm-sdk/attribute_group.rb, line 56
def template
  group = {}
  attributes_config.each { |attribute|
    type = attribute["Type"]
    if type == "DynamicDropDown"
      type = "MagicDropdown" # Not sure why they do this for applied groups, but oh well
    end
    repeating = attribute["RepeatingAttribute"]
    attr = {
      "AttributeType" => type,
      "RepeatingAttribute" => repeating
    }
    if attribute.key?("Attributes") # If it's a set
      attr["AttributeType"] = "Set"
      set = attr
      if repeating
        set = {}
        attr["Items"] = [set]
      end
      attribute["Attributes"].each { |field|
        set[field["Name"]] = {
          "AttributeType" => field["Type"],
          "RepeatingAttribute" => false
        }
      }
    elsif repeating
      # Must have empty array for repeating plain fields
      attr["Value"] = []
    end
    group[attribute["Name"]] = attr
  }
  group
end

Protected Instance Methods

attributes_config() click to toggle source
# File lib/springcm-sdk/attribute_group.rb, line 92
def attributes_config
  @data = reload.raw if !@data.key?("Attributes")
  @data["Attributes"]
end