Class: CSS::MarginProperty

Inherits:
Property show all
Defined in:
lib/css/properties/margin_property.rb

Direct Known Subclasses

PaddingProperty

Instance Method Summary collapse

Methods inherited from Property

#<<, #==, #[], create, #get, #initialize, #inspect, #method_missing, #value

Methods included from Normalize

#normalize_property_name

Constructor Details

This class inherits a constructor from CSS::Property

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class CSS::Property

Instance Method Details

#nameObject



3
4
5
# File 'lib/css/properties/margin_property.rb', line 3

def name
  'margin'
end

#to_sObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/css/properties/margin_property.rb', line 7

def to_s
  top = @properties['top']
  right = @properties['right']
  bottom = @properties['bottom']
  left = @properties['left']

  if top && right && bottom && left
    if [top, right, bottom, left] == Array.new(4) { top }
      top
    elsif [top, bottom] == Array.new(2) { top } && [left, right] == Array.new(2) { left }
      [top, left].join(' ')
    elsif [top, bottom] != Array.new(2) { top } && [left, right] == Array.new(2) { left }
      [top, left, bottom].join(' ')
    else
      [top, right, bottom, left].join(' ')
    end
  end
end

#to_styleObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/css/properties/margin_property.rb', line 26

def to_style
  top = @properties['top']
  right = @properties['right']
  bottom = @properties['bottom']
  left = @properties['left']

  if top && right && bottom && left
    value = if [top, right, bottom, left] == Array.new(4) { top }
      top
    elsif [top, bottom] == Array.new(2) { top } && [left, right] == Array.new(2) { left }
      [top, left].join(' ')
    elsif [top, bottom] != Array.new(2) { top } && [left, right] == Array.new(2) { left }
      [top, left, bottom].join(' ')
    else
      [top, right, bottom, left].join(' ')
    end
    [name, value].join(':')
  else
    default_properties.keys.map { |prop| @properties[prop] ? ["#{name}-#{prop}", @properties[prop]].join(':') : nil }.compact.join(';')
  end
end