Module: Jasonette::Properties
- Included in:
- Base
- Defined in:
- lib/jasonette/core/properties.rb
Defined Under Namespace
Modules: ClassMethods
Classes: PropertyEnum
Constant Summary
collapse
- TYPES =
[:is_many, :is_single]
- DEFAULT_IS_ARRAY =
[:items, :components, :layers]
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
42
43
44
|
# File 'lib/jasonette/core/properties.rb', line 42
def self.included base
base.send :extend, ClassMethods
end
|
Instance Method Details
#all_instance_variable_set(name, *args) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/jasonette/core/properties.rb', line 124
def all_instance_variable_set name, *args
new_klass = create_new_klass name
set_ivar(name, new_klass) if (get_ivar(name).nil? || is_many?(name))
ivar = get_ivar(name)
if is_single?(name)
new_klass.set_is_single_ivar(name, args.first)
end
if is_many?(name)
ivar_all = get_is_many_ivar(name)
set_is_many_ivar(name, ivar_all << ivar)
end
end
|
#create_new_klass(name) ⇒ Object
117
118
119
120
121
122
|
# File 'lib/jasonette/core/properties.rb', line 117
def create_new_klass name
klass = klass_for_property name
new_klass = klass.new(@context)
new_klass.parent_jasonette_set self
new_klass
end
|
#get_default_for_property(name) ⇒ Object
151
152
153
154
155
156
157
|
# File 'lib/jasonette/core/properties.rb', line 151
def get_default_for_property name
is_many_ivars = get_is_many_ivar(name)
single_default = is_single?(name) && is_many_ivars.to_a.length <= 1 ? {} : nil
many_default = is_many?(name) ? [] : nil
single_default || many_default || {}
end
|
#get_ivar(name) ⇒ Object
82
83
84
|
# File 'lib/jasonette/core/properties.rb', line 82
def get_ivar name
instance_variable_get(ivar_for_property(name))
end
|
#has_any_property_type?(name) ⇒ Boolean
66
67
68
|
# File 'lib/jasonette/core/properties.rb', line 66
def has_any_property_type? name
property_type_methods.map { |m| send(m, name) }.any?
end
|
#has_property?(name) ⇒ Boolean
70
71
72
|
# File 'lib/jasonette/core/properties.rb', line 70
def has_property? name
property_names.include?(name.to_sym)
end
|
#ivar_for_property(name) ⇒ Object
74
75
76
|
# File 'lib/jasonette/core/properties.rb', line 74
def ivar_for_property name
"@#{name}"
end
|
#klass_for_property(name) ⇒ Object
97
98
99
100
101
102
|
# File 'lib/jasonette/core/properties.rb', line 97
def klass_for_property name
name = name.to_s.camelize
klass = "#{self.class}::#{name}".constantize rescue nil
klass ||= "Jasonette::#{name}".constantize rescue Jasonette::Base
klass
end
|
#merge_properties ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/jasonette/core/properties.rb', line 159
def merge_properties
property_names.each do |property_name|
property_name = property_name.to_s
ivar = get_ivar(property_name)
next if ivar.nil? || ivar.empty?
@attributes[property_name] = get_default_for_property(property_name)
if !has_any_property_type?(property_name)
@attributes[property_name].merge! ivar.attributes!
next
end
if is_many?(property_name)
get_is_many_ivar(property_name).each do |iv|
ivar_attributes = iv.attributes!
if is_single?(property_name) && (single_ivar = iv.get_is_single_ivar(property_name))
@attributes[property_name] = @attributes[property_name].reduce({}, :merge) if ::Array === @attributes[property_name]
@attributes[property_name][single_ivar] ||= {}
@attributes[property_name][single_ivar].merge! ivar_attributes
else
if ::Hash === @attributes[property_name]
@attributes[property_name].merge! ivar_attributes
elsif DEFAULT_IS_ARRAY.map(&:to_s).include?(property_name) && ::Array === ivar_attributes
@attributes[property_name] += ivar_attributes
else
@attributes[property_name] << ivar_attributes
end
end
end
end
end
end
|
#parent_jasonette ⇒ Object
113
114
115
|
# File 'lib/jasonette/core/properties.rb', line 113
def parent_jasonette
instance_variable_get("@_parent_jasonette")
end
|
#parent_jasonette_set(klass) ⇒ Object
104
105
106
107
108
109
110
111
|
# File 'lib/jasonette/core/properties.rb', line 104
def parent_jasonette_set klass
instance_variable_set("@_parent_jasonette", klass)
(klass.instance_variables - klass.property_variables).each do |var|
instance_variable_set(var, klass.instance_variable_get(var)) unless instance_variable_get(var)
end
klass
end
|
#prop(name) ⇒ Object
86
87
88
|
# File 'lib/jasonette/core/properties.rb', line 86
def prop name
instance_variable_get("@#{name}")
end
|
#properties_empty? ⇒ Boolean
90
91
92
93
94
95
|
# File 'lib/jasonette/core/properties.rb', line 90
def properties_empty?
property_names.all? do |ivar_name|
ivar = get_ivar(ivar_name)
ivar.nil? || ivar.empty?
end
end
|
#property_get!(name, *args) ⇒ Object
140
141
142
143
|
# File 'lib/jasonette/core/properties.rb', line 140
def property_get! name, *args
all_instance_variable_set(name, *args)
get_ivar(name)
end
|
#property_names ⇒ Object
46
47
48
|
# File 'lib/jasonette/core/properties.rb', line 46
def property_names
self.class.properties.names
end
|
#property_sender(target, name, *args, &block) ⇒ Object
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/jasonette/core/properties.rb', line 192
def property_sender target, name, *args, &block
raise "unhandled definition! : use different property name then `#{name}`" if Object.new.methods.include?(name.to_sym)
if block_given?
target.set! name, _scope { block.call }
elsif args.one? && args.first.is_a?(Hash)
target.set! name, args.first
else
raise "unhandled definition!"
end
self
end
|
#property_set!(name, *args, &block) ⇒ Object
145
146
147
148
149
|
# File 'lib/jasonette/core/properties.rb', line 145
def property_set! name, *args, &block
ivar = property_get! name, *args
return ivar unless block_given?
ivar.tap { |v| v.encode(&block) }
end
|
#property_variables ⇒ Object
50
51
52
|
# File 'lib/jasonette/core/properties.rb', line 50
def property_variables
property_names.map { |i| "@#{i}".to_sym }
end
|
#set_ivar(name, value) ⇒ Object
78
79
80
|
# File 'lib/jasonette/core/properties.rb', line 78
def set_ivar name, value
instance_variable_set(ivar_for_property(name), value)
end
|