Class: PlanMyStuff::IssueFieldValueSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/plan_my_stuff/issue_field_value_set.rb

Overview

Hash-like read-side view of GitHub Issue Field values on a single Issue. Returned by Issue#issue_fields. Values are coerced into Ruby types on construction: date fields come back as Date, number fields as Float, single-select fields as the option name String, and text fields as the raw String.

Access is by field display name; string and symbol keys both work. Iteration yields [name, value] pairs in the order GitHub returned them.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ IssueFieldValueSet

Returns a new instance of IssueFieldValueSet.

Parameters:

  • hash (Hash{String => Object})


42
43
44
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 42

def initialize(hash)
  @hash = hash
end

Class Method Details

.coerce(node) ⇒ Object

Parameters:

  • node (Hash)

Returns:

  • (Object)


30
31
32
33
34
35
36
37
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 30

def coerce(node)
  case node[:__typename].to_s
  when 'IssueFieldDateValue' then Date.parse(node.fetch(:value))
  when 'IssueFieldNumberValue' then node.fetch(:value).to_f
  when 'IssueFieldSingleSelectValue' then node.fetch(:name)
  else node.fetch(:value)
  end
end

.from_graphql(nodes) ⇒ PlanMyStuff::IssueFieldValueSet

Parameters:

  • nodes (Array<Hash>, nil)

    issueFieldValues.nodes from the GraphQL read query

Returns:



21
22
23
24
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 21

def from_graphql(nodes)
  pairs = Array.wrap(nodes).map { |node| [node.dig(:field, :name), coerce(node)] }
  new(pairs.to_h)
end

Instance Method Details

#[](name) ⇒ Object?

Parameters:

  • name (String, Symbol)

    field display name

Returns:

  • (Object, nil)


50
51
52
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 50

def [](name)
  @hash[name.to_s]
end

#eachEnumerator, void

Returns:

  • (Enumerator, void)


60
61
62
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 60

def each(&)
  @hash.each(&)
end

#to_hHash{String => Object}

Returns copy of the underlying hash.

Returns:

  • (Hash{String => Object})

    copy of the underlying hash



55
56
57
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 55

def to_h
  @hash.dup
end