Class: ACTV::Base
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/actv/base.rb
Direct Known Subclasses
Address, AssetChannel, AssetComponent, AssetDescription, AssetImage, AssetLegacyData, AssetPrice, AssetSeoUrl, AssetStatsResult, AssetTag, AssetTopic, EventResult, Facet, FacetTerm, FacetValue, Identity, OrganizerResults, PhoneNumber, Place, Recurrence, SearchResults, Suggestion, Topic
Instance Attribute Summary collapse
-
#attrs ⇒ Object
(also: #body)
readonly
Returns the value of attribute attrs.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
84
85
86
|
# File 'lib/actv/base.rb', line 84
def initialize(attrs={})
@attrs = attrs || {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/actv/base.rb', line 101
def method_missing(meth, *args, &block)
if @attrs && @attrs.has_key?(meth)
@attrs[meth]
elsif meth.to_s.include?('=') and !args.empty?
@attrs[meth[0..-2].to_sym] = args.first
else
super
end
end
|
Instance Attribute Details
#attrs ⇒ Object
Also known as:
body
Returns the value of attribute attrs.
8
9
10
|
# File 'lib/actv/base.rb', line 8
def attrs
@attrs
end
|
Class Method Details
.self.attr_reader(attr) ⇒ Object
.self.attr_reader(attrs) ⇒ Object
Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
18
19
20
21
22
23
|
# File 'lib/actv/base.rb', line 18
def self.attr_reader(*attrs)
attrs.each do |attribute|
define_attribute_method(attribute)
define_predicate_method(attribute)
end
end
|
.define_attribute_method(key1, klass = nil, key2 = nil) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/actv/base.rb', line 52
def self.define_attribute_method(key1, klass=nil, key2=nil)
define_method(key1) do
memoize(key1) do
if klass.nil?
@attrs[key1]
else
if @attrs[key1]
if key2.nil?
ACTV.const_get(klass).new(@attrs[key1])
else
attrs = @attrs.dup
value = attrs.delete(key1)
ACTV.const_get(klass).new(value.update(key2 => attrs))
end
else
ACTV::NullObject.instance
end
end
end
end
end
|
.define_predicate_method(key1, key2 = key1) ⇒ Object
74
75
76
77
78
|
# File 'lib/actv/base.rb', line 74
def self.define_predicate_method(key1, key2=key1)
define_method(:"#{key1}?") do
!!@attrs[key2]
end
end
|
.define_uri_method(key1, key2) ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/actv/base.rb', line 44
def self.define_uri_method(key1, key2)
define_method(key1) do
memoize(key1) do
::URI.parse(@attrs[key2]) if @attrs[key2]
end
end
end
|
.from_response(response = {}) ⇒ Object
80
81
82
|
# File 'lib/actv/base.rb', line 80
def self.from_response(response={})
new(response[:body])
end
|
.object_attr_reader(klass, key1, key2 = nil) ⇒ Object
25
26
27
28
|
# File 'lib/actv/base.rb', line 25
def self.object_attr_reader(klass, key1, key2=nil)
define_attribute_method(key1, kass, key2)
define_predicate_method(key1)
end
|
.uri_attr_reader(*attrs) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/actv/base.rb', line 30
def self.uri_attr_reader(*attrs)
attrs.each do |uri_key|
array = uri_key.to_s.split("_")
index = array.index("uri")
array[index] = "url"
url_key = array.join("_").to_sym
define_uri_method(uri_key, url_key)
define_predicate_method(uri_key, url_key)
alias_method(url_key, uri_key)
alias_method("#{url_key}?", "#{uri_key}?")
end
end
|
Instance Method Details
#[](method) ⇒ Object
88
89
90
91
92
|
# File 'lib/actv/base.rb', line 88
def [](method)
send(method)
rescue NoMethodError
nil
end
|
#memoize(key, &block) ⇒ Object
94
95
96
97
98
99
|
# File 'lib/actv/base.rb', line 94
def memoize(key, &block)
ivar = :"@#{key}"
return instance_variable_get(ivar) if instance_variable_defined?(ivar)
result = block.call
instance_variable_set(ivar, result)
end
|
#respond_to?(meth, *args) ⇒ Boolean
111
112
113
114
115
116
117
|
# File 'lib/actv/base.rb', line 111
def respond_to?(meth, *args)
if @attrs && @attrs.has_key?(meth)
true
else
super
end
end
|
#to_hash ⇒ Object
Creation of object hash when sending request to soap
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/actv/base.rb', line 120
def to_hash
hash = {}
hash["attrs"] = @attrs
self.instance_variables.keep_if { |key| key != :@attrs }.each do |var|
val = self.instance_variable_get(var)
hash["attrs"][var.to_s.delete("@").to_sym] = val.to_hash if val.is_a? ACTV::Base
end
hash["attrs"]
end
|