Class: Teacup::Constraint

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

Constant Summary collapse

Priorities =
{
  required: 1000,  # NSLayoutPriorityRequired
  high: 750,  # NSLayoutPriorityDefaultHigh
  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.



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

def initialize(target=nil, attribute=nil)
  self.target = target
  self.attribute = attribute
  self.constant = 0
  self.multiplier = 1
  self.priority = :high
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



42
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
# File 'lib/teacup/constraint.rb', line 42

def self.from_sym(sym, relative_to=:superview)
  case sym
  when :full
    [
      Teacup::Constraint.new(:self, :left).equals(relative_to, :left),
      Teacup::Constraint.new(:self, :top).equals(relative_to, :top),
      Teacup::Constraint.new(:self, :width).equals(relative_to, :width),
      Teacup::Constraint.new(:self, :height).equals(relative_to, :height),
    ]
  when :full_width
    [
      Teacup::Constraint.new(:self, :left).equals(relative_to, :left),
      Teacup::Constraint.new(:self, :width).equals(relative_to, :width),
    ]
  when :full_height
    [
      Teacup::Constraint.new(:self, :top).equals(relative_to, :top),
      Teacup::Constraint.new(:self, :height).equals(relative_to, :height),
    ]
  when :center_x
    [
      Teacup::Constraint.new(:self, :center_x).equals(:superview, :center_x),
    ]
  when :center_y
    [
      Teacup::Constraint.new(:self, :center_y).equals(:superview, :center_y),
    ]
  when :centered
    [
      Teacup::Constraint.new(:self, :center_x).equals(:superview, :center_x),
      Teacup::Constraint.new(:self, :center_y).equals(:superview, :center_y),
    ]
  when :top
    [
      Teacup::Constraint.new(:self, :top).equals(relative_to, :top),
    ]
  when :right
    [
      Teacup::Constraint.new(:self, :right).equals(relative_to, :right),
    ]
  when :bottom
    [
      Teacup::Constraint.new(:self, :bottom).equals(relative_to, :bottom),
    ]
  when :left
    [
      Teacup::Constraint.new(:self, :left).equals(relative_to, :left),
    ]
  when :top_left
    [
      Teacup::Constraint.new(:self, :left).equals(relative_to, :left),
      Teacup::Constraint.new(:self, :top).equals(relative_to, :top),
    ]
  when :top_right
    [
      Teacup::Constraint.new(:self, :right).equals(relative_to, :right),
      Teacup::Constraint.new(:self, :top).equals(relative_to, :top),
    ]
  when :bottom_right
    [
      Teacup::Constraint.new(:self, :right).equals(relative_to, :right),
      Teacup::Constraint.new(:self, :bottom).equals(relative_to, :bottom),
    ]
  when :bottom_left
    [
      Teacup::Constraint.new(:self, :left).equals(relative_to, :left),
      Teacup::Constraint.new(:self, :bottom).equals(relative_to, :bottom),
    ]
  else
    raise "Unknown symbol #{sym.inspect}"
  end
end

Instance Method Details

#copyObject



186
187
188
189
190
191
192
193
194
195
# File 'lib/teacup/constraint.rb', line 186

def copy
  copy = Teacup::Constraint.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



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

def divided_by(multiplier)
  times 1.0/multiplier
end

#equals(relative_to, attribute2 = nil) ⇒ Object



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

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



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

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

#inspectObject



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

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



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

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

#minus(constant) ⇒ Object



175
176
177
# File 'lib/teacup/constraint.rb', line 175

def minus(constant)
  plus -constant
end

#nslayoutconstraintObject



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

def nslayoutconstraint
  NSLayoutConstraint.constraintWithItem( self.target,
                              attribute: self.attribute,
                              relatedBy: self.relationship,
                                 toItem: self.relative_to,
                              attribute: self.attribute2,
                             multiplier: self.multiplier,
                               constant: self.constant
                                       ) .tap do |nsconstraint|
    nsconstraint.priority = priority_lookup self.priority
  end
end

#plus(constant) ⇒ Object



170
171
172
173
# File 'lib/teacup/constraint.rb', line 170

def plus(constant)
  self.constant += constant
  self
end

#set_relationship(relation, relative_to, attribute2) ⇒ Object



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

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



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

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