Class: Enolib::Context
- Inherits:
-
Object
- Object
- Enolib::Context
- Defined in:
- lib/enolib/context.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#input ⇒ Object
Returns the value of attribute input.
-
#line_count ⇒ Object
Returns the value of attribute line_count.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#reporter ⇒ Object
Returns the value of attribute reporter.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #comment(element) ⇒ Object
- #elements(section) ⇒ Object
- #entries(fieldset) ⇒ Object
-
#initialize(input, locale: Locales::En, reporter: TextReporter, source: nil) ⇒ Context
constructor
A new instance of Context.
- #items(list) ⇒ Object
- #raw(element) ⇒ Object
- #value(element) ⇒ Object
Constructor Details
#initialize(input, locale: Locales::En, reporter: TextReporter, source: nil) ⇒ Context
Returns a new instance of Context.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/enolib/context.rb', line 7 def initialize(input, locale: Locales::En, reporter: TextReporter, source: nil) @input = input = locale @reporter = reporter @source = source @document = { elements: [], type: :document } = [] Parser.new(self).run end |
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
5 6 7 |
# File 'lib/enolib/context.rb', line 5 def document @document end |
#input ⇒ Object
Returns the value of attribute input.
5 6 7 |
# File 'lib/enolib/context.rb', line 5 def input @input end |
#line_count ⇒ Object
Returns the value of attribute line_count.
5 6 7 |
# File 'lib/enolib/context.rb', line 5 def line_count @line_count end |
#messages ⇒ Object
Returns the value of attribute messages.
5 6 7 |
# File 'lib/enolib/context.rb', line 5 def end |
#meta ⇒ Object
Returns the value of attribute meta.
5 6 7 |
# File 'lib/enolib/context.rb', line 5 def end |
#reporter ⇒ Object
Returns the value of attribute reporter.
5 6 7 |
# File 'lib/enolib/context.rb', line 5 def reporter @reporter end |
#source ⇒ Object
Returns the value of attribute source.
5 6 7 |
# File 'lib/enolib/context.rb', line 5 def source @source end |
Instance Method Details
#comment(element) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/enolib/context.rb', line 23 def comment(element) unless element.has_key?(:computed_comment) element[:computed_comment] = if !element.has_key?(:comments) nil elsif element[:comments].length == 1 element[:comments].first[:comment] else first_non_empty_line_index = nil last_non_empty_line_index = nil shared_indent = Float::INFINITY element[:comments].each_with_index do |comment, index| next unless comment.has_key?(:comment) first_non_empty_line_index ||= index last_non_empty_line_index = index indent = comment[:ranges][:comment][RANGE_BEGIN] - comment[:ranges][:line][RANGE_BEGIN] shared_indent = indent if indent < shared_indent end if first_non_empty_line_index non_empty_lines = element[:comments][first_non_empty_line_index..last_non_empty_line_index] non_empty_lines.map do |comment| if !comment.has_key?(:comment) '' elsif (comment[:ranges][:comment][RANGE_BEGIN] - comment[:ranges][:line][RANGE_BEGIN]) == shared_indent comment[:comment] else (' ' * (comment[:ranges][:comment][RANGE_BEGIN] - comment[:ranges][:line][RANGE_BEGIN] - shared_indent)) + comment[:comment] end end.join("\n") else nil end end end element[:computed_comment] end |
#elements(section) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/enolib/context.rb', line 66 def elements(section) return elements(section[:mirror]) if section.has_key?(:mirror) unless section.has_key?(:computed_elements) section[:computed_elements] = section[:elements] section[:computed_elements_map] = {} section[:computed_elements].each do |element| if section[:computed_elements_map].has_key?(element[:key]) section[:computed_elements_map][element[:key]].push(element) else section[:computed_elements_map][element[:key]] = [element] end end if section.has_key?(:extend) copied_elements = elements(section[:extend]).reject { |element| section[:computed_elements_map].has_key?(element[:key]) } section[:computed_elements] = copied_elements + section[:computed_elements] copied_elements.each do |element| if section[:computed_elements_map].has_key?(element[:key]) section[:computed_elements_map][element[:key]].push(element) else section[:computed_elements_map][element[:key]] = [element] end end end end section[:computed_elements] end |
#entries(fieldset) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/enolib/context.rb', line 99 def entries(fieldset) return entries(fieldset[:mirror]) if fieldset.has_key?(:mirror) unless fieldset.has_key?(:computed_entries) fieldset[:computed_entries] = fieldset[:entries] fieldset[:computed_entries_map] = {} fieldset[:computed_entries].each do |entry| if fieldset[:computed_entries_map].has_key?(entry[:key]) fieldset[:computed_entries_map][entry[:key]].push(entry) else fieldset[:computed_entries_map][entry[:key]] = [entry] end end if fieldset.has_key?(:extend) copied_entries = entries(fieldset[:extend]).reject { |entry| fieldset[:computed_entries_map].has_key?(entry[:key]) } fieldset[:computed_entries] = copied_entries + fieldset[:computed_entries] copied_entries.each do |entry| if fieldset[:computed_entries_map].has_key?(entry[:key]) fieldset[:computed_entries_map][entry[:key]].push(entry) else fieldset[:computed_entries_map][entry[:key]] = [entry] end end end end fieldset[:computed_entries] end |
#items(list) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/enolib/context.rb', line 132 def items(list) if list.has_key?(:mirror) items(list[:mirror]) elsif list.has_key?(:extend) items(list[:extend]) + list[:items] elsif list.has_key?(:items) list[:items] else [] end end |
#raw(element) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/enolib/context.rb', line 144 def raw(element) # TODO: In other implementations there is only one 'field' type # here we get a) symbols, b) including :multiline_field_begin result = { type: element[:type] } # TODO: Revisit to think through the case of a present but empty comment result[:comment] = comment(element) if element.has_key?(:comments) case element[:type] when :empty_element result[:key] = element[:key] when :field result[:key] = element[:key] result[:value] = value(element) when :list_item result[:value] = value(element) when :fieldset_entry result[:key] = element[:key] result[:value] = value(element) when :multiline_field_begin result[:key] = element[:key] result[:value] = value(element) when :list result[:key] = element[:key] result[:items] = items(element).map { |item| raw(item) } when :fieldset result[:key] = element[:key] result[:entries] = entries(element).map { |entry| raw(entry) } when :section result[:key] = element[:key] result[:elements] = elements(element).map { |section_element| raw(section_element) } when :document result[:elements] = elements(element).map { |section_element| raw(section_element) } end result end |
#value(element) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/enolib/context.rb', line 182 def value(element) unless element.has_key?(:computed_value) return value(element[:mirror]) if element.has_key?(:mirror) element[:computed_value] = nil if element[:type] == :multiline_field_begin if element.has_key?(:lines) element[:computed_value] = @input[ element[:lines][0][:ranges][:line][0]...element[:lines][-1][:ranges][:line][1] ] end else element[:computed_value] = element[:value] if element.has_key?(:value) if element.has_key?(:continuations) unapplied_spacing = nil element[:continuations].each do |continuation| if element[:computed_value] == nil element[:computed_value] = continuation[:value] if continuation.has_key?(:value) unapplied_spacing = nil elsif !continuation.has_key?(:value) unapplied_spacing ||= continuation.has_key?(:spaced) elsif continuation.has_key?(:spaced) || unapplied_spacing element[:computed_value] += ' ' + continuation[:value] unapplied_spacing = nil else element[:computed_value] += continuation[:value] end end end end end element[:computed_value] end |