Class: Teacup::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/teacup/constraint.rb

Constant Summary collapse

Priorities =
{
  required: 1000,  # NSLayoutPriorityRequired
  high: 750,  # NSLayoutPriorityDefaultHigh
  medium: 500,
  low: 250,  # NSLayoutPriorityDefaultLow
}
Relationships =

NSLayoutPriorityDefaultLow

{}
Attributes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target = nil, attribute = nil) ⇒ Constraint

Returns a new instance of Constraint.



116
117
118
119
120
121
122
# File 'lib/teacup/constraint.rb', line 116

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

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



4
5
6
# File 'lib/teacup/constraint.rb', line 4

def attribute
  @attribute
end

#attribute2Object

Returns the value of attribute attribute2.



7
8
9
# File 'lib/teacup/constraint.rb', line 7

def attribute2
  @attribute2
end

#constantObject

Returns the value of attribute constant.



9
10
11
# File 'lib/teacup/constraint.rb', line 9

def constant
  @constant
end

#multiplierObject

Returns the value of attribute multiplier.



8
9
10
# File 'lib/teacup/constraint.rb', line 8

def multiplier
  @multiplier
end

#priority(priority = nil) ⇒ Object

Returns the value of attribute priority.



10
11
12
# File 'lib/teacup/constraint.rb', line 10

def priority
  @priority
end

#relationshipObject

Returns the value of attribute relationship.



5
6
7
# File 'lib/teacup/constraint.rb', line 5

def relationship
  @relationship
end

#relative_toObject

Returns the value of attribute relative_to.



6
7
8
# File 'lib/teacup/constraint.rb', line 6

def relative_to
  @relative_to
end

#targetObject

Returns the value of attribute target.



3
4
5
# File 'lib/teacup/constraint.rb', line 3

def target
  @target
end

Class Method Details

.from_sym(sym, relative_to = :superview) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/teacup/constraint.rb', line 43

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

Instance Method Details

#copyObject



190
191
192
193
194
195
196
197
198
199
# File 'lib/teacup/constraint.rb', line 190

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
end

#divided_by(multiplier) ⇒ Object



167
168
169
# File 'lib/teacup/constraint.rb', line 167

def divided_by(multiplier)
  times 1.0/multiplier
end

#equals(relative_to, attribute2 = nil) ⇒ Object



124
125
126
# File 'lib/teacup/constraint.rb', line 124

def equals(relative_to, attribute2=nil)
  self.set_relationship(NSLayoutRelationEqual, relative_to, attribute2)
end

#gte(relative_to, attribute2 = nil) ⇒ Object Also known as: at_least, is_at_least



134
135
136
# File 'lib/teacup/constraint.rb', line 134

def gte(relative_to, attribute2=nil)
  self.set_relationship(NSLayoutRelationGreaterThanOrEqual, relative_to, attribute2)
end

#inspectObject



201
202
203
204
205
206
207
208
209
210
211
# File 'lib/teacup/constraint.rb', line 201

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

#lte(relative_to, attribute2 = nil) ⇒ Object Also known as: at_most, is_at_most



128
129
130
# File 'lib/teacup/constraint.rb', line 128

def lte(relative_to, attribute2=nil)
  self.set_relationship(NSLayoutRelationLessThanOrEqual, relative_to, attribute2)
end

#minus(constant) ⇒ Object



179
180
181
# File 'lib/teacup/constraint.rb', line 179

def minus(constant)
  plus -constant
end

#nslayoutconstraintObject



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/teacup/constraint.rb', line 213

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)
  return nsconstraint
end

#plus(constant) ⇒ Object



171
172
173
174
175
176
177
# File 'lib/teacup/constraint.rb', line 171

def plus(constant)
  if not self.relationship
    constant = -constant
  end
  self.constant += constant
  self
end

#set_relationship(relation, relative_to, attribute2) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/teacup/constraint.rb', line 140

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) ⇒ Object



162
163
164
165
# File 'lib/teacup/constraint.rb', line 162

def times(multiplier)
  self.multiplier *= multiplier
  self
end