Class: Dayman::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dayman/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, attributes: {}) ⇒ Resource

Returns a new instance of Resource.



46
47
48
49
# File 'lib/dayman/resource.rb', line 46

def initialize(id: nil, attributes: {})
  @id = id
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



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

def method_missing(name, *args, &block)
  attributes.key?(name) ? attributes[name] : super
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/dayman/resource.rb', line 4

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/dayman/resource.rb', line 4

def id
  @id
end

#relationshipsObject

Returns the value of attribute relationships.



4
5
6
# File 'lib/dayman/resource.rb', line 4

def relationships
  @relationships
end

Class Method Details

.has_many(relationship_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dayman/resource.rb', line 27

def has_many(relationship_name)
  instance_variable_name = "@#{relationship_name}".to_sym

  define_method(relationship_name) do
    if instance_variable_defined?(instance_variable_name)
      instance_variable_get(instance_variable_name)
    else
      instance_variable_set(instance_variable_name, [])
    end
  end
end

.has_one(relationship_name) ⇒ Object



23
24
25
# File 'lib/dayman/resource.rb', line 23

def has_one(relationship_name)
  attr_accessor relationship_name
end

.pathObject



19
20
21
# File 'lib/dayman/resource.rb', line 19

def path
  name.demodulize.underscore.pluralize
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/dayman/resource.rb', line 55

def respond_to_missing?(name, include_private = false)
  attributes.key?(name) || super
end