Class: WaxTasks::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/wax_tasks/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Record

Returns a new instance of Record.



8
9
10
11
12
# File 'lib/wax_tasks/record.rb', line 8

def initialize(hash)
  @hash  = hash
  @pid   = @hash.dig 'pid'
  @order = @hash.dig 'order'
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



6
7
8
# File 'lib/wax_tasks/record.rb', line 6

def hash
  @hash
end

#orderObject (readonly)

Returns the value of attribute order.



6
7
8
# File 'lib/wax_tasks/record.rb', line 6

def order
  @order
end

#pidObject (readonly)

Returns the value of attribute pid.



6
7
8
# File 'lib/wax_tasks/record.rb', line 6

def pid
  @pid
end

Instance Method Details

#keep_only(fields) ⇒ Object



46
47
48
# File 'lib/wax_tasks/record.rb', line 46

def keep_only(fields)
  @hash.select! { |k, _v| fields.include? k }
end

#keysObject



22
23
24
# File 'lib/wax_tasks/record.rb', line 22

def keys
  @hash.keys
end

#lunr_normalize_valuesObject



16
17
18
# File 'lib/wax_tasks/record.rb', line 16

def lunr_normalize_values
  @hash.transform_values { |v| Utils.lunr_normalize v }
end

#order?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/wax_tasks/record.rb', line 40

def order?
  @order.is_a? String
end

#permalink?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/wax_tasks/record.rb', line 34

def permalink?
  @hash.key? 'permalink'
end

#set(key, value) ⇒ Object



28
29
30
# File 'lib/wax_tasks/record.rb', line 28

def set(key, value)
  @hash[key] = value
end

#write_to_page(dir) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wax_tasks/record.rb', line 52

def write_to_page(dir)
  raise Error::MissingPid if @pid.nil?

  path = "#{dir}/#{Utils.slug(@pid)}.md"
  if File.exist? path
    0
  else
    FileUtils.mkdir_p File.dirname(path)
    File.open(path, 'w') { |f| f.puts "#{@hash.to_yaml}---" }
    1
  end
end