Sha256: e50d914761c14797cf3ccb58c2b5a89f74a37452d789a9b1bf797a994b02d461

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

require 'json_builder/value'
require 'json_builder/elements'

module JSONBuilder
  class Member
    attr_accessor :key, :value

    # Public: Returns a key value pair for the stored value which could
    # be an instance of JSONBuilder::Elements or JSONBuilder::Value.
    #
    # key   - Used to generate the JSON member's key. Can be a String or Symbol.
    # scope - The view scope context for any variables.
    # args  - Can be an Array or other standard Ruby value.
    # block - Yielding any block passed to the element.
    #
    # Raises JSONBuilder::MissingKeyError if the key passed is nil.
    # Returns instance of JSONBuilder::Member.
    def initialize(key, scope, *args, &block)
      raise MissingKeyError if key.nil?

      @key = key

      argument = args.shift
      if argument.is_a?(Array) || defined?(ActiveRecord::Relation) && argument.is_a?(ActiveRecord::Relation)
        @value = Elements.new(scope, argument, &block)
      else
        @value = Value.new(scope, argument, &block)
      end
    end

    # Public: Returns a key value pair for the stored value which could
    # be an instance of JSONBuilder::Elements or JSONBuilder::Value.
    #
    # Returns a String.
    def to_s
      "\"#{@key}\": #{@value}"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
json_builder-3.1.7 lib/json_builder/member.rb
json_builder-3.1.6 lib/json_builder/member.rb
json_builder-3.1.5 lib/json_builder/member.rb
json_builder-3.1.4 lib/json_builder/member.rb
json_builder-3.1.3 lib/json_builder/member.rb
json_builder-3.1.2 lib/json_builder/member.rb
json_builder-3.1.1 lib/json_builder/member.rb
json_builder-3.1.0 lib/json_builder/member.rb
json_builder-3.0.7 lib/json_builder/member.rb