Class: Selene::Line
- Inherits:
-
Struct
- Object
- Struct
- Selene::Line
- 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 everything before ‘=’
/(?<key>[^=]+)=(?<value>.*)/
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#params ⇒ Object
Returns the value of attribute params.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.parse(content_line) ⇒ Object
Parse a content line into a line object.
-
.parse_params(params_string) ⇒ Object
Parse a param string into a hash.
-
.split(string) ⇒ Object
Split a string into content lines.
Instance Method Summary collapse
- #begin_component? ⇒ Boolean
- #end_component? ⇒ Boolean
-
#initialize(name, params, value) ⇒ Line
constructor
A new instance of Line.
- #params? ⇒ Boolean
- #rrule ⇒ Object
- #value_with_params ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(name, params, value) ⇒ Line
Returns a new instance of Line.
44 45 46 47 48 |
# File 'lib/selene/line.rb', line 44 def initialize(name, params, value) self.name = name.downcase self.params = params || {} self.value = value end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/selene/line.rb', line 2 def name @name end |
#params ⇒ Object
Returns the value of attribute params
2 3 4 |
# File 'lib/selene/line.rb', line 2 def params @params end |
#value ⇒ Object
Returns the value of attribute value
2 3 4 |
# File 'lib/selene/line.rb', line 2 def value @value end |
Class Method Details
.parse(content_line) ⇒ Object
Parse a content line into a line object
26 27 28 29 30 |
# File 'lib/selene/line.rb', line 26 def self.parse(content_line) content_line.match(/#{NAME}#{PARAMS}?:#{VALUE}/) do |match| return new(match[:name], parse_params(match[:params]), match[:value]) end end |
.parse_params(params_string) ⇒ Object
Parse a param string into a hash
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/selene/line.rb', line 33 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) ⇒ Object
Split a string into content lines
20 21 22 23 |
# File 'lib/selene/line.rb', line 20 def self.split(string) delimiter = string.scan(/\r\n?|\n/).first || "\r\n" string.gsub(/#{delimiter}\s/, '').split(delimiter).map { |line| parse(line) } end |
Instance Method Details
#begin_component? ⇒ Boolean
50 51 52 |
# File 'lib/selene/line.rb', line 50 def begin_component? name == 'begin' end |
#end_component? ⇒ Boolean
54 55 56 |
# File 'lib/selene/line.rb', line 54 def end_component? name == 'end' end |
#params? ⇒ Boolean
58 59 60 |
# File 'lib/selene/line.rb', line 58 def params? params && !params.empty? end |
#rrule ⇒ Object
66 67 68 |
# File 'lib/selene/line.rb', line 66 def rrule Hash[values.map { |values| k, v = values.split('=', 2); [k.downcase, v] }] end |
#value_with_params ⇒ Object
62 63 64 |
# File 'lib/selene/line.rb', line 62 def value_with_params params? ? [value, params] : value end |
#values ⇒ Object
70 71 72 |
# File 'lib/selene/line.rb', line 70 def values value.split(';') end |