Module: Jasonette::Properties

Included in:
Base
Defined in:
lib/jasonette/core/properties.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
# File 'lib/jasonette/core/properties.rb', line 12

def self.included base
  base.send :extend, ClassMethods
end

Instance Method Details

#klass_for_property(name) ⇒ Object



31
32
33
34
35
36
# File 'lib/jasonette/core/properties.rb', line 31

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_propertiesObject



53
54
55
56
57
58
59
60
# File 'lib/jasonette/core/properties.rb', line 53

def merge_properties
  properties.each do |property_name|
    ivar = instance_variable_get(:"@#{property_name}")
    next if ivar.nil? || ivar.empty?
    @attributes[property_name.to_s] ||= {}
    @attributes[property_name.to_s].merge! ivar.attributes!
  end
end

#prop(name) ⇒ Object



20
21
22
# File 'lib/jasonette/core/properties.rb', line 20

def prop name
  instance_variable_get("@#{name}")
end

#propertiesObject



16
17
18
# File 'lib/jasonette/core/properties.rb', line 16

def properties
  self.class.properties
end

#properties_empty?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/jasonette/core/properties.rb', line 24

def properties_empty?
  properties.all? do |ivar_name|
    ivar = instance_variable_get(:"@#{ivar_name}")
    ivar.nil? || ivar.empty?
  end
end

#property_get!(name) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/jasonette/core/properties.rb', line 38

def property_get! name
  ivar_name = "@#{name}"
  if instance_variable_get(ivar_name).nil?
    klass = klass_for_property name
    instance_variable_set(ivar_name, klass.new(@context))
  end
  instance_variable_get(ivar_name)
end

#property_sender(target, name, *args, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jasonette/core/properties.rb', line 62

def property_sender target, name, *args, &block
  if block_given?
    target.send name, *args, &block
  elsif args.one? && args.first.is_a?(Hash)
    target.send name do
      args.first.each{ |key, value| json.set! key, value.to_s }
    end
  else
    raise "unhandled definition!"
  end
  self
end

#property_set!(name, *args, &block) ⇒ Object



47
48
49
50
51
# File 'lib/jasonette/core/properties.rb', line 47

def property_set! name, *args, &block
  ivar = property_get! name
  return ivar unless block_given?
  ivar.tap { |v| v.encode(&block) }
end