Class: NokogiriStyles::Propset

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri-styles/propset.rb

Instance Method Summary collapse

Constructor Details

#initialize(style_string) ⇒ Propset

Returns a new instance of Propset.



3
4
5
6
# File 'lib/nokogiri-styles/propset.rb', line 3

def initialize(style_string)
  @properties = parse_properties(style_string)
  @mapping    = make_mapping(@properties)
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
17
# File 'lib/nokogiri-styles/propset.rb', line 14

def [](key)
  property = @mapping[key]
  property.nil? ? nil : property[:value]
end

#[]=(key, val) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/nokogiri-styles/propset.rb', line 19

def []=(key, val)
  return self.delete(key) if val.nil?
  property = @mapping[key]
  if property.nil?
    property      = {:key => key}
    @properties  << property
    @mapping[key] = property
  end
  @mapping[key][:value] = val
end

#delete(key) ⇒ Object



30
31
32
33
# File 'lib/nokogiri-styles/propset.rb', line 30

def delete(key)
  @mapping.delete(key)
  @properties.reject! { |p| p[:key] == key }
end

#to_sObject



8
9
10
11
12
# File 'lib/nokogiri-styles/propset.rb', line 8

def to_s
  @properties.
    map { |p| [p[:key], p[:value]].join(': ') }.
    join('; ')
end