Class: Heading

Inherits:
Object
  • Object
show all
Defined in:
lib/values/heading.rb

Instance Method Summary collapse

Constructor Details

#initialize(values = nil) ⇒ Heading

Returns a new instance of Heading.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/values/heading.rb', line 4

def initialize values = nil
  @attributes = ImmutableSet.new
  
  if values.is_a? Attribute
    @attributes = @attributes.add values
  elsif values.is_a? ImmutableSet
    @attributes = values
  elsif values.is_a? Array
    @attributes = ImmutableSet.new values
  elsif values.is_a? Hash
    @attributes = @attributes.add(Attribute.new(values))
  elsif values.is_a? Heading
    values.get_attributes.each do |attribute|
      @attributes = @attributes.add(attribute)
    end
  elsif values.nil?
    # Nil is the same as a no params
  else
    raise 'Invalid parameter, expected a hash with name and type or a attribute'
  end
  
  # Validate
  @attributes.each do |a|
    raise "Invalid input, all values must be an attribute or hash that creats an attribute" unless a.is_a? Attribute
  end
end

Instance Method Details

#==(object) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/values/heading.rb', line 60

def == object
  if object.equal?(self)
    true
  elsif !self.class.equal?(object.class)
    false
  else
    self.get_attributes.eql?(object.get_attributes)
  end
end

#[](attribute_name) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/values/heading.rb', line 80

def [] attribute_name
  @attributes.to_a.each do |value|
    if value.name == attribute_name.to_s
      return value
    end
  end
  
  nil
end

#add(values) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/values/heading.rb', line 40

def add values
  if values.is_a? Attribute
    Heading.new(@attributes.add(values))
  elsif values.is_a? Hash
    Heading.new(@attributes.add(Attribute.new(values)))
  elsif values.is_a? Heading
    toReturn = Heading.new(self)
    values.get_attributes.each do |attribute|
      toReturn = toReturn.add(attribute)
    end
    toReturn
  else
    throw "Unknown argument #{values.class}"
  end
end

#countObject



115
116
117
# File 'lib/values/heading.rb', line 115

def count
  @attributes.count
end

#each(&block) ⇒ Object



74
75
76
77
78
# File 'lib/values/heading.rb', line 74

def each &block
  @attributes.each do |value|
    block.call value
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/values/heading.rb', line 70

def eql? other
  self == other
end

#firstObject



119
120
121
# File 'lib/values/heading.rb', line 119

def first 
  @attributes.to_a.first
end

#hashObject



56
57
58
# File 'lib/values/heading.rb', line 56

def hash
  @attributes.hash
end

#namesObject



31
32
33
34
35
36
37
38
# File 'lib/values/heading.rb', line 31

def names
  toReturn = []
  @attributes.each do |attribute|
    toReturn << attribute.name
  end
  
  toReturn
end

#remove(attribute_or_name) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/values/heading.rb', line 97

def remove attribute_or_name
  if attribute_or_name.is_a? Attribute
    Heading.new(@attributes.remove(attribute_or_name))
  else
    result = nil
    @attributes.to_a.each do |value|
      if value.name == attribute_or_name.to_s
        result = value
      end
    end
    if result
      Heading.new(@attributes.remove(result))
    else
      Heading.new @attributes
    end
  end
end

#rename(from, to) ⇒ Object



90
91
92
93
94
95
# File 'lib/values/heading.rb', line 90

def rename from,to
  throw "Missing from" if self[from].nil?
  throw "to already exists" unless self[to].nil?
  
  Heading.new(@attributes).remove(from).add(:name => to, :type => self[from].type)
end