Module: Myxy::Resource

Included in:
Calendar, Event
Defined in:
lib/myxy/resource.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Getter/Setter for resource



75
76
77
78
79
80
81
82
83
# File 'lib/myxy/resource.rb', line 75

def method_missing(method, *args, &block)
  yield if block
  if /^(\w+)=$/ =~ method
    set_attribute($1, args[0])
  else
    nil unless @attributes[method.to_sym]
  end
  @attributes[method.to_sym]
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



7
8
9
# File 'lib/myxy/resource.rb', line 7

def attributes
  @attributes
end

Class Method Details

.included(base) ⇒ Object



70
71
72
# File 'lib/myxy/resource.rb', line 70

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#base_pathObject



13
14
15
# File 'lib/myxy/resource.rb', line 13

def base_path
  Utils.collection_path self.class.name
end

#initialize(attributes = {}) ⇒ Object



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

def initialize(attributes = {})
  @attributes = Utils.normalize_attributes(attributes)
end

#save(params = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/myxy/resource.rb', line 23

def save(params = nil)
  update_params(params) if params
  if id
    uri = "#{base_path}/#{id}/"
    Myxy.put(uri.to_s, params: attributes)
  else
    Myxy.post(base_path, params: attributes)
  end
end

#set_attribute(attribute, value) ⇒ Object



85
86
87
# File 'lib/myxy/resource.rb', line 85

def set_attribute(attribute, value)
  @attributes[attribute.to_sym] = value if valid_attribute?(attribute)
end

#update_params(params) ⇒ Object



17
18
19
20
21
# File 'lib/myxy/resource.rb', line 17

def update_params(params)
  params.each do |key, value|
    set_attribute(key, value)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/myxy/resource.rb', line 97

def valid?
  mandatory_attributes.each do |attribute|
    return false unless @attributes.key? attribute
  end
  true
end

#valid_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/myxy/resource.rb', line 89

def valid_attribute?(attribute)
  valid_attributes.include?(attribute.to_sym)
end

#valid_attributesObject



93
94
95
# File 'lib/myxy/resource.rb', line 93

def valid_attributes
  @valid_attributes ||= mandatory_attributes.inject(other_attributes, :<<)
end