Class: ANVL::Document
- Inherits:
-
Object
- Object
- ANVL::Document
- Defined in:
- lib/anvl/document.rb
Overview
< Hash
Direct Known Subclasses
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Class Method Summary collapse
Instance Method Summary collapse
- #[](display_label, args = {}) ⇒ Object (also: #fetch)
- #[]=(display_label, value, append = false) ⇒ Object (also: #store)
-
#initialize(obj = nil) ⇒ Document
constructor
A new instance of Document.
- #push(hash) ⇒ Object (also: #<<)
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(obj = nil) ⇒ Document
Returns a new instance of Document.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/anvl/document.rb', line 10 def initialize obj = nil @entries = Array.new case obj when Hash self.push obj when String lines = obj.gsub(/\s?\n\s+/, ' ').split("\n") lines.each_with_index do |str, i| case str when /^#/ parse_comment str, i when /:/ parse_entry str, i else # something is wrong.. nil end end end if obj add_entries_methods gc! end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
8 9 10 |
# File 'lib/anvl/document.rb', line 8 def entries @entries end |
Class Method Details
.parse(str) ⇒ Object
3 4 5 6 |
# File 'lib/anvl/document.rb', line 3 def self.parse str anvl = self.new str anvl end |
Instance Method Details
#[](display_label, args = {}) ⇒ Object Also known as: fetch
61 62 63 64 65 66 |
# File 'lib/anvl/document.rb', line 61 def [] display_label, args = {} v = @entries.select { |x| x =~ display_label } v &&= v.map { |x| x.to_s } unless args[:raw] v &&= v.first if v.length == 1 v end |
#[]=(display_label, value, append = false) ⇒ Object Also known as: store
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/anvl/document.rb', line 69 def []= display_label, value, append = false label = convert_label display_label value = [value] unless value.is_a? Array @entries.delete_if { |x| x =~ label } unless append value.each do |v| case v when Hash @entries << element_class.new({ :document => self, :label => label, :value => v }.merge(v)) else @entries << element_class.new({ :document => self, :label => label, :value => v }) end end end |
#push(hash) ⇒ Object Also known as: <<
85 86 87 88 89 90 91 |
# File 'lib/anvl/document.rb', line 85 def push hash hash.each do |label, value| self.store label, value, true end gc! @entries end |
#to_h ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/anvl/document.rb', line 45 def to_h gc! h = {} @entries.map do |obj| h[(obj.display_label || obj.label).to_sym] ||= [] h[(obj.display_label || obj.label).to_sym] << obj.to_s end h.each do |label, value| h[label] = value.first if value.length == 1 end h end |
#to_s ⇒ Object
38 39 40 41 42 43 |
# File 'lib/anvl/document.rb', line 38 def to_s gc! @entries.map do |obj| obj.to_anvl end.join "\n" end |