Class: Jasonette::Base

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers, Properties
Defined in:
lib/jasonette/core/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Properties

included, #klass_for_property, #merge_properties, #prop, #properties, #properties_empty?, #property_get!, #property_sender, #property_set!

Constructor Details

#initialize(context) ⇒ Base

Returns a new instance of Base.



41
42
43
44
45
# File 'lib/jasonette/core/base.rb', line 41

def initialize context
  @context = context
  @attributes = {}
  instance_eval(&::Proc.new) if ::Kernel.block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jasonette/core/base.rb', line 24

def method_missing name, *args, &block
  if ::Kernel.block_given?
    implicit_set! name, *args, &block
  else
    if properties.include? name
      return property_get! name
    else
      with_attributes { json.set! name, *args }
    end
  end
  self
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



38
39
40
# File 'lib/jasonette/core/base.rb', line 38

def attributes
  @attributes
end

#contextObject (readonly)

Returns the value of attribute context.



37
38
39
# File 'lib/jasonette/core/base.rb', line 37

def context
  @context
end

#jsonObject (readonly)

Returns the value of attribute json.



39
40
41
# File 'lib/jasonette/core/base.rb', line 39

def json
  @json
end

Instance Method Details

#action(name = "action", &block) ⇒ Object



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

def action name="action", &block
  with_attributes do
    json.set! name do
      block_given? ? instance_eval(&block) : success { type "$render" }
    end
  end
end

#attr_value(name) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/jasonette/core/base.rb', line 16

def attr_value name
  if properties.include? name
    instance_variable_get :"@#{name}"
  else
    @attributes[name.to_s]
  end
end

#attributes!Object



123
124
125
126
# File 'lib/jasonette/core/base.rb', line 123

def attributes!
  merge_properties
  @attributes
end

#empty?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/jasonette/core/base.rb', line 89

def empty?
  properties_empty? && @attributes.empty?
end

#encodeObject



84
85
86
87
# File 'lib/jasonette/core/base.rb', line 84

def encode
  instance_eval(&::Proc.new)
  self
end

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



6
7
8
9
10
11
12
13
14
# File 'lib/jasonette/core/base.rb', line 6

def implicit_set! name, *args, &block
  with_attributes do
    if properties.include? name
      property_set! name, *args, &block
    else
      json.set!(name) { instance_eval(&block) }
    end
  end
end

#inline(json) ⇒ Object



112
113
114
115
# File 'lib/jasonette/core/base.rb', line 112

def inline json
  @attributes.merge! JSON.parse(json)
  self
end

#inline!(name, *args) ⇒ Object



117
118
119
120
121
# File 'lib/jasonette/core/base.rb', line 117

def inline! name, *args
  with_attributes do
    json.partial! name, *args
  end
end

#klass(name) ⇒ Object Also known as: css_class



93
94
95
# File 'lib/jasonette/core/base.rb', line 93

def klass name
  json.set! "class", name
end

#reload!Object



66
67
68
# File 'lib/jasonette/core/base.rb', line 66

def reload!
  with_attributes { json.type "$reload" }
end

#render!Object



62
63
64
# File 'lib/jasonette/core/base.rb', line 62

def render!
  with_attributes { json.type "$render" }
end

#success(&block) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/jasonette/core/base.rb', line 70

def success &block
  with_attributes do
    if block_given?
      json.success { instance_eval(&block) }
    else
      json.success { json.type "$render" }
    end
  end
end

#target!Object



80
81
82
# File 'lib/jasonette/core/base.rb', line 80

def target!
  attributes!.to_json
end

#trigger(name, &block) ⇒ Object



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

def trigger name, &block
  with_attributes do
    json.trigger name
    instance_eval(&block) if block_given?
  end
end

#with_attributesObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jasonette/core/base.rb', line 98

def with_attributes
  if json
    instance_eval(&::Proc.new)
    return self
  end
  template = JbuilderTemplate.new(context) do |json|
    @json = json
    instance_eval(&::Proc.new)
    @json = nil
  end
  @attributes.merge! template.attributes!
  self
end