module Jasonette::Properties
Constants
- DEFAULT_IS_ARRAY
- TYPES
Public Class Methods
included(base)
click to toggle source
# File lib/jasonette/core/properties.rb, line 42 def self.included base base.send :extend, ClassMethods end
Public Instance Methods
all_instance_variable_set(name, *args)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/jasonette/core/properties.rb, line 70 def has_property? name property_names.include?(name.to_sym) end
ivar_for_property(name)
click to toggle source
# File lib/jasonette/core/properties.rb, line 74 def ivar_for_property name "@#{name}" end
klass_for_property(name)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/jasonette/core/properties.rb, line 113 def parent_jasonette instance_variable_get("@_parent_jasonette") end
parent_jasonette_set(klass)
click to toggle source
# 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)
click to toggle source
# File lib/jasonette/core/properties.rb, line 86 def prop name instance_variable_get("@#{name}") end
properties_empty?()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/jasonette/core/properties.rb, line 46 def property_names self.class.properties.names end
property_sender(target, name, *args, &block)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/jasonette/core/properties.rb, line 50 def property_variables property_names.map { |i| "@#{i}".to_sym } end
set_ivar(name, value)
click to toggle source
# File lib/jasonette/core/properties.rb, line 78 def set_ivar name, value instance_variable_set(ivar_for_property(name), value) end