Class: CSS::Property

Inherits:
Object
  • Object
show all
Includes:
Normalize
Defined in:
lib/css/property.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Normalize

#normalize_property_name

Constructor Details

#initialize(*args) ⇒ Property

Returns a new instance of Property.



7
8
9
10
11
# File 'lib/css/property.rb', line 7

def initialize(*args)
  raise "Please use Property.create instead of Property.new" unless args[0] == :p
  @properties ||= {}
  init(args[1], args[2])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/css/property.rb', line 78

def method_missing(method_name, *args, &block)
  if method_name.to_s[-1..-1] == '='
    property_name = normalize_property_name(method_name.to_s.chop)
    if default_properties.keys.include?(property_name)
      @properties[property_name] = Property.new(:p, property_name, args[0])
    else
      super
    end
  else
    property_name = normalize_property_name(method_name.to_s)
    if default_properties.keys.include?(property_name)
      get(property_name)
    else
      super
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/css/property.rb', line 5

def name
  @name
end

Class Method Details

.create(name, value = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/css/property.rb', line 13

def self.create(name, value = nil)
  klass = case name
  when /^background/
    BackgroundProperty
  when /^(font|line-height)/
    FontProperty
  when /^border/
    BorderProperty
  when /^outline/
    OutlineProperty
  when /^margin/
    MarginProperty
  when /^padding/
    PaddingProperty
  when /^list-style/
    ListStyleProperty
  else
    Property
  end

  klass.new(:p, name, value)
end

Instance Method Details

#<<(merge_property) ⇒ Object



60
61
62
# File 'lib/css/property.rb', line 60

def <<(val)
  @value = val
end

#==(val) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/css/property.rb', line 52

def ==(val)
  if val.is_a?(Property)
    @value == val.value
  else
    @value == val
  end
end

#[](property_name) ⇒ Object



68
69
70
# File 'lib/css/property.rb', line 68

def [](property_name)
  get property_name
end

#get(property_name) ⇒ Object



64
65
66
# File 'lib/css/property.rb', line 64

def get(property_name)
  @properties[property_name] == default_properties[property_name] ? nil : @properties[property_name]
end

#inspectObject



44
45
46
# File 'lib/css/property.rb', line 44

def inspect
  to_s
end

#to_sObject



36
37
38
# File 'lib/css/property.rb', line 36

def to_s
  @value
end

#to_styleObject



48
49
50
# File 'lib/css/property.rb', line 48

def to_style
  [@name, @value].join(':')
end

#valueObject



40
41
42
# File 'lib/css/property.rb', line 40

def value
  to_s
end