Module: LLT::Core::Containable

Includes:
Enumerable
Defined in:
lib/llt/core/containable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



6
7
8
# File 'lib/llt/core/containable.rb', line 6

def container
  @container
end

#idObject (readonly) Also known as: n

Returns the value of attribute id.



6
7
8
# File 'lib/llt/core/containable.rb', line 6

def id
  @id
end

Class Method Details

.included(klass) ⇒ Object



85
86
87
# File 'lib/llt/core/containable.rb', line 85

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#<<(obj) ⇒ Object



15
16
17
18
# File 'lib/llt/core/containable.rb', line 15

def <<(obj)
  @container << obj
  @container.flatten!
end

#[](arg) ⇒ Object



20
21
22
# File 'lib/llt/core/containable.rb', line 20

def [](arg)
  @container[arg]
end

#all?(&blk) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/llt/core/containable.rb', line 73

def all?(&blk)
  @container.empty? ? false : super
end

#as_jsonObject



59
60
61
# File 'lib/llt/core/containable.rb', line 59

def as_json
  @string
end

#as_xmlObject



55
56
57
# File 'lib/llt/core/containable.rb', line 55

def as_xml
  @string
end

#each(&blk) ⇒ Object



69
70
71
# File 'lib/llt/core/containable.rb', line 69

def each(&blk)
  @container.each(&blk)
end

#empty?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/llt/core/containable.rb', line 81

def empty?
  @container.empty?
end

#include?(x) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/llt/core/containable.rb', line 77

def include? x
  @container.include? x
end

#initialize(string, id = nil) ⇒ Object



9
10
11
12
13
# File 'lib/llt/core/containable.rb', line 9

def initialize(string, id = nil)
  @string = string
  @container = []
  @id        = id
end

#to_sObject



24
25
26
# File 'lib/llt/core/containable.rb', line 24

def to_s
  @string
end

#to_xml(tags = nil, indexing: true, recursive: true, inline: false, id_as: 'n', attrs: {}) ⇒ Object



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
# File 'lib/llt/core/containable.rb', line 28

def to_xml(tags = nil, indexing: true,
                       recursive: true,
                       inline: false,
                       id_as: 'n',
                       attrs: {})

  # for easier recursion it's solved in a way that might
  # look awkward on first sight
  tags = Array(tags)
  tag = tags.shift || default_xml_tag
  end_of_recursion = false

  val = if recursive && all? { |e| e.respond_to?(:to_xml)}
          attrs.merge!(inline_id(tag, id_as)) if inline && indexing
          recursive_xml(tags, indexing, inline, id_as, attrs)
        else
          end_of_recursion = true
          as_xml
        end

  if inline
    end_of_recursion ? wrap_with_xml(tag, val, indexing, id_as, attrs) : val
  else
    wrap_with_xml(tag, val, indexing, id_as, attrs)
  end
end

#xml_tagstring Also known as: default_xml_tag

Returns the default xml tag defined for the instances class.

Returns:

  • (string)

    the default xml tag defined for the instances class



64
65
66
# File 'lib/llt/core/containable.rb', line 64

def xml_tag
  self.class.default_xml_tag
end