Module: Jsonize

Defined in:
lib/jsonize.rb,
lib/jsonize/version.rb

Defined Under Namespace

Modules: Relation

Constant Summary collapse

JSON_ATTRS =
{
   created_at: nil,
   updated_at: nil,
}
ORMS =
{
   ActiveRecord: 'active_record'
}
VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.detect_ormObject



135
136
137
138
139
140
# File 'lib/jsonize.rb', line 135

def detect_orm
   Object.constants.each do |anc|
      orm = ORMS.keys.find {|re| /#{re}/ =~ anc.to_s }
      require("jsonize/orm/#{ORMS[orm]}") if orm
   end
end

.included(kls) ⇒ Object



131
132
133
# File 'lib/jsonize.rb', line 131

def included kls
   kls.include(Redisize)
end

Instance Method Details

#additional_attrsObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jsonize.rb', line 35

def additional_attrs
   attributes = self.instance_variable_get(:@attributes).send(:attributes)

   if attributes.is_a?(ActiveModel::LazyAttributeHash)
      attributes.send(:additional_types)
   elsif attributes.is_a?(Hash)
      attributes
   else
      raise
   end
end

#as_json(options = {}) ⇒ Object



105
106
107
108
# File 'lib/jsonize.rb', line 105

def as_json options = {}
   attr_props = prepare_json(options)
   generate_json(attr_props, options[:externals])
end

#dejsonize(options = {}) ⇒ Object



100
101
102
103
# File 'lib/jsonize.rb', line 100

def dejsonize options = {}
   attr_props = prepare_json(options)
   deredisize_json(attr_props)
end

#embed_attrsObject



27
28
29
30
31
32
33
# File 'lib/jsonize.rb', line 27

def embed_attrs
   begin
      self.class.const_get("JSON_ATTRS")
   rescue
      {}
   end
end

#external_attrs(options = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/jsonize.rb', line 15

def external_attrs options = {}
   if externals = options[:externals]
      externals.keys.map {|k| [k.to_sym, k.to_sym] }.to_h
   else
      {}
   end
end

#generate_json(propses, externals = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jsonize.rb', line 47

def generate_json propses, externals = {}
   propses.reduce({}) do |r, (name, props)|
      value =
         if props["rule"].is_a?(Proc)
            props["rule"][self]
         elsif props["rule"].is_a?(String)
            externals.fetch(props["rule"].to_sym) { |x| externals[props["rule"]] }
         elsif props["real_name"] != name.to_s
            read_attribute(props["real_name"]).as_json
         elsif props["rule"].instance_variable_get(:@value)
            props["rule"].instance_variable_get(:@value)
         elsif props["rule"]
            read_attribute(props["real_name"] || props["rule"])
         end

      r.merge(name => value)
   end
end

#instance_attrsObject



23
24
25
# File 'lib/jsonize.rb', line 23

def instance_attrs
   self.attribute_names.map {|a| [a.to_sym, true] }.to_h
end

#jsonize(options = {}) ⇒ Object



93
94
95
96
97
98
# File 'lib/jsonize.rb', line 93

def jsonize options = {}
   attr_props = prepare_json(options)
   redisize_json(attr_props) do
      generate_json(attr_props, options[:externals])
   end
end

#parse_rule(rule_in) ⇒ Object



89
90
91
# File 'lib/jsonize.rb', line 89

def parse_rule rule_in
   %w(TrueClass FalseClass NilClass Symbol).all? {|k| !rule_in.is_a?(k.constantize) } && true || rule_in.is_a?(Symbol) && rule_in.to_s || rule_in
end

#prepare_json(options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jsonize.rb', line 66

def prepare_json options = {}
   attr_hash = [
      instance_attrs,
      JSON_ATTRS,
      embed_attrs,
      additional_attrs,
      external_attrs(options),
      options[:map] || {}
   ].reduce { |r, hash| r.merge(hash.map {|k,v| [k.to_sym, v] }.to_h) }
   except = options.fetch(:except, [])
   only = options.fetch(:only, self.attributes.keys.map(&:to_sym) | (options[:map] || {}).keys | embed_attrs.keys | external_attrs(options).keys)

   attr_hash.map do |(name_in, rule_in)|
      name = /^_(?<_name>.*)/ =~ name_in && _name || name_in.to_s

      next nil if except.include?(name.to_sym) || (only & [ name.to_sym, name_in.to_sym ].uniq).blank?

      rule = parse_rule(rule_in)
      #binding.pry
      [name, { "rule" => rule, "real_name" => name_in.to_s }]
   end.compact.to_h
end