Class: Nube::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nube/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}, new_record = true) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
# File 'lib/nube/base.rb', line 20

def initialize attrs={}, new_record=true
  @new_record    = new_record
  @attrs_changed = {}
  @errors        = {}
  @attributes    = (attrs.empty? ? self.class.empty_attributes : attrs).stringify_keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/nube/base.rb', line 114

def method_missing(name, *args, &block)
  name = name.to_s
  setter = name.end_with?('=')
  name = name[0..-2] if setter
  if @attributes.has_key?(name)
    if setter
      @attributes[name] = args[0]
      @attrs_changed[name] = args[0]
    else
      @attributes[name]
    end
  else
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



10
11
12
# File 'lib/nube/base.rb', line 10

def attributes
  @attributes
end

#attrs_changedObject

Returns the value of attribute attrs_changed.



10
11
12
# File 'lib/nube/base.rb', line 10

def attrs_changed
  @attrs_changed
end

#errorsObject

Returns the value of attribute errors.



10
11
12
# File 'lib/nube/base.rb', line 10

def errors
  @errors
end

#new_recordObject (readonly) Also known as: new_record?

Returns the value of attribute new_record.



9
10
11
# File 'lib/nube/base.rb', line 9

def new_record
  @new_record
end

Class Method Details

.do_request(method, site_options, params = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/nube/base.rb', line 100

def self.do_request(method, site_options, params={})
  site = site(site_options[:identity], self.name.split('::').last.pluralize.underscore, site_options[:action], site_options[:id])
  url = URI.parse(site)
  req = "Net::HTTP::#{method.to_s.camelize}".constantize.new(url.to_s)
  req.body = params.to_param
  req['Authorization'] = "Token token=#{token(site_options[:identity])}"
  res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
  if (res.code == "200")
    [JSON.parse(res.body, quirks_mode: true)].flatten.compact
  else
    raise res.msg + res.code
  end
end

.enum(options) ⇒ Object

alias_method :persisted?, :new_record?



90
91
92
93
94
95
96
97
98
# File 'lib/nube/base.rb', line 90

def self.enum(options)
  options.each do |attr, values|
    values.each do |value|
      define_method "#{value}?" do
        self.send(attr) == value.to_s
      end
    end
  end
end

.inherited(subclass) ⇒ Object



14
15
16
17
18
# File 'lib/nube/base.rb', line 14

def self.inherited(subclass)
  subclass.include(Site)
  subclass.include(RemoteScope)
  subclass.include(RemoteAssociation)
end

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



130
131
132
133
# File 'lib/nube/base.rb', line 130

def self.method_missing(name, *args, &block)
  super(name, *args) unless ("#{self.name}Relation".constantize.instance_methods - self.instance_methods).include?(name)
  args.empty? ? self.node.send(name) : self.node.send(name, *args)
end

Instance Method Details

#destroyObject



48
49
50
# File 'lib/nube/base.rb', line 48

def destroy
  self.class.destroy_all(id: id).first unless id.nil?
end

#identityObject



58
59
60
61
62
63
64
65
66
# File 'lib/nube/base.rb', line 58

def identity
  _identity = nil
  _identity = if defined?(IDENTITY_ATTR)
                send(IDENTITY_ATTR)
              elsif defined?(IDENTITY_SITE)
                IDENTITY_SITE
              end
  _identity.nil? ? {} : { identity: _identity }
end

#inspectObject



68
69
70
# File 'lib/nube/base.rb', line 68

def inspect
  "#<#{self.class.name} " + attributes.keys.map{|attr| "#{attr}: #{attributes[attr].inspect}" }.join(', ') + '>'
end

#save(params = {}) ⇒ Object



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

def save(params={})
  if new_record?
    remote_obj  = self.class.post(identity.merge(params), { attributes: @attributes } ).first
    @attributes = remote_obj["object"]
    @errors     = remote_obj["errors"]
    @new_record = false if @errors.empty?
  else
    @errors = self.class.put(identity.merge(params.merge(id: id)), { attributes: @attrs_changed, id: id } ).first
  end
  @errors.empty?
end

#update_attribute(attr, value) ⇒ Object



44
45
46
# File 'lib/nube/base.rb', line 44

def update_attribute(attr, value)
  update_attributes(attr => value)
end

#update_attributes(attrs = {}) ⇒ Object



39
40
41
42
# File 'lib/nube/base.rb', line 39

def update_attributes(attrs={})
  @attrs_changed.merge!(attrs)
  save
end