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 canonical field name (consumer names / values are reverse-translated via PlanMyStuff::IssueFieldTranslation on construction); 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})


46
47
48
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 46

def initialize(hash)
  @hash = hash
end

Class Method Details

.coerce(node) ⇒ Object

Parameters:

  • node (Hash)

Returns:

  • (Object)


34
35
36
37
38
39
40
41
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 34

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:



22
23
24
25
26
27
28
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 22

def from_graphql(nodes)
  pairs = Array.wrap(nodes).map do |node|
    canonical_name = PlanMyStuff::IssueFieldTranslation.canonical_field_name(node.dig(:field, :name))
    [canonical_name, PlanMyStuff::IssueFieldTranslation.canonical_value(canonical_name, coerce(node))]
  end
  new(pairs.to_h)
end

Instance Method Details

#[](name) ⇒ Object?

Parameters:

  • name (String, Symbol)

    field display name

Returns:

  • (Object, nil)


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

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

#eachEnumerator, void

Returns:

  • (Enumerator, void)


64
65
66
# File 'lib/plan_my_stuff/issue_field_value_set.rb', line 64

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

#to_hHash{String => Object}

Returns copy of the underlying hash.

Returns:

  • (Hash{String => Object})

    copy of the underlying hash



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

def to_h
  @hash.dup
end