Class: Cat::Style::Position

Inherits:
Object
  • Object
show all
Defined in:
lib/cat/style/position.rb

Direct Known Subclasses

Rounded

Instance Method Summary collapse

Constructor Details

#initialize(top: nil, right: nil, bottom: nil, left: nil) ⇒ Position

Returns a new instance of Position.



4
5
6
7
8
9
# File 'lib/cat/style/position.rb', line 4

def initialize(top: nil, right: nil, bottom: nil, left: nil)
  @top = validate(top)
  @right = validate(right)
  @bottom = validate(bottom)
  @left = validate(left)
end

Instance Method Details

#to_sObject



18
19
20
21
22
23
24
25
26
# File 'lib/cat/style/position.rb', line 18

def to_s
  %{
position: absolute;
#{@top ? "top: #{@top};" : ""}
#{@right ? "right: #{@right};" : ""}
#{@bottom ? "bottom: #{@bottom};" : ""}
#{@left ? "left: #{@left};" : ""}
  }
end

#validate(value) ⇒ Object



11
12
13
14
15
16
# File 'lib/cat/style/position.rb', line 11

def validate(value)
  return nil if value.nil?
  return "#{value}px" if value.is_a?(Integer)
  return "0px" unless (value =~ /px|%|em|rem$/) > 0
  value
end