Class: FlexmlsApi::Models::Base

Inherits:
Object
  • Object
show all
Extended by:
Paginate
Defined in:
lib/flexmls_api/models/base.rb

Overview

API Model Base class

Intended to be a lot like working with ActiveResource, this class adds most of the basic active model type niceties.

Constant Summary

Constants included from Paginate

Paginate::DEFAULT_PAGE_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Paginate

collect, paginate, per_page

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



40
41
42
43
44
45
# File 'lib/flexmls_api/models/base.rb', line 40

def initialize(attributes={})
  @attributes = {}
  @errors = []
  @changed = []
  load(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *arguments) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/flexmls_api/models/base.rb', line 65

def method_missing(method_symbol, *arguments)
  method_name = method_symbol.to_s

  if method_name =~ /(=|\?)$/
    case $1
    when "=" 
      write_attribute($`, arguments.first)
      # TODO figure out a nice way to present setters for the standard fields
    when "?" 
      if attributes.include?($`)
        attributes[$`] ? true : false
      else
        raise NoMethodError
      end
    end 
  else
    return attributes[method_name] if attributes.include?(method_name)
    super # GTFO
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#changedObject (readonly)

Returns the value of attribute changed.



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

def changed
  @changed
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

Class Method Details

.connectionObject



33
34
35
# File 'lib/flexmls_api/models/base.rb', line 33

def self.connection
  FlexmlsApi.client
end

.count(options = {}) ⇒ Object



61
62
63
# File 'lib/flexmls_api/models/base.rb', line 61

def self.count(options={})
  connection.get(path, options.merge({:_pagination=>"count"}))
end

.element_nameObject

Name of the resource as related to the path name



13
14
15
16
# File 'lib/flexmls_api/models/base.rb', line 13

def self.element_name
  # TODO I'd love to pull in active model at this point to provide default naming
  @element_name ||= "resource"
end

.element_name=(name) ⇒ Object



18
19
20
# File 'lib/flexmls_api/models/base.rb', line 18

def self.element_name=(name)
  @element_name = name
end

.first(options = {}) ⇒ Object



57
58
59
# File 'lib/flexmls_api/models/base.rb', line 57

def self.first(options={})
  get(options).first
end

.get(options = {}) ⇒ Object



53
54
55
# File 'lib/flexmls_api/models/base.rb', line 53

def self.get(options={})
  collect(connection.get(path, options))
end

.pathObject



29
30
31
# File 'lib/flexmls_api/models/base.rb', line 29

def self.path
  "#{prefix}#{element_name}"
end

.prefixObject

Resource path prefix, prepended to the url



23
24
25
# File 'lib/flexmls_api/models/base.rb', line 23

def self.prefix
  @prefix ||= "/"
end

.prefix=(prefix) ⇒ Object



26
27
28
# File 'lib/flexmls_api/models/base.rb', line 26

def self.prefix=(prefix)
  @prefix = prefix
end

Instance Method Details

#connectionObject



36
37
38
# File 'lib/flexmls_api/models/base.rb', line 36

def connection
  self.class.connection
end

#load(attributes) ⇒ Object



47
48
49
50
51
# File 'lib/flexmls_api/models/base.rb', line 47

def load(attributes)
  attributes.each do |key,val|
    @attributes[key.to_s] = val
  end
end

#parse_id(uri) ⇒ Object



103
104
105
# File 'lib/flexmls_api/models/base.rb', line 103

def parse_id(uri)
  uri[/\/.*\/(.+)$/, 1]
end

#respond_to?(method_symbol, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/flexmls_api/models/base.rb', line 86

def respond_to?(method_symbol, include_private=false)
  if super
    return true
  else
    method_name = method_symbol.to_s

    if method_name =~ /=$/
      true
    elsif method_name =~ /(\?)$/
      attributes.include?($`)
    else
      attributes.include?(method_name)
    end

  end
end