Class: Selene::ComponentBuilder

Inherits:
Object
  • Object
show all
Includes:
ComponentValidator
Defined in:
lib/selene/component_builder.rb

Overview

This is the base class for all component builders.

Properties are specified one per property with optional rules, e.g.:

property ‘version’, required: true, multiple: false

If :required is truthy, a component is not valid without that property. If :multiple is falsy, a component can only have one of that property

Custom rules can be implemented by overriding can_add?(property) or valid?

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ComponentValidator

#can_add?, #can_contain?, #error, included, inherited, #multiple?, #properties, #required?, #valid?

Constructor Details

#initialize(name) ⇒ ComponentBuilder

Returns a new instance of ComponentBuilder.



21
22
23
24
25
# File 'lib/selene/component_builder.rb', line 21

def initialize(name)
  @name = name
  @component = Hash.new { |h, k| h[k] = [] }
  @errors = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#componentObject

Returns the value of attribute component.



19
20
21
# File 'lib/selene/component_builder.rb', line 19

def component
  @component
end

#errorsObject

Returns the value of attribute errors.



19
20
21
# File 'lib/selene/component_builder.rb', line 19

def errors
  @errors
end

#name(property) ⇒ Object

Returns the value of attribute name.



19
20
21
# File 'lib/selene/component_builder.rb', line 19

def name
  @name
end

#parentObject

Returns the value of attribute parent.



19
20
21
# File 'lib/selene/component_builder.rb', line 19

def parent
  @parent
end

Instance Method Details

#add(name, builder) ⇒ Object



27
28
29
# File 'lib/selene/component_builder.rb', line 27

def add(name, builder)
  @component[name] << builder.component
end

#contains?(property) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/selene/component_builder.rb', line 80

def contains?(property)
  @component.key?(property)
end

#parse(*properties) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/selene/component_builder.rb', line 31

def parse(*properties)
  properties.each do |property|
    case property
    when Line
      @component[name(property)] = value(property) if can_add?(property)
    when String
      Line.split(property).each { |line| parse(line) }
    else
      raise ParseError, "Cannot parse argument of type #{property.class}"
    end
  end
end

#to_ical(component) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/selene/component_builder.rb', line 44

def to_ical(component)
  lines = []
  component.each_pair do |key, value|
    keys = []
    values = []
    keys << key.upcase
    case value
    when Array
      if value[1].is_a?(Hash)
        value[1].each_pair do |pkey, pvalue|
          keys << [pkey.upcase, pvalue].join('=')
        end
        values << value[0]
      else
        values += value
      end
    when Hash
      value.each_pair do |vkey, vvalue|
        values << [vkey.upcase, vvalue].join('=')
      end
    end

    lines << [keys.join(';'), values.join(';')].join(':')
  end

  lines.join("\n")
end

#value(property) ⇒ Object



76
77
78
# File 'lib/selene/component_builder.rb', line 76

def value(property)
  property.value
end