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 parameter key and value
/(?<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(line_string) ⇒ Object
convert line string into line object.
-
.parse_params(params_string) ⇒ Object
Parse a param string into a hash.
-
.split(string, &block) ⇒ Object
Split a string into content lines.
Instance Method Summary collapse
- #begin_component? ⇒ Boolean
- #component_name ⇒ Object
- #end_component? ⇒ Boolean
-
#initialize(name, value, params = {}) ⇒ Line
constructor
A new instance of Line.
- #params? ⇒ Boolean
- #rrule ⇒ Object
- #value_with_params ⇒ Object
- #values ⇒ Object
- #values_with_params ⇒ Object
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
#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(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
52 53 54 |
# File 'lib/selene/line.rb', line 52 def begin_component? name == 'begin' end |
#component_name ⇒ Object
56 57 58 |
# File 'lib/selene/line.rb', line 56 def component_name value.downcase end |
#end_component? ⇒ Boolean
60 61 62 |
# File 'lib/selene/line.rb', line 60 def end_component? name == 'end' end |
#params? ⇒ Boolean
64 65 66 |
# File 'lib/selene/line.rb', line 64 def params? !params.empty? end |
#rrule ⇒ Object
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_params ⇒ Object
68 69 70 |
# File 'lib/selene/line.rb', line 68 def value_with_params params? ? [value, params] : value end |
#values ⇒ Object
80 81 82 |
# File 'lib/selene/line.rb', line 80 def values value.split(/[;,]/) end |
#values_with_params ⇒ Object
72 73 74 |
# File 'lib/selene/line.rb', line 72 def values_with_params params? ? [values, params] : values end |