Class: CSS::Property

Inherits:
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.



9
10
11
12
13
14
15
# File 'lib/css/property.rb', line 9

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/css/property.rb', line 102

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

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Class Method Details

.create(name, value = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/css/property.rb', line 17

def self.create(name, value = nil)
  klass = case name.to_s
  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



84
85
86
# File 'lib/css/property.rb', line 84

def <<(val)
  @value = val
end

#==(val) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/css/property.rb', line 68

def ==(val)
  if val.is_a?(Property)
    @value == val.instance_variable_get(:@value) && @properties == val.instance_variable_get(:@properties)
  else
    @value == val
  end
end

#[](property_name) ⇒ Object



92
93
94
# File 'lib/css/property.rb', line 92

def [](property_name)
  get property_name
end

#empty?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/css/property.rb', line 125

def empty?
  @value.nil? && @properties.all? { |p| p.empty? }
end

#eql?(property) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/css/property.rb', line 76

def eql?(property)
  property.is_a?(Property) && self == property
end

#get(property_name) ⇒ Object



88
89
90
# File 'lib/css/property.rb', line 88

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

#has_property?(property_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_property?(property_name)
  @properties.keys.include?(normalize_property_name(property_name))
end

#hashObject



80
81
82
# File 'lib/css/property.rb', line 80

def hash
  to_style.hash
end

#inspectObject



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

def inspect
  "#<Property #{to_style}>"
end

#nameObject



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

def name
  [@parent.try(:name), @name].compact.join('-')
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/css/property.rb', line 120

def respond_to?(method_name, include_private = false)
  property_name = normalize_property_name(method_name.to_s[-1..-1] == '=' ? method_name.to_s.chop : method_name)
  default_properties.keys.include?(property_name) || super
end

#to_sObject



56
57
58
# File 'lib/css/property.rb', line 56

def to_s
  @value || to_style
end

#to_styleObject



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

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