Class: JCF::CF::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Serialization, ActiveModel::Validations
Defined in:
lib/jcf/cf/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, guid: nil, relationships: nil) ⇒ Base

Returns a new instance of Base.



34
35
36
37
38
# File 'lib/jcf/cf/base.rb', line 34

def initialize(name: nil, guid: nil, relationships: nil)
  @name = name
  @guid = guid
  @relationships = Relationships.new(self, relationships)
end

Class Attribute Details

.endpointObject

Returns the value of attribute endpoint.



41
42
43
# File 'lib/jcf/cf/base.rb', line 41

def endpoint
  @endpoint
end

Instance Attribute Details

#guidObject

Returns the value of attribute guid.



18
19
20
# File 'lib/jcf/cf/base.rb', line 18

def guid
  @guid
end

#nameObject

Returns the value of attribute name.



18
19
20
# File 'lib/jcf/cf/base.rb', line 18

def name
  @name
end

#rawObject

Returns the value of attribute raw.



18
19
20
# File 'lib/jcf/cf/base.rb', line 18

def raw
  @raw
end

#relationshipsObject

Returns the value of attribute relationships.



18
19
20
# File 'lib/jcf/cf/base.rb', line 18

def relationships
  @relationships
end

Class Method Details

.all(params = {}) ⇒ Object

TODO: make this less greedy



66
67
68
69
70
# File 'lib/jcf/cf/base.rb', line 66

def all(params = {})
  params.compact!

  resources(params: params)
end

.find(guid) ⇒ Object



61
62
63
# File 'lib/jcf/cf/base.rb', line 61

def find(guid)
  new(guid: guid).populate!
end

.find_by(attrs) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jcf/cf/base.rb', line 45

def find_by(attrs)
  objects = all
  objects.find_all do |obj|
    keys = obj.attributes.keys & attrs.keys
    keys.all? do |key|
      return true if attrs[key].nil?

      obj.attributes[key].include? attrs[key]
    end
  end
end

.first(attrs) ⇒ Object



57
58
59
# File 'lib/jcf/cf/base.rb', line 57

def first(attrs)
  find_by(attrs).first
end

.format(data) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/jcf/cf/base.rb', line 76

def format(data)
  data.each_with_object({}) do |broker, h|
    broker.attributes.each do |k, v|
      h[k] ||= []
      h[k] << v
    end
  end
end

.keysObject



43
# File 'lib/jcf/cf/base.rb', line 43

def keys = new.keys

.resource_urlObject



72
73
74
# File 'lib/jcf/cf/base.rb', line 72

def resource_url
  endpoint || name.demodulize.tableize
end

Instance Method Details

#attributesObject



22
23
24
# File 'lib/jcf/cf/base.rb', line 22

def attributes
  { name: name, guid: guid, relationships: relationships }
end

#keysObject



26
27
28
# File 'lib/jcf/cf/base.rb', line 26

def keys
  attributes.keys
end

#populate!Object



110
111
112
# File 'lib/jcf/cf/base.rb', line 110

def populate!
  resource(guid)
end

#to_sObject



106
107
108
# File 'lib/jcf/cf/base.rb', line 106

def to_s
  "#{name} #{guid}"
end

#valuesObject



30
31
32
# File 'lib/jcf/cf/base.rb', line 30

def values
  attributes.values
end