jsonize 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 787277d3b1f9c06c0e3df496dcb3402ceaf1643ee5048ac359bc7592a67487b9
4
- data.tar.gz: 1005708186a5a16ee757b5b4d0393a2af6403465c6377f4ef8defbf50d62df79
3
+ metadata.gz: bdb7d503cc06ab7caf4bad0cfb1f7162fab524a76d7cfc5d24c695f58231f5ae
4
+ data.tar.gz: 5d4ac6b92ab4f1893d7cd23609460e9f2009bd646f1df4f2878a5fa4db758465
5
5
  SHA512:
6
- metadata.gz: e469b13fd8e298a003d31438dfe78dba30cf0154732e44d3c31246c75417629383a4d6499d132a2b92c003682ccba9893ded343f2d8f241e81b8f6e678402c2c
7
- data.tar.gz: 8ac936e618c70b4eb5b14f6fec34754d2e4806225e23d0b9177f505b82efa91bbb6b66b87a38ca333b12f7026af0c36dce60700a3d892bb8956d3e77d7258afe
6
+ metadata.gz: 93ee3812b793b0dd34f88df23bc3438372d7cfb07d1ddefa72026bd9a064e37e4d5689bb13d46ae8eff5e62e90ee844f16179235d4806ec71519ff6dc87e89f1
7
+ data.tar.gz: 8830854c6b28efcdeae92aebef0b75138a6f85fc87be5016931374b7b3c2f32c2cec8a0aa133f6a14db1634d83ed5db7f2ab8be7baa40a1603000611e882809d
@@ -1,3 +1,3 @@
1
1
  module Jsonize
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/jsonize.rb CHANGED
@@ -44,12 +44,13 @@ module Jsonize
44
44
  end
45
45
  end
46
46
 
47
- def generate_json propses, externals = {}
47
+ def generate_json propses, options = {}
48
48
  propses.reduce({}) do |r, (name, props)|
49
49
  value =
50
- if props["rule"].is_a?(Proc)
51
- props["rule"][self]
52
- elsif props["rule"].is_a?(String)
50
+ if props["rule"] == '_reflection'
51
+ send(name).as_json(options[name.to_sym] || {})
52
+ elsif props["rule"].is_a?(String) # NOTE required for sidekiq key
53
+ extenrals = options[:externals]
53
54
  externals.fetch(props["rule"].to_sym) { |x| externals[props["rule"]] }
54
55
  elsif props["real_name"] != name.to_s
55
56
  read_attribute(props["real_name"]).as_json
@@ -70,7 +71,8 @@ module Jsonize
70
71
  embed_attrs,
71
72
  additional_attrs,
72
73
  external_attrs(options),
73
- options[:map] || {}
74
+ options[:map] || {},
75
+ _reflections
74
76
  ].reduce { |r, hash| r.merge(hash.map {|k,v| [k.to_sym, v] }.to_h) }
75
77
  except = options.fetch(:except, [])
76
78
  only = options.fetch(:only, self.attributes.keys.map(&:to_sym) | (options[:map] || {}).keys | embed_attrs.keys | external_attrs(options).keys)
@@ -81,19 +83,27 @@ module Jsonize
81
83
  next nil if except.include?(name.to_sym) || (only & [ name.to_sym, name_in.to_sym ].uniq).blank?
82
84
 
83
85
  rule = parse_rule(rule_in)
84
- #binding.pry
85
86
  [name, { "rule" => rule, "real_name" => name_in.to_s }]
86
87
  end.compact.to_h
87
88
  end
88
89
 
89
90
  def parse_rule rule_in
90
- %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
91
+ case rule_in
92
+ when TrueClass, FalseClass, NilClass
93
+ true
94
+ when ActiveRecord::Reflection::AbstractReflection
95
+ '_reflection'
96
+ when Symbol, String
97
+ rule_in.to_s
98
+ else
99
+ true
100
+ end
91
101
  end
92
102
 
93
103
  def jsonize options = {}
94
104
  attr_props = prepare_json(options)
95
105
  redisize_json(attr_props) do
96
- generate_json(attr_props, options[:externals])
106
+ generate_json(attr_props, options)
97
107
  end
98
108
  end
99
109
 
@@ -104,7 +114,7 @@ module Jsonize
104
114
 
105
115
  def as_json options = {}
106
116
  attr_props = prepare_json(options)
107
- generate_json(attr_props, options[:externals])
117
+ generate_json(attr_props, options)
108
118
  end
109
119
 
110
120
  module Relation
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel «Malo» Skrylev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-26 00:00:00.000000000 Z
11
+ date: 2022-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redisize