Class: Atlas::Resource

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/atlas/resource.rb

Direct Known Subclasses

Box, BoxProvider, BoxVersion, User

Constant Summary collapse

INTERNAL_ATTRIBUTE_KEYS =
%w(@tag @url_builder).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

included

Constructor Details

#initialize(tag, hash = {}) ⇒ Resource

Returns a new instance of Resource.



11
12
13
14
15
16
# File 'lib/atlas/resource.rb', line 11

def initialize(tag, hash = {})
  @tag = tag
  @url_builder = UrlBuilder.new tag

  hash.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
end

Instance Attribute Details

#tagObject

Returns the value of attribute tag.



9
10
11
# File 'lib/atlas/resource.rb', line 9

def tag
  @tag
end

#url_builderObject

Returns the value of attribute url_builder.



9
10
11
# File 'lib/atlas/resource.rb', line 9

def url_builder
  @url_builder
end

Class Method Details

.date_accessor(*args) ⇒ Object



50
51
52
53
# File 'lib/atlas/resource.rb', line 50

def date_accessor(*args)
  attr_reader(*args)
  date_writer(*args)
end

.date_writer(*args) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/atlas/resource.rb', line 41

def date_writer(*args)
  args.each do |attr|
    define_method("#{attr}=".to_sym) do |date|
      date = date.is_a?(String) ? Time.parse(date) : date
      instance_variable_set("@#{attr}", date)
    end
  end
end

Instance Method Details

#attributesObject



25
26
27
28
29
# File 'lib/atlas/resource.rb', line 25

def attributes
  instance_variables.map do |v|
    INTERNAL_ATTRIBUTE_KEYS.include?(v.to_s) ? next : v.to_s.sub(/^@/, "")
  end.compact!
end

#inspectObject



35
36
37
38
# File 'lib/atlas/resource.rb', line 35

def inspect
  objects = to_hash.map { |k, v| "#{k}=#{v.inspect}" }.join(' ')
  "#<#{self.class.name}:#{object_id} #{objects}>"
end

#to_hashObject



31
32
33
# File 'lib/atlas/resource.rb', line 31

def to_hash
  Hash[attributes.map { |v| [v.to_sym, send(v)] }]
end

#update_with_response(response, except_keys = []) ⇒ Object



18
19
20
21
22
23
# File 'lib/atlas/resource.rb', line 18

def update_with_response(response, except_keys = [])
  # remove keys that shouldn't be included
  except_keys.each { |k| response.delete(k) }

  response.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
end