Class: Selene::Line

Inherits:
Struct
  • Object
show all
Defined in:
lib/selene/line.rb

Constant Summary collapse

NAME =

Match everything until we hit ‘;’ (parameter separator) or ‘:’ (value separator)

/(?<name>[^:\;]+)/
PARAMS =

Match everything until we hit ‘:’ (value separator)

/(?:\;(?<params>[^:]*))/
VALUE =

Match everything that’s left

/(?<value>.*)/
PARAM =

Match everything that is not ‘;’ (parameter separator)

/[^\;]+/
PARAM_KEY_VALUE =

Match parameter key and value

/(?<key>[^=]+)=(?<value>.*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, params = {}) ⇒ Line

Returns a new instance of Line.



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

def initialize(name, value, params = {})
  super(name.downcase, value, params)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/selene/line.rb', line 2

def name
  @name
end

#paramsObject

Returns the value of attribute params

Returns:

  • (Object)

    the current value of params



2
3
4
# File 'lib/selene/line.rb', line 2

def params
  @params
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



2
3
4
# File 'lib/selene/line.rb', line 2

def value
  @value
end

Class Method Details

.parse(line_string) ⇒ Object

convert line string into line object



30
31
32
33
34
# File 'lib/selene/line.rb', line 30

def self.parse(line_string)
  line_string.match(/#{NAME}#{PARAMS}?:#{VALUE}/) do |match|
    new(match[:name], match[:value], parse_params(match[:params]))
  end
end

.parse_params(params_string) ⇒ Object

Parse a param string into a hash



37
38
39
40
41
42
43
44
45
46
# File 'lib/selene/line.rb', line 37

def self.parse_params(params_string)
  {}.tap do |params|
    return params unless params_string
    params_string.scan(PARAM).map do |param|
      param.match(PARAM_KEY_VALUE) do |match|
        params[match[:key].downcase] = match[:value]
      end
    end
  end
end

.split(string, &block) ⇒ Object

Split a string into content lines



20
21
22
23
24
25
26
27
# File 'lib/selene/line.rb', line 20

def self.split(string, &block)
  separator = string.match(/\r\n|\r|\n/, &:to_s) || "\r\n"
  string.gsub("#{separator}\s", '').split(separator).map do |line_string|
    parse(line_string).tap do |line|
      block.call(line) if line && block
    end
  end
end

Instance Method Details

#begin_component?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/selene/line.rb', line 52

def begin_component?
  name == 'begin'
end

#component_nameObject



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

def component_name
  value.downcase
end

#end_component?Boolean

Returns:

  • (Boolean)


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

def end_component?
  name == 'end'
end

#params?Boolean

Returns:

  • (Boolean)


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

def params?
  !params.empty?
end

#rruleObject



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

def rrule
  Hash[values.map { |values| k, v = values.split('=', 2); [k.downcase, v] }]
end

#value_with_paramsObject



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

def value_with_params
  params? ? [value, params] : value
end

#valuesObject



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

def values
  value.split(/[;,]/)
end

#values_with_paramsObject



72
73
74
# File 'lib/selene/line.rb', line 72

def values_with_params
  params? ? [values, params] : values
end