Class: Representative::Json

Inherits:
Base
  • Object
show all
Defined in:
lib/representative/json.rb

Constant Summary collapse

DEFAULT_ATTRIBUTE_PREFIX =
"@".freeze
DEFAULT_INDENTATION =

two spaces

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#current_subject, #representing

Constructor Details

#initialize(subject = nil, options = {}) {|_self| ... } ⇒ Json

Returns a new instance of Json.

Yields:

  • (_self)

Yield Parameters:



12
13
14
15
16
17
18
19
20
# File 'lib/representative/json.rb', line 12

def initialize(subject = nil, options = {})
  super(subject, options)
  @buffer = []
  @indent_level = 0
  @indent_string = resolve_indentation(options.fetch(:indentation, DEFAULT_INDENTATION))
  @attribute_prefix = options.fetch(:attribute_prefix, DEFAULT_ATTRIBUTE_PREFIX)
  now_at :beginning_of_buffer
  yield self if block_given?
end

Instance Attribute Details

#attribute_prefixObject (readonly)

Returns the value of attribute attribute_prefix.



22
23
24
# File 'lib/representative/json.rb', line 22

def attribute_prefix
  @attribute_prefix
end

Instance Method Details

#attribute(name, value_generator = name) ⇒ Object



42
43
44
# File 'lib/representative/json.rb', line 42

def attribute(name, value_generator = name)
  element(attribute_prefix + name.to_s, value_generator)
end

#comment(text) ⇒ Object



80
81
82
83
84
# File 'lib/representative/json.rb', line 80

def comment(text)
  new_item
  emit("// " + text)
  now_at :end_of_comment
end

#element(name, *args, &block) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/representative/json.rb', line 24

def element(name, *args, &block)

   = @inspector.(current_subject, name)
  attributes = args.extract_options!.merge()

  subject_of_element = if args.empty?
    @inspector.get_value(current_subject, name)
  else
    args.shift
  end

  raise ArgumentError, "too many arguments" unless args.empty?

  label(name)
  value(subject_of_element, attributes, &block)

end

#list_of(name, *args, &block) ⇒ Object

Raises:

  • (ArgumentError)


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

def list_of(name, *args, &block)
  options = args.extract_options!
  list_subject = args.empty? ? name : args.shift
  raise ArgumentError, "too many arguments" unless args.empty?
  list_attributes = options[:list_attributes]
  raise ArgumentError, "list_attributes #{list_attributes} not supported for json representation" if list_attributes
  item_attributes = options[:item_attributes] || {}

  items = resolve_value(list_subject)
  label(name)
  inside "[", "]" do
    items.each do |item|
      new_item
      value(item, item_attributes, &block)
    end
  end
end

#to_jsonObject



86
87
88
89
# File 'lib/representative/json.rb', line 86

def to_json
  emit("\n") if indenting?
  @buffer.join
end

#to_sObject



91
92
93
# File 'lib/representative/json.rb', line 91

def to_s
  to_json
end

#value(subject, attributes = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/representative/json.rb', line 64

def value(subject, attributes = {})
  representing(subject) do
    if block_given? && !current_subject.nil?
      inside "{", "}" do
        attributes.each do |name, value_generator|
          attribute(name, value_generator)
        end
        yield current_subject
      end
    else
      emit(encode(current_subject))
    end
  end
  now_at :end_of_item
end