Class: Gigya::GigyaDynamicImplementation

Inherits:
Object
  • Object
show all
Defined in:
lib/gigya/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/gigya/connection.rb', line 70

def method_missing(name, *args, &block)
	name = name.to_s
	gdi = clone_gdi

	if data_results == nil
		if(name[-1..-1] == "=" && args.size == 1)
			field_name = name[0..-2]

			if self.area == nil
				raise "Can't run a setter on #{area}"
			else
				function_name = "set" + field_name.camelize
				return connection.api_get(area, function_name, val)
			end
		elsif args.size == 0
			field_name = name
			function_name = "get" + field_name.camelize
			results = connection.api_get(area, function_name)
			gdi.function = function_name
			gdi.data_results = results
			gdi.data_keys = []

			return gdi
		end
	else
		field_name = name
		if field_name[-1..-1] == "="
			field_name = field_name[0..-2]
			if field_name[0..1] == "__"
				field_name = field_name[2..-1]
			else
				field_name = field_name.camelize(:lower)
			end
			setter_hash = {}
			cur_hashpoint = setter_hash
			curval = gdi.data_results
			gdi.data_keys.each do |k|
				curval = curval[k]
				if Hash === curval
					cur_hashpoint[k] = {}
					cur_hashpoint = cur_hashpoint[k]
				elsif Array === curval
					cur_hashpoint[k] = []
					cur_hashpoint = cur_hashpoint[k]
				else
					cur_hashpoint[k] = curval
					cur_hashpoint = curval
				end
			end
			cur_hashpoint[field_name] = args[0]
			setter_hash.keys.each do |k|
				val = setter_hash[k]
				if Hash === val || Array === val
					val = val.to_json
					setter_hash[k] = val
				end
			end

			return connection.api_get(area, function.gsub(/^get/, "set"), setter_hash)
		else
			if field_name[0..1] == "__" 
				# This is an escape sequence to maintain capitalization
				field_name = field_name[2..-1]
			else
				field_name = field_name.camelize(:lower) 
			end
			gdi.data_keys.push(field_name) 

			val = gdi.to_value
			if Hash === val || Array === val || val == nil
				return gdi
			else
				return val
			end
		end
	end

	super
end

Instance Attribute Details

#areaObject

Returns the value of attribute area.



24
25
26
# File 'lib/gigya/connection.rb', line 24

def area
  @area
end

#connectionObject

Returns the value of attribute connection.



24
25
26
# File 'lib/gigya/connection.rb', line 24

def connection
  @connection
end

#data_keysObject

Returns the value of attribute data_keys.



24
25
26
# File 'lib/gigya/connection.rb', line 24

def data_keys
  @data_keys
end

#data_resultsObject

Returns the value of attribute data_results.



24
25
26
# File 'lib/gigya/connection.rb', line 24

def data_results
  @data_results
end

#functionObject

Returns the value of attribute function.



24
25
26
# File 'lib/gigya/connection.rb', line 24

def function
  @function
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/gigya/connection.rb', line 62

def blank?
	to_value.blank?
end

#clone_gdiObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/gigya/connection.rb', line 26

def clone_gdi
	gdi = GigyaDynamicImplementation.new
	gdi.connection = connection
	gdi.area = area
	gdi.function = function
	gdi.data_results = data_results
	gdi.data_keys = (data_keys || []).clone

	return gdi
end

#empty?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/gigya/connection.rb', line 66

def empty?
	to_value.empty?
end

#gdi_valueObject



37
38
39
40
41
42
43
44
# File 'lib/gigya/connection.rb', line 37

def gdi_value
	current_result = data_results
	data_keys.each do |k|
		current_result = current_result[k]
		return nil if current_result == nil
	end
	return current_result
end

#nil?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/gigya/connection.rb', line 58

def nil?
	to_value.nil?
end

#to_hObject



46
47
48
# File 'lib/gigya/connection.rb', line 46

def to_h
	gdi_value
end

#to_hashObject



50
51
52
# File 'lib/gigya/connection.rb', line 50

def to_hash
	gdi_value
end

#to_valueObject



54
55
56
# File 'lib/gigya/connection.rb', line 54

def to_value
	gdi_value
end