class Teacup::Constraint

Constants

Attributes
Priorities
Relationships

Attributes

attribute[RW]
attribute2[RW]
constant[RW]
identifier[RW]
multiplier[RW]
priority[RW]
relationship[RW]
relative_to[RW]
target[RW]

Public Class Methods

from_sym(sym, relative_to=:superview) click to toggle source
# File lib/teacup/constraint.rb, line 44
def self.from_sym(sym, relative_to=:superview)
  case sym
  when :full
    [
      self.new(:self, :left).equals(relative_to, :left),
      self.new(:self, :top).equals(relative_to, :top),
      self.new(:self, :width).equals(relative_to, :width),
      self.new(:self, :height).equals(relative_to, :height),
    ]
  when :full_width
    [
      self.new(:self, :center_x).equals(relative_to, :center_x),
      self.new(:self, :width).equals(relative_to, :width),
    ]
  when :full_height
    [
      self.new(:self, :center_y).equals(relative_to, :center_y),
      self.new(:self, :height).equals(relative_to, :height),
    ]
  when :center_x
    [
      self.new(:self, :center_x).equals(:superview, :center_x),
    ]
  when :center_y
    [
      self.new(:self, :center_y).equals(:superview, :center_y),
    ]
  when :centered
    [
      self.new(:self, :center_x).equals(:superview, :center_x),
      self.new(:self, :center_y).equals(:superview, :center_y),
    ]
  when :top
    [
      self.new(:self, :top).equals(relative_to, :top),
    ]
  when :right
    [
      self.new(:self, :right).equals(relative_to, :right),
    ]
  when :bottom
    [
      self.new(:self, :bottom).equals(relative_to, :bottom),
    ]
  when :left
    [
      self.new(:self, :left).equals(relative_to, :left),
    ]
  when :top_left, :topleft
    [
      self.new(:self, :left).equals(relative_to, :left),
      self.new(:self, :top).equals(relative_to, :top),
    ]
  when :top_right, :topright
    [
      self.new(:self, :right).equals(relative_to, :right),
      self.new(:self, :top).equals(relative_to, :top),
    ]
  when :bottom_right, :bottomright
    [
      self.new(:self, :right).equals(relative_to, :right),
      self.new(:self, :bottom).equals(relative_to, :bottom),
    ]
  when :bottom_left, :bottomleft
    [
      self.new(:self, :left).equals(relative_to, :left),
      self.new(:self, :bottom).equals(relative_to, :bottom),
    ]
  else
    raise "Unknown symbol #{sym.inspect}"
  end
end
new(target=nil, attribute=nil) click to toggle source
# File lib/teacup/constraint.rb, line 117
def initialize(target=nil, attribute=nil)
  self.target = target
  self.attribute = attribute
  self.constant = 0
  self.multiplier = 1
  self.priority = :high  # this is the xcode default
end

Public Instance Methods

at_least(relative_to, attribute2=nil)
Alias for: gte
at_most(relative_to, attribute2=nil)
Alias for: lte
attribute2=(attribute) click to toggle source
# File lib/teacup/constraint.rb, line 161
def attribute2=(attribute)
  @attribute2 = attribute_lookup attribute
end
attribute=(attribute) click to toggle source
# File lib/teacup/constraint.rb, line 157
def attribute=(attribute)
  @attribute = attribute_lookup attribute
end
copy() click to toggle source
# File lib/teacup/constraint.rb, line 207
def copy
  copy = self.class.new(self.target, self.attribute)
  copy.relationship = self.relationship
  copy.relative_to = self.relative_to
  copy.attribute2 = self.attribute2
  copy.multiplier = self.multiplier
  copy.constant = self.constant
  copy.priority = self.priority
  copy.identifier = self.identifier
  copy
end
divided_by(multiplier) click to toggle source
# File lib/teacup/constraint.rb, line 170
def divided_by(multiplier)
  times 1.0/multiplier
end
equals(relative_to, attribute2=nil) click to toggle source

c.equals(100) c.equals(:superview, :width)

# File lib/teacup/constraint.rb, line 127
def equals(relative_to, attribute2=nil)
  self.set_relationship(NSLayoutRelationEqual, relative_to, attribute2)
end
gte(relative_to, attribute2=nil) click to toggle source
# File lib/teacup/constraint.rb, line 137
def gte(relative_to, attribute2=nil)
  self.set_relationship(NSLayoutRelationGreaterThanOrEqual, relative_to, attribute2)
end
Also aliased as: at_least, is_at_least
inspect() click to toggle source
# File lib/teacup/constraint.rb, line 219
def inspect
  "#<#{self.class.name} ##{object_id.to_s(16)}" +
  " target=#{target.inspect}" +
  " attribute=#{attribute_reverse(attribute).inspect}" +
  " relationship=#{relationship_reverse(relationship).inspect}" +
  " relative_to=#{relative_to.inspect}" +
  " attribute2=#{attribute_reverse(attribute2).inspect}" +
  " multiplier=#{multiplier.inspect}" +
  " constant=#{constant.inspect}" +
  ">"
end
is_at_least(relative_to, attribute2=nil)
Alias for: gte
is_at_most(relative_to, attribute2=nil)
Alias for: lte
lte(relative_to, attribute2=nil) click to toggle source
# File lib/teacup/constraint.rb, line 131
def lte(relative_to, attribute2=nil)
  self.set_relationship(NSLayoutRelationLessThanOrEqual, relative_to, attribute2)
end
Also aliased as: at_most, is_at_most
minus(constant) click to toggle source
# File lib/teacup/constraint.rb, line 189
def minus(constant)
  plus -constant
end
nslayoutconstraint() click to toggle source
# File lib/teacup/constraint.rb, line 231
def nslayoutconstraint
  nsconstraint = NSLayoutConstraint.constraintWithItem( self.target,
                              attribute: self.attribute,
                              relatedBy: self.relationship,
                                 toItem: self.relative_to,
                              attribute: self.attribute2,
                             multiplier: self.multiplier,
                               constant: self.constant
                                       )
  nsconstraint.priority = priority_lookup(self.priority)
  nsconstraint.setIdentifier(self.identifier) if self.identifier
  return nsconstraint
end
plus(constant) click to toggle source

If no relationship has been set, the “use case” here is:

c.plus(10).equals(:view, :x)

Which is the same as

c.equals(:view, :x).minus(10)
# File lib/teacup/constraint.rb, line 181
def plus(constant)
  if not self.relationship
    constant = -constant
  end
  self.constant += constant
  self
end
set_relationship(relation, relative_to, attribute2) click to toggle source
# File lib/teacup/constraint.rb, line 143
def set_relationship(relation, relative_to, attribute2)
  if attribute2.nil?
    self.constant = relative_to
    self.relative_to = nil
    self.attribute2 = :none
  else
    self.relative_to = relative_to
    self.attribute2 = attribute2
  end
  self.relationship = relation

  self
end
times(multiplier) click to toggle source
# File lib/teacup/constraint.rb, line 165
def times(multiplier)
  self.multiplier *= multiplier
  self
end

Private Instance Methods

attribute_lookup(attribute) click to toggle source
# File lib/teacup/constraint.rb, line 246
def attribute_lookup(attribute)
  return attribute if attribute.is_a? Fixnum
  raise "Unsupported attribute #{attribute.inspect}" unless Attributes.key? attribute
  Attributes[attribute]
end
attribute_reverse(attribute) click to toggle source
# File lib/teacup/constraint.rb, line 264
def attribute_reverse(attribute)
  Attributes.key(attribute) || :none
end
priority_lookup(priority) click to toggle source
# File lib/teacup/constraint.rb, line 252
def priority_lookup(priority)
  return priority if priority.is_a? Fixnum
  raise "Unsupported priority #{priority.inspect}" unless Priorities.key? priority
  Priorities[priority]
end
relationship_lookup(relationship) click to toggle source
# File lib/teacup/constraint.rb, line 258
def relationship_lookup(relationship)
  return relationship if relationship.is_a? Fixnum
  raise "Unsupported relationship #{relationship.inspect}" unless Relationships.key? relationship
  Relationships[relationship]
end
relationship_reverse(relationship) click to toggle source
# File lib/teacup/constraint.rb, line 268
def relationship_reverse(relationship)
  Relationships.key(relationship)
end