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.



117
118
119
120
121
122
123
# 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

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

#identifier(identifier = nil) ⇒ Object

Returns the value of attribute identifier.



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

def identifier
  @identifier
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



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
115
# 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

Instance Method Details

#copyObject



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/teacup/constraint.rb', line 198

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



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

def divided_by(multiplier)
  times 1.0/multiplier
end

#equals(relative_to, attribute2 = nil) ⇒ Object



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

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



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

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

#inspectObject



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/teacup/constraint.rb', line 210

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



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

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

#minus(constant) ⇒ Object



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

def minus(constant)
  plus -constant
end

#nslayoutconstraintObject



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/teacup/constraint.rb', line 222

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



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

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

#set_relationship(relation, relative_to, attribute2) ⇒ Object



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

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



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

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