Module: Structure::JSON

Defined in:
lib/structure/json.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
# File 'lib/structure/json.rb', line 9

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#as_json(options = nil) ⇒ JSON

Converts structure to its JSON representation

Parameters:

  • options (Hash) (defaults to: nil)

Returns:

  • (JSON)

    a JSON representation of the structure



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/structure/json.rb', line 28

def as_json(options = nil)
  subset = if options
    if only = options[:only]
      @attributes.slice(*Array.wrap(only))
    elsif except = options[:except]
      @attributes.except(*Array.wrap(except))
    else
      @attributes.dup
    end
  else
    @attributes.dup
  end

  { ::JSON.create_id => self.class.name }.
    merge(subset)
end

#to_json(*args) ⇒ JSON

Converts structure to its JSON representation

Parameters:

  • args (Hash)

Returns:

  • (JSON)

    a JSON representation of the structure



17
18
19
20
21
# File 'lib/structure/json.rb', line 17

def to_json(*args)
  { ::JSON.create_id => self.class.name }.
    merge(@attributes).
    to_json(*args)
end