module Calendly::ModelUtils

Calendly model utility.

Public Class Methods

included(base) click to toggle source
# File lib/calendly/models/model_utils.rb, line 69
def self.included(base)
  base.extend ClassMethods
end
new(attrs = nil, client = nil) click to toggle source

@param [Hash] attrs the attributes of the model. @param [Calendly::Client] the api client.

# File lib/calendly/models/model_utils.rb, line 11
def initialize(attrs = nil, client = nil)
  @client = client
  set_attributes attrs
end

Public Instance Methods

client() click to toggle source

Returns api client.

@return [Calendly::Client] @raise [Calendly::Error] if the client is nil. @since 0.1.0

# File lib/calendly/models/model_utils.rb, line 22
def client
  raise Error.new('@client is not ready.') if !@client || !@client.is_a?(Client)

  @client
end
id() click to toggle source

Alias of uuid.

@return [String] @raise [Calendly::Error] if uuid is not defined. @since 0.1.0

# File lib/calendly/models/model_utils.rb, line 34
def id
  raise Error.new('uuid is not defined.') unless defined? uuid

  uuid
end
inspect() click to toggle source

Self object description human readable in CLI.

@return [String] @since 0.0.1

# File lib/calendly/models/model_utils.rb, line 45
def inspect
  att_info = []
  inspect_attributes.each do |att|
    next unless respond_to? att

    att_info << "#{att}=#{send(att).inspect}"
  end
  att_info << '..'
  "\#<#{self.class}:#{object_id} #{att_info.join(', ')}>"
end

Private Instance Methods

after_set_attributes(attrs) click to toggle source
# File lib/calendly/models/model_utils.rb, line 100
def after_set_attributes(attrs)
  @uuid = self.class.extract_uuid(attrs[:uri]) if respond_to? :uuid=
end
auto_pagination(request_proc, opts) click to toggle source

Get all collection from single page or plurality of pages.

@param [Proc] request_proc the procedure of request portion of collection. @param [Hash] opts the optional request parameters for the procedure. @return [Array<Calendly::Model>] @since 0.1.0

# File lib/calendly/models/model_utils.rb, line 111
def auto_pagination(request_proc, opts)
  items = []
  loop do
    new_items, next_opts = request_proc.call opts
    items = [*items, *new_items]
    break unless next_opts

    opts = next_opts
  end
  items
end
inspect_attributes() click to toggle source

Basic attributes used by inspect method.

@return [Array<Symbol>] @since 0.6.0

# File lib/calendly/models/model_utils.rb, line 128
def inspect_attributes
  %i[uuid name type slug status email]
end
set_attributes(attrs) click to toggle source
# File lib/calendly/models/model_utils.rb, line 75
def set_attributes(attrs) # rubocop:disable Naming/AccessorMethodName, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  return if attrs.nil?
  return unless attrs.is_a? Hash
  return if attrs.empty?

  attrs.each do |key, value|
    next unless respond_to? "#{key}=".to_sym

    if value && defined?(self.class::ASSOCIATION) && self.class::ASSOCIATION.key?(key)
      klass = self.class::ASSOCIATION[key]
      if value.is_a? String # rubocop:disable Style/CaseLikeIf
        value = klass.new({uri: value}, @client)
      elsif value.is_a? Hash
        value = klass.new(value, @client)
      elsif value.is_a? Array
        value = value.map { |v| klass.new(v, @client) }
      end
    elsif value && defined?(self.class::TIME_FIELDS) && self.class::TIME_FIELDS.include?(key)
      value = Time.parse value
    end
    instance_variable_set "@#{key}", value
  end
  after_set_attributes(attrs)
end