Class: JSONAPI::Serializable::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/serializable/link.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exposures = {}, &block) ⇒ Link

Returns a new instance of Link.



8
9
10
11
12
# File 'lib/jsonapi/serializable/link.rb', line 8

def initialize(exposures = {}, &block)
  exposures.each { |k, v| instance_variable_set("@#{k}", v) }
  static_value = instance_eval(&block)
  @_href = static_value if static_value.is_a?(String)
end

Class Method Details

.as_jsonapi(exposures = {}, &block) ⇒ Object



4
5
6
# File 'lib/jsonapi/serializable/link.rb', line 4

def self.as_jsonapi(exposures = {}, &block)
  new(exposures, &block).as_jsonapi
end

Instance Method Details

#as_jsonapiObject



51
52
53
54
55
56
57
58
# File 'lib/jsonapi/serializable/link.rb', line 51

def as_jsonapi
  @_hash ||=
    if @_meta.nil?
      @_href
    else
      { href: @_href, meta: @_meta }
    end
end

#href(value) ⇒ Object #href(&block) ⇒ Object

Overloads:

  • #href(value) ⇒ Object

    Declare the href for this link.

    Examples:

    href "http://api.example.com/users/#{@user.id}"

    Parameters:

    • value (String)

      The value of href.

  • #href(&block) ⇒ Object

    Declare the href for this link.

    Examples:

    href do
      "http://api.example.com/users/#{@user.id}"
    end

    Yield Returns:

    • (String)

      The value of href.



29
30
31
# File 'lib/jsonapi/serializable/link.rb', line 29

def href(value = nil, &block)
  @_href = block.nil? ? value : instance_eval(&block)
end

#meta(value) ⇒ Object #meta(&block) ⇒ Object

Overloads:

  • #meta(value) ⇒ Object

    Declare the meta information for this link.

    Examples:

    meta paginated: true

    Parameters:

    • value (Hash)

      The meta information hash.

  • #meta(&block) ⇒ Object

    Declare the meta information for this link.

    Examples:

    meta do
      { paginated: true }
    end

    Yield Returns:

    • (String)

      The meta information hash.



47
48
49
# File 'lib/jsonapi/serializable/link.rb', line 47

def meta(value = nil, &block)
  @_meta = block.nil? ? value : instance_eval(&block)
end