Class: Rtasklib::Taskrc

Inherits:
Object
  • Object
show all
Defined in:
lib/rtasklib/taskrc.rb

Overview

A class that wraps a single Virtus domain model with a number of creation and manipulation methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rc, type = :array) ⇒ Taskrc

Generate a dynamic Virtus model, with the attributes defined by the input

Parameters:

  • rc (Hash, Pathname)

    either a hash of attribute value pairs or a Pathname to the raw taskrc file.

Raises:

  • (TypeError)

    if rc is not of type Hash, String, or Pathname

  • (RuntimeError)

    if rc is a path and does not exist on the fs



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rtasklib/taskrc.rb', line 18

def initialize rc, type=:array
  @config = Models::TaskrcModel.new().extend(Virtus.model)

  case type
  when :array
    mappable_to_model(rc)
  when :hash
    hash_to_model(rc)
  when :path
    if path_exist?(rc)
      mappable_to_model(File.open(rc))
    else
      raise RuntimeError.new("rc path does not exist on the file system")
    end
  else
    raise TypeError.new("no implicit conversion to Hash, String, or Pathname")
  end
end

Instance Attribute Details

#configObject



10
11
12
# File 'lib/rtasklib/taskrc.rb', line 10

def config
  @config
end

Instance Method Details

#get_model_attr_value(attr) ⇒ Object

Gets the current value of a given attr in the config object

Parameters:

  • attr (#to_s)

    the name for the attr, e.g. “json_array”

Returns:

  • (Object)

    the type varies depending on the type attr



162
163
164
# File 'lib/rtasklib/taskrc.rb', line 162

def get_model_attr_value attr
  config.send("#{attr.to_s}".to_sym)
end

#model_to_rcArray<String>

Serialize all attrs of the model to the taskrc format

Returns:

  • (Array<String>)

    an array of CLI formatted strings



101
102
103
# File 'lib/rtasklib/taskrc.rb', line 101

def model_to_rc
  part_of_model_to_rc(*config.attributes.keys)
end

#model_to_sString

Serialize all attrs model back to the taskrc format and reduce them to a string that can be passed directly to Execute

Returns:

  • (String)

    a CLI formatted string



120
121
122
# File 'lib/rtasklib/taskrc.rb', line 120

def model_to_s
  model_to_rc().join(" ")
end

#part_of_model_to_rc(*attrs) ⇒ Array<String>

Serialize the given attrs model back to the taskrc format

Parameters:

  • attrs (Array)

    a splat of attributes

Returns:

  • (Array<String>)

    an array of CLI formatted strings



89
90
91
92
93
94
95
# File 'lib/rtasklib/taskrc.rb', line 89

def part_of_model_to_rc *attrs
  attrs.map do |attr|
    value = get_model_attr_value attr
    hash_attr = get_rc_attr_from_hash attr.to_s
    attr = "rc.#{hash_attr}=#{value}"
  end
end

#part_of_model_to_s(*attrs) ⇒ String

Serialize the given attrs model back to the taskrc format and reduce them to a string that can be passed directly to Execute

Parameters:

  • attrs (Array)

    a splat of attributes

Returns:

  • (String)

    a CLI formatted string



111
112
113
# File 'lib/rtasklib/taskrc.rb', line 111

def part_of_model_to_s *attrs
  part_of_model_to_rc(*attrs).join(" ")
end

#set_model_attr_value(attr, value) ⇒ undefined

Modifies the value of a given attr in the config object

Parameters:

  • attr (#to_s)

    the name for the attr, e.g. “json_array”

  • attr (String)

    the value of the attr, e.g. “yes”

Returns:

  • (undefined)


153
154
155
# File 'lib/rtasklib/taskrc.rb', line 153

def set_model_attr_value attr, value
  config.send("#{attr}=".to_sym, value)
end