Class: PassiveResource::Base

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

Constant Summary collapse

HASH_METHODS =
(ActiveSupport::HashWithIndifferentAccess.new.methods - Object.new.methods)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p = {}) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/passive_resource/base.rb', line 10

def initialize(p = {})
  p = load_from_url(p) if p.is_a?(String)
  if self.class.nestable?(p)
    @seedling = ActiveSupport::HashWithIndifferentAccess.new(p)
  elsif self.class.collection?(p)
    @seedling = self.class.many(p)
  else
    message = p.is_a?(String) ? "The data from the remote service was unparseable" : "A hash like object or a collection of hash like objects is required."
    raise PassiveResource::ParseError, message
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/passive_resource/base.rb', line 47

def method_missing(method_sym, *arguments, &block)
  accessor = method_sym.to_s.gsub(/\=$/,'')
  if @seedling.has_key?(accessor)
    accessor == method_sym.to_s ? self[accessor] : @seedling[accessor] = arguments.first
  elsif HASH_METHODS.include?(method_sym)
    @seedling.send(method_sym, *arguments, &block)
  else
    super
  end
end

Class Method Details

.callable?(obj) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/passive_resource/base.rb', line 76

def self.callable?(obj)
  ['Proc'].include?(obj.class.to_s)
end

.collection?(obj) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/passive_resource/base.rb', line 72

def self.collection?(obj)
  ['Array', 'WillPaginate::Collection'].include?(obj.class.to_s)
end

.many(ary) ⇒ Object



42
43
44
45
# File 'lib/passive_resource/base.rb', line 42

def self.many(ary)
  ary = [ary].compact.flatten
  ary.collect {|hash| nestable?(hash) ? new(hash) : hash }
end

.nestable?(obj) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.nestable?(obj)
  ['ActiveSupport::HashWithIndifferentAccess', 'HashWithIndifferentAccess', 'Hash'].include?(obj.class.to_s)
end

Instance Method Details

#[](key) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/passive_resource/base.rb', line 32

def [](key)
  if self.class.nestable?(@seedling[key])
    @seedling[key] = self.class.new(@seedling[key])
  elsif self.class.collection?(@seedling[key])
    @seedling[key] = self.class.many(@seedling[key])
  else
    @seedling[key]
  end
end

#inspectObject



58
59
60
# File 'lib/passive_resource/base.rb', line 58

def inspect
  @seedling.inspect
end

#load_from_url(url) ⇒ Object



62
63
64
65
66
# File 'lib/passive_resource/base.rb', line 62

def load_from_url(url)
  rtn = RestClient.get(url)
  res = JSON.parse(rtn) rescue nil
  res ||= Hash.from_xml(rtn) rescue nil
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/passive_resource/base.rb', line 26

def respond_to?(method)
  return true if super
  accessor = method.to_s.gsub(/\=$/,'')
  @seedling.has_key?(accessor) or HASH_METHODS.include?(method)
end

#seedlingObject



22
23
24
# File 'lib/passive_resource/base.rb', line 22

def seedling
  @seedling
end