Class: Trello::BasicData
- Inherits:
-
Object
show all
- Includes:
- ActiveModel::Dirty, ActiveModel::Serializers::JSON, ActiveModel::Validations
- Defined in:
- lib/trello/basic_data.rb
Direct Known Subclasses
Action, Attachment, Board, Card, CheckItemState, Checklist, Item, Label, LabelName, List, Member, Notification, Organization, Token
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(fields = {}) ⇒ BasicData
Returns a new instance of BasicData.
106
107
108
|
# File 'lib/trello/basic_data.rb', line 106
def initialize(fields = {})
update_fields(fields)
end
|
Instance Attribute Details
#client ⇒ Object
124
125
126
|
# File 'lib/trello/basic_data.rb', line 124
def client
@client ||= self.class.client
end
|
Class Method Details
.client ⇒ Object
98
99
100
|
# File 'lib/trello/basic_data.rb', line 98
def self.client
Trello.client
end
|
.create(options) ⇒ Object
18
19
20
|
# File 'lib/trello/basic_data.rb', line 18
def create(options)
client.create(path_name, options)
end
|
.find(id, params = {}) ⇒ Object
14
15
16
|
# File 'lib/trello/basic_data.rb', line 14
def find(id, params = {})
client.find(path_name, id, params)
end
|
.many(name, opts = {}) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/trello/basic_data.rb', line 85
def self.many(name, opts = {})
class_eval do
define_method(:"#{name}") do |*args|
options = opts.dup
resource = options.delete(:in) || self.class.to_s.split("::").last.downcase.pluralize
klass = options.delete(:via) || Trello.const_get(name.to_s.singularize.camelize)
params = options.merge(args[0] || {})
resources = client.find_many(klass, "/#{resource}/#{id}/#{name}", params)
MultiAssociation.new(self, resources).proxy
end
end
end
|
.one(name, opts = {}) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/trello/basic_data.rb', line 68
def self.one(name, opts = {})
class_eval do
define_method(:"#{name}") do |*args|
options = opts.dup
klass = options.delete(:via) || Trello.const_get(name.to_s.camelize)
ident = options.delete(:using) || :id
path = options.delete(:path)
if path
client.find(path, self.send(ident))
else
klass.find(self.send(ident))
end
end
end
end
|
.parse(response) ⇒ Object
28
29
30
31
32
|
# File 'lib/trello/basic_data.rb', line 28
def parse(response)
response.json_into(self).tap do |basic_data|
yield basic_data if block_given?
end
end
|
.parse_many(response) ⇒ Object
34
35
36
37
38
39
40
|
# File 'lib/trello/basic_data.rb', line 34
def parse_many(response)
response.json_into(self).map do |data|
data.tap do |d|
yield d if block_given?
end
end
end
|
.path_name ⇒ Object
10
11
12
|
# File 'lib/trello/basic_data.rb', line 10
def path_name
name.split("::").last.underscore
end
|
.register_attributes(*names) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/trello/basic_data.rb', line 43
def self.register_attributes(*names)
options = { :readonly => [] }
options.merge!(names.pop) if names.last.kind_of? Hash
class_eval do
define_method :attributes do
@attributes ||= names.inject({}) { |hash,k| hash.merge(k.to_sym => nil) }
end
names.each do |key|
define_method(:"#{key}") { @attributes[key] }
unless options[:readonly].include?(key.to_sym)
define_method :"#{key}=" do |val|
send(:"#{key}_will_change!") unless val == @attributes[key]
@attributes[key] = val
end
end
end
define_attribute_methods names
end
end
|
.save(options) ⇒ Object
22
23
24
25
26
|
# File 'lib/trello/basic_data.rb', line 22
def save(options)
new(options).tap do |basic_data|
yield basic_data if block_given?
end.save
end
|
Instance Method Details
#==(other) ⇒ Object
Two objects are equal if their id methods are equal.
120
121
122
|
# File 'lib/trello/basic_data.rb', line 120
def ==(other)
id == other.id
end
|
#refresh! ⇒ Object
Refresh the contents of our object.
115
116
117
|
# File 'lib/trello/basic_data.rb', line 115
def refresh!
self.class.find(id)
end
|
#update_fields(fields) ⇒ Object
110
111
112
|
# File 'lib/trello/basic_data.rb', line 110
def update_fields(fields)
raise NotImplementedError, "#{self.class} does not implement update_fields."
end
|