Class: Trello::BasicData

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Dirty, ActiveModel::Serializers::JSON, ActiveModel::Validations, JsonUtils
Defined in:
lib/trello/basic_data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JsonUtils

included

Constructor Details

#initialize(fields = {}) ⇒ BasicData

Returns a new instance of BasicData.



76
77
78
# File 'lib/trello/basic_data.rb', line 76

def initialize(fields = {})
  initialize_fields(fields)
end

Instance Attribute Details

#clientObject



160
161
162
# File 'lib/trello/basic_data.rb', line 160

def client
  @client ||= self.class.client
end

Class Method Details

.clientObject



70
71
72
# File 'lib/trello/basic_data.rb', line 70

def self.client
  Trello.client
end

.create(options) ⇒ Object



20
21
22
# File 'lib/trello/basic_data.rb', line 20

def create(options)
  client.create(path_name, options)
end

.find(id, params = {}) ⇒ Object



16
17
18
# File 'lib/trello/basic_data.rb', line 16

def find(id, params = {})
  client.find(path_name, id, params)
end

.many(name, opts = {}) ⇒ Object



66
67
68
# File 'lib/trello/basic_data.rb', line 66

def self.many(name, opts = {})
  AssociationBuilder::HasMany.build(self, name, opts)
end

.one(name, opts = {}) ⇒ Object



62
63
64
# File 'lib/trello/basic_data.rb', line 62

def self.one(name, opts = {})
  AssociationBuilder::HasOne.build(self, name, opts)
end

.parse(response) ⇒ Object



30
31
32
33
34
# File 'lib/trello/basic_data.rb', line 30

def parse(response)
  from_response(response).tap do |basic_data|
    yield basic_data if block_given?
  end
end

.parse_many(response) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/trello/basic_data.rb', line 36

def parse_many(response)
  from_response(response).map do |data|
    data.tap do |d|
      yield d if block_given?
    end
  end
end

.path_nameObject



12
13
14
# File 'lib/trello/basic_data.rb', line 12

def path_name
  name.split("::").last.underscore
end

.register_attrsObject



56
57
58
59
60
# File 'lib/trello/basic_data.rb', line 56

def self.register_attrs
  schema.attrs.values.each do |attribute|
    attribute.register(self)
  end
end

.save(options) ⇒ Object



24
25
26
27
28
# File 'lib/trello/basic_data.rb', line 24

def save(options)
  new(options).tap do |basic_data|
    yield basic_data if block_given?
  end.save
end

.schema(&block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/trello/basic_data.rb', line 45

def self.schema(&block)
  @schema ||= Schema.new
  return @schema unless block_given?

  @schema.instance_eval(&block)

  register_attrs

  @schema
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Two objects are equal if their id methods are equal.



148
149
150
# File 'lib/trello/basic_data.rb', line 148

def ==(other)
  self.class == other.class && id == other.id
end

#attributesObject



168
169
170
# File 'lib/trello/basic_data.rb', line 168

def attributes
  @__attributes ||= ActiveSupport::HashWithIndifferentAccess.new
end

#collection_nameObject



134
135
136
# File 'lib/trello/basic_data.rb', line 134

def collection_name
  @collection_path ||= ActiveSupport::Inflector.pluralize(element_name)
end

#collection_pathObject



126
127
128
# File 'lib/trello/basic_data.rb', line 126

def collection_path
  "/#{collection_name}"
end

#element_nameObject



138
139
140
# File 'lib/trello/basic_data.rb', line 138

def element_name
  @element_name ||= model_name.element
end

#element_pathObject



130
131
132
# File 'lib/trello/basic_data.rb', line 130

def element_path
  "/#{collection_name}/#{id}"
end

#hashObject

Delegate hash key computation to class and id pair



156
157
158
# File 'lib/trello/basic_data.rb', line 156

def hash
  [self.class, id].hash
end

#refresh!Object

Refresh the contents of our object.



143
144
145
# File 'lib/trello/basic_data.rb', line 143

def refresh!
  self.class.find(id)
end

#saveObject



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/trello/basic_data.rb', line 80

def save
  return update! if id

  payload = {}

  schema.attrs.each do |_, attribute|
    payload = attribute.build_payload_for_create(attributes, payload)
  end

  post(collection_path, payload)
end

#schemaObject



164
165
166
# File 'lib/trello/basic_data.rb', line 164

def schema
  self.class.schema
end

#update!Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/trello/basic_data.rb', line 92

def update!
  fail "Cannot save new instance." unless id

  @previously_changed = changes

  payload = {}
  changed_attrs = attributes.select {|name, _| changed.include?(name.to_s)}

  schema.attrs.each do |_, attribute|
    payload = attribute.build_payload_for_update(changed_attrs, payload)
  end

  from_response client.put(element_path, payload)

  @changed_attributes.clear if @changed_attributes.respond_to?(:clear)
  changes_applied if respond_to?(:changes_applied)

  self
end

#update_fields(fields) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/trello/basic_data.rb', line 112

def update_fields(fields)
  attrs = {}

  schema.attrs.each do |_, attribute|
    attrs = attribute.build_pending_update_attributes(fields, attrs)
  end

  attrs.each do |name, value|
    send("#{name}=", value)
  end

  self
end