Class: Margin::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/margin/item.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data: "", type: :item, done: false, value: "", annotations: [], children: []) ⇒ Item

Returns a new instance of Item.



14
15
16
17
18
19
20
21
# File 'lib/margin/item.rb', line 14

def initialize(raw_data: "", type: :item, done: false, value: "", annotations: [], children: [])
  @raw_data = raw_data
  @type = type
  @done = done
  @value = value
  @annotations = annotations
  @children = children
end

Instance Attribute Details

#annotationsObject

Returns the value of attribute annotations.



7
8
9
# File 'lib/margin/item.rb', line 7

def annotations
  @annotations
end

#childrenObject

Returns the value of attribute children.



7
8
9
# File 'lib/margin/item.rb', line 7

def children
  @children
end

#doneObject

Returns the value of attribute done.



7
8
9
# File 'lib/margin/item.rb', line 7

def done
  @done
end

#raw_dataObject

Returns the value of attribute raw_data.



7
8
9
# File 'lib/margin/item.rb', line 7

def raw_data
  @raw_data
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/margin/item.rb', line 7

def type
  @type
end

#valueObject

Returns the value of attribute value.



7
8
9
# File 'lib/margin/item.rb', line 7

def value
  @value
end

Class Method Details

.from_line(line) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/margin/item.rb', line 55

def from_line(line)
  new raw_data: line.raw_data,
      type: line.type,
      value: line.value,
      done: line.done,
      annotations: line.annotations
end

.rootObject



51
52
53
# File 'lib/margin/item.rb', line 51

def root
  new raw_data: "root", value: "root"
end

Instance Method Details

#as_jsonObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/margin/item.rb', line 35

def as_json
  h = {}
  h[:raw_data] = raw_data
  h[:type] = type
  h[:value] = value
  h[:done] = done if type == :task
  h[:annotations] = annotations
  h[:children] = children.map(&:as_json)
  h
end

#done?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/margin/item.rb', line 27

def done?
  done
end

#root?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/margin/item.rb', line 31

def root?
  value == "root"
end

#task?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/margin/item.rb', line 23

def task?
  type == :task
end

#to_json(pretty: false) ⇒ Object



46
47
48
# File 'lib/margin/item.rb', line 46

def to_json(pretty: false)
  pretty ? JSON.pretty_generate(as_json) : JSON.generate(as_json)
end