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.1"
Class Method Summary collapse
Instance Method Summary collapse
- #additional_attrs ⇒ Object
- #as_json(options = {}) ⇒ Object
- #dejsonize(options = {}) ⇒ Object
- #embed_attrs ⇒ Object
- #external_attrs(options = {}) ⇒ Object
- #generate_json(propses, options = {}) ⇒ Object
- #instance_attrs ⇒ Object
- #jsonize(options = {}) ⇒ Object
- #parse_rule(rule_in) ⇒ Object
- #prepare_json(options = {}) ⇒ Object
Class Method Details
.detect_orm ⇒ Object
145 146 147 148 149 150 |
# File 'lib/jsonize.rb', line 145 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
141 142 143 |
# File 'lib/jsonize.rb', line 141 def included kls kls.include(Redisize) end |
Instance Method Details
#additional_attrs ⇒ Object
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
115 116 117 118 |
# File 'lib/jsonize.rb', line 115 def as_json = {} attr_props = prepare_json() generate_json(attr_props, ) end |
#dejsonize(options = {}) ⇒ Object
110 111 112 113 |
# File 'lib/jsonize.rb', line 110 def dejsonize = {} attr_props = prepare_json() deredisize_json(attr_props) end |
#embed_attrs ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/jsonize.rb', line 27 def 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 = {} if externals = [:externals] externals.keys.map {|k| [k.to_sym, k.to_sym] }.to_h else {} end end |
#generate_json(propses, options = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jsonize.rb', line 47 def generate_json propses, = {} propses.reduce({}) do |r, (name, props)| value = if props["rule"] == '_reflection' send(name).as_json([name.to_sym] || {}) elsif props["rule"].is_a?(String) # NOTE required for sidekiq key extenrals = [:externals] 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_attrs ⇒ Object
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
103 104 105 106 107 108 |
# File 'lib/jsonize.rb', line 103 def jsonize = {} attr_props = prepare_json() redisize_json(attr_props) do generate_json(attr_props, ) end end |
#parse_rule(rule_in) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jsonize.rb', line 90 def parse_rule rule_in case rule_in when TrueClass, FalseClass, NilClass true when ActiveRecord::Reflection::AbstractReflection '_reflection' when Symbol, String rule_in.to_s else true end end |
#prepare_json(options = {}) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jsonize.rb', line 67 def prepare_json = {} attr_hash = [ instance_attrs, JSON_ATTRS, , additional_attrs, external_attrs(), [:map] || {}, _reflections ].reduce { |r, hash| r.merge(hash.map {|k,v| [k.to_sym, v] }.to_h) } except = .fetch(:except, []) only = .fetch(:only, self.attributes.keys.map(&:to_sym) | ([:map] || {}).keys | .keys | external_attrs().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) [name, { "rule" => rule, "real_name" => name_in.to_s }] end.compact.to_h end |