Module: RockingChair::Helper
- Defined in:
- lib/rocking_chair/helper.rb
Class Method Summary collapse
- .access(attr_name, doc) ⇒ Object
- .fixed_json_content(string) ⇒ Object
- .json_content(string) ⇒ Object
- .jsonfy_options(options, *keys) ⇒ Object
Class Method Details
.access(attr_name, doc) ⇒ Object
12 13 14 |
# File 'lib/rocking_chair/helper.rb', line 12 def self.access(attr_name, doc) doc.respond_to?(:_document) ? doc._document[attr_name] : doc[attr_name] end |
.fixed_json_content(string) ⇒ Object
31 32 33 34 |
# File 'lib/rocking_chair/helper.rb', line 31 def self.fixed_json_content(string) # wrap content in array, then parse and return first value ActiveSupport::JSON.decode("[#{string}]")[0] end |
.json_content(string) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rocking_chair/helper.rb', line 16 def self.json_content(string) if string && string.size < 2 # just one character --> only hashes and arrays are valid root objects fixed_json_content(string) elsif string == 'null' nil elsif string ActiveSupport::JSON.decode(string) else nil end rescue JSON::ParserError, MultiJson::DecodeError # probably: only hashes and arrays are valid root objects --> try to fix fixed_json_content(string) end |
.jsonfy_options(options, *keys) ⇒ Object
6 7 8 9 10 |
# File 'lib/rocking_chair/helper.rb', line 6 def self.(, *keys) keys.each do |key| [key] = json_content([key]) if [key] end end |