Class: Trello::BasicData

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = {}) ⇒ BasicData

Returns a new instance of BasicData.



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

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

Class Method Details

.find(path, id) ⇒ Object



10
11
12
# File 'lib/trello/basic_data.rb', line 10

def find(path, id)
  Client.get("/#{path}/#{id}").json_into(self)
end

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



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/trello/basic_data.rb', line 51

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.get("/#{resource}/#{id}/#{name}", options).json_into(klass)
      MultiAssociation.new(self, resources).proxy
    end
  end
end

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



40
41
42
43
44
45
46
47
48
49
# File 'lib/trello/basic_data.rb', line 40

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
      klass.find(self.send(ident))
    end
  end
end

.register_attributes(*names) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/trello/basic_data.rb', line 15

def self.register_attributes(*names)
  options = { :readonly => [] }
  options.merge!(names.pop) if names.last.kind_of? Hash

  # Defines the attribute getter and setters.
  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

Instance Method Details

#==(other) ⇒ Object

Two objects are equal if their id methods are equal.



80
81
82
# File 'lib/trello/basic_data.rb', line 80

def ==(other)
  id == other.id
end

#refresh!Object

Refresh the contents of our object.



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

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

#update_fields(fields) ⇒ Object

Raises:

  • (NotImplementedError)


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

def update_fields(fields)
  raise NotImplementedError, "#{self.class} does not implement update_fields."
end