class Fried::Schema::Compare

Compares two {Struct} objects, used for sorting and equality

Attributes

get_attribute[RW]

Public Class Methods

build() click to toggle source
# File lib/fried/schema/compare.rb, line 18
def self.build
  new.tap do |instance|
    instance.get_attribute = GetAttribute.build
  end
end
call(schema, struct, other) click to toggle source
# File lib/fried/schema/compare.rb, line 24
def self.call(schema, struct, other)
  instance = build
  instance.(schema, struct, other)
end
new() click to toggle source
# File lib/fried/schema/compare.rb, line 14
def initialize
  self.get_attribute = GetAttribute.new
end

Public Instance Methods

call(schema, struct, other) click to toggle source

@param schema [Definition] @param struct [Struct] @param other [Struct] @return [Integer, nil] like `<=>` sign

# File lib/fried/schema/compare.rb, line 33
def call(schema, struct, other)
  return nil unless struct.class == other.class
  return nil unless Is[Struct].valid?(struct)
  return nil unless Is[Struct].valid?(other)

  compare(schema, struct, other)
end

Private Instance Methods

attr_values_pair(attr, struct, other) click to toggle source
# File lib/fried/schema/compare.rb, line 74
def attr_values_pair(attr, struct, other)
  [get_attribute.(struct, attr), get_attribute.(other, attr)]
end
compare(schema, struct, other) click to toggle source
# File lib/fried/schema/compare.rb, line 43
def compare(schema, struct, other)
  comparison = 0

  each_values(schema, struct, other) do |struct_value, other_value|
    next unless struct_value.respond_to?(:"<=>")
    next unless other_value.respond_to?(:"<=>")
    comparison = struct_value <=> other_value
    break if comparison != 0
  end

  comparison
end
each_values(schema, struct, other, &block) click to toggle source
# File lib/fried/schema/compare.rb, line 64
def each_values(schema, struct, other, &block)
  schema.
    each_attribute.
    lazy.
    map { |attr| attr_values_pair(attr, struct, other) }.
    each do |struct_value, other_value|
      block.call(struct_value, other_value)
    end
end
same_schema?(schema, struct, other) click to toggle source
# File lib/fried/schema/compare.rb, line 56
def same_schema?(schema, struct, other)
  schema.
    each_attribute.
    all? do |attr|
      struct.respond_to?(attr.reader) && other.respond_to?(attr.reader)
    end
end