class ConcourseObjects::Object

Constants

AsArray
AsArrayOf
AsBoolean
AsHash
AsHashOf
AsInteger
AsString
EmptyArray
EmptyHash
Enum
Nothing

Public Class Methods

attributes() click to toggle source
# File lib/concourse-objects.rb, line 124
def self.attributes
  readable_attributes + writable_attributes
end
call(object) click to toggle source
# File lib/concourse-objects.rb, line 8
def self.call(object)
  case object
  when self  then object
  when Hash  then new(object)
  when Array then object.map(&self)
  when nil   then nil
  else raise TypeError, "#{self.class.inspect} does not know how to parse #{object.class}(#{object.inspect})"
  end
end
inherited(child) click to toggle source
# File lib/concourse-objects.rb, line 199
def self.inherited(child)
  self.required_attributes.reduce(child.required_attributes, :<<)
  self.readable_attributes.reduce(child.readable_attributes, :<<)
  self.writable_attributes.reduce(child.writable_attributes, :<<)
end
keys() click to toggle source
# File lib/concourse-objects.rb, line 73
def self.keys
  instance_methods(false).map(&:to_s)
end
map(callable) click to toggle source
# File lib/concourse-objects.rb, line 104
def self.map(callable)
  ->(enumerable) { enumerable.map(&callable) }
end
new(options = {}) { |self, options| ... } click to toggle source
# File lib/concourse-objects.rb, line 49
def initialize(options = {})
  options.transform_keys(&:to_sym).yield_self do |options|
    # if ENV["REPORT_UNKNOWN_KEYS"]
    #   (Set.new(options.keys) - self.class.attributes).each do |attr|
    #     STDERR.puts "#{self.class.inspect} has no attribute :#{attr}"
    #   end
    # end

    self.class.required_attributes.each do |required|
      if options.key?(required)
        self.public_send("#{required}=", options.fetch(required))
      else
        raise KeyError, "#{self.class.inspect} is missing required attribute: #{required}"
      end
    end

    self.class.optional_attributes.each do |optional|
      self.public_send("#{optional}=", options.fetch(optional)) if options.key?(optional)
    end

    yield self, options if block_given?
  end
end
optional(attr, required: false, write: [], default: nil, guard: nil) click to toggle source
# File lib/concourse-objects.rb, line 186
def self.optional(attr, required: false, write: [], default: nil, guard: nil)
  readable(attr, &default)
  writable(attr, write, &guard)

  return attr.to_sym
end
optional_attributes() click to toggle source
# File lib/concourse-objects.rb, line 128
def self.optional_attributes
  attributes - required_attributes
end
readable(attr, &default) click to toggle source
# File lib/concourse-objects.rb, line 132
def self.readable(attr, &default)
  String(attr).yield_self do |attr|
    define_method("#{attr}?") do
      not instance_variable_get("@#{attr}").nil?
    end

    define_method(attr) do
      if instance_variable_defined?("@#{attr}")
        instance_variable_get("@#{attr}")
      else
        default.(self) if default
      end
    end

    readable_attributes << attr.to_sym

    return attr.to_sym
  end
end
required(attr, *rest) click to toggle source
# File lib/concourse-objects.rb, line 193
def self.required(attr, *rest)
  required_attributes << optional(attr, *rest)

  return attr.to_sym
end
singleton_class_Set(*attrs) click to toggle source
# File lib/concourse-objects.rb, line 108
def self.singleton_class_Set(*attrs)
  String(__callee__).prepend("@@").yield_self do |class_variable|
    if singleton_class.class_variable_defined?(class_variable)
      singleton_class.class_variable_get(class_variable)
    else
      singleton_class.class_variable_set(class_variable, Set.new)
    end.tap do |set|
      attrs.reduce(set, :<<)
    end
  end
end
to_proc() click to toggle source
# File lib/concourse-objects.rb, line 18
def self.to_proc
  proc { |*rest| self.call(*rest) }
end
writable(attr, transforms = [], &guard) click to toggle source
# File lib/concourse-objects.rb, line 152
def self.writable(attr, transforms = [], &guard)
  Array(transforms).map do |transform|
    if transform.respond_to?(:call)
      transform
    elsif transform.respond_to?(:to_proc)
      transform.to_proc
    elsif transform.is_a?(Class)
      ->(object) { object.send(transform.name, object) }
    else
      raise TypeError, "transformations must be callable"
    end
  end.yield_self do |transforms|
    String(attr).yield_self do |attr|
      define_method("#{attr}=") do |value|
        return unless guard.(self, value) if guard

        if value.nil?
          remove_instance_variable("@#{attr}")
        else
          transforms.reduce(value) do |value, transform|
            transform.(value)
          end.yield_self do |value|
            instance_variable_set("@#{attr}", value)
          end
        end
      end

      writable_attributes << attr.to_sym

      return attr.to_sym
    end
  end
end

Public Instance Methods

__instance_variable_name(name) click to toggle source
# File lib/concourse-objects.rb, line 100
def __instance_variable_name(name)
  String(name).delete_suffix("?").delete_suffix("=").prepend("@").to_sym
end
eql?(other) click to toggle source
# File lib/concourse-objects.rb, line 45
def eql?(other)
  self.hash == other.hash
end
hash() click to toggle source
# File lib/concourse-objects.rb, line 41
def hash
  self.to_h.hash
end
to_h() click to toggle source
# File lib/concourse-objects.rb, line 77
def to_h
  instance_variables.map(&:to_s).sort.map do |key|
    instance_variable_get(key.delete_prefix("@").prepend("@")).yield_self do |value|
      [
        key.delete_prefix("@"),
        (
          if (Array === value)
            value.map do |value|
              ((ConcourseObjects::Object === value) ? value.to_h : value)
            end
          else
            ((ConcourseObjects::Object === value) ? value.to_h : value)
          end
          )
      ] unless key.match? /@_/
    end
  end.to_h.compact
end
to_yaml() click to toggle source
# File lib/concourse-objects.rb, line 96
def to_yaml
  YAML.dump(self.to_h)
end