Class: Jasonette::Base

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

Constant Summary

Constants included from Properties

Properties::DEFAULT_IS_ARRAY, Properties::TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Properties

#all_instance_variable_set, #create_new_klass, #get_default_for_property, #get_ivar, #has_any_property_type?, #has_property?, included, #ivar_for_property, #klass_for_property, #merge_properties, #parent_jasonette, #parent_jasonette_set, #prop, #properties_empty?, #property_get!, #property_names, #property_sender, #property_set!, #property_variables, #set_ivar

Constructor Details

#initialize(context) ⇒ Base



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

def initialize context
  @context = context
  @attributes = {}

  encode(&::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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jasonette/core/base.rb', line 21

def method_missing name, *args, &block
  if ::Kernel.block_given?
    implicit_set! name, *args, &block
  else
    if property_names.include? name
      return property_get! name
    else
      set! name, *args if args.present?
    end
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



34
35
36
# File 'lib/jasonette/core/base.rb', line 34

def attributes
  @attributes
end

#contextObject (readonly)

Returns the value of attribute context.



33
34
35
# File 'lib/jasonette/core/base.rb', line 33

def context
  @context
end

Instance Method Details

#_method(name = nil) ⇒ Object Also known as: action_method



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

def _method name = nil
  if block_given?
    set! 'method', _scope { yield }
  else
    set! 'method', name
  end
end

#array!(collection = [], *args) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jasonette/core/base.rb', line 132

def array! collection = [], *args
  array = if collection.nil?
    []
  elsif ::Kernel.block_given?
    _map_collection(collection, &::Proc.new)
  else
    _map_collection(collection) { |element| extract! element, *args }
  end

  merge! array
end

#as_json(options = nil) ⇒ Object

Fixed for below error : IOError - not opened for reading: activesupport (5.0.1) lib/active_support/core_ext/object/json.rb:130:in ‘as_json’ Eventually called by multi_json/adapter.rb:25:in ‘dump’



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

def as_json(options = nil)
  attributes!
end

#attr_value(name) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/jasonette/core/base.rb', line 13

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

#attributes!Object



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

def attributes!
  merge_properties
  @attributes
end

#empty?Boolean



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

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

#encodeObject



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

def encode
  binding = eval "self", ::Proc.new.binding
  if (binding.method(:encode).parameters.first.include?(:req) rescue false)
    binding.encode(self, &::Proc.new)
  else
    instance_eval(&::Proc.new)
  end
  self
end

#extract!(object, *attributes) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/jasonette/core/base.rb', line 144

def extract! object, *attributes
  if ::Hash === object
    _extract_hash_values(object, attributes)
  elsif Jasonette::Base === object
    _extract_hash_values(object.attributes!, attributes)
  else
    _extract_method_values(object, attributes)
  end
end

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



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

def implicit_set! name, *args, &block
  if property_names.include? name
    with_attributes { property_set! name, *args, &block }
  else
    set!(name) { encode(&block) }
  end
end

#inline(json) ⇒ Object



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

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

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



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

def klass name
  set! "class", name
end

#merge!(key) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/jasonette/core/base.rb', line 154

def merge! key
  case key
  when Jasonette::Base
    merge! key.attributes! 
  when Hash
    key.each{ |key, value| set! _key(key), value }
  when Array
    _set_value key
  end
  @attributes
end

#set!(key, value = nil, *args) ⇒ Object



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
# File 'lib/jasonette/core/base.rb', line 94

def set! key, value=nil, *args
  result = if ::Kernel.block_given?
    if !_blank?(value)
      # comments @post.comments { |comment| ... }
      # { "comments": [ { ... }, { ... } ] }
      _scope{ array! value, &::Proc.new }
    else
      # comments { ... }
      # { "comments": ... }
      _merge_block(key){ yield self }
    end
  elsif args.empty?
    if _is_collection?(value) || Jasonette::Base === value
      # person another_jasonette
      # { "person": { ...  } }
      # comments [ { content: "...", created_at: "..." } ]
      # { "comments": [ { "content": "hello", "created_at": "..." } ] }
      # comments { content: "...", created_at: "..." }
      # { "comments": [ { "content": "hello", "created_at": "..." } ] }
      _scope{ merge! value }
    else
      _key(value)
    end
  elsif _is_collection?(value)
    # comments @post.comments, :content, :created_at
    # { "comments": [ { "content": "hello", "created_at": "..." }, { "content": "world", "created_at": "..." } ] }
    _scope{ array! value, *args }
  else
    # author @post.creator, :name, :email_address
    # { "author": { "name": "David", "email_address": "[email protected]" } }
    _merge_block(key){ extract! value, *args }
  end

  _set_key_value key, result
  self
end

#with_attributesObject



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

def with_attributes
  @attributes.merge! _scope { yield self }
  self
end