Class: Youtrack::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/youtrack/models/base.rb

Defined Under Namespace

Classes: Field

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Base

Returns a new instance of Base.



86
87
88
89
90
91
# File 'lib/youtrack/models/base.rb', line 86

def initialize(attrs = {})
  attrs.each do |k, v|
    next unless respond_to?(k.to_s + "=")
    send(k.to_s + "=", v)
  end
end

Class Attribute Details

.fieldsObject (readonly)

Returns the value of attribute fields.



38
39
40
# File 'lib/youtrack/models/base.rb', line 38

def fields
  @fields
end

Class Method Details

.all_fieldsObject



40
41
42
# File 'lib/youtrack/models/base.rb', line 40

def all_fields
  fields
end

.field(name, extended: false) ⇒ Object



48
49
50
51
52
# File 'lib/youtrack/models/base.rb', line 48

def field(name, extended: false)
  @fields ||= []
  @fields << Field.new(name: name, extended: extended)
  register_field_as_attr(@fields.last)
end

.from_json(json) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/youtrack/models/base.rb', line 70

def from_json(json)
  if json.is_a?(Array)
    json.map { |item| from_json(item) }
  else
    self.new.tap do |instance|
      json.each do |key, value|
        field = fields.find { |f| f.name == key }
        next unless field.present?
        field_value = field.model.present? && value.present? ? field.model.from_json(value) : value
        instance.send(field.attr_name + "=", field_value)
      end
    end
  end
end

.has_many(name, model, extended: true) ⇒ Object



54
55
56
57
58
# File 'lib/youtrack/models/base.rb', line 54

def has_many(name, model, extended: true)
  @fields ||= []
  @fields << Field.new(name: name, extended: extended, model: model, type: :many)
  register_field_as_attr(@fields.last)
end

.has_one(name, model, extended: true) ⇒ Object



60
61
62
63
64
# File 'lib/youtrack/models/base.rb', line 60

def has_one(name, model, extended: true)
  @fields ||= []
  @fields << Field.new(name: name, extended: extended, model: model, type: :one)
  register_field_as_attr(@fields.last)
end

.minimal_fieldsObject



44
45
46
# File 'lib/youtrack/models/base.rb', line 44

def minimal_fields
  fields.reject(&:extended?)
end

.register_field_as_attr(field) ⇒ Object



66
67
68
# File 'lib/youtrack/models/base.rb', line 66

def register_field_as_attr(field)
  attr_accessor field.attr_name
end

Instance Method Details

#as_payloadObject



108
109
110
# File 'lib/youtrack/models/base.rb', line 108

def as_payload
  to_h.compact.to_json
end

#to_hObject Also known as: to_hash



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/youtrack/models/base.rb', line 93

def to_h
  self.class.all_fields.map do |field|
    value = send(field.attr_name)
    if field.model.present?
      if value.is_a?(Array)
        value = value.map(&:to_h)
      elsif value.present?
        value = value.to_h
      end
    end
    [field.attr_name, value]
  end.to_h
end