Class: Scjson::Types::ScriptProps

Inherits:
Object
  • Object
show all
Defined in:
lib/scjson/types.rb

Overview

inline executable script.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ ScriptProps

Instantiate a new ScriptProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1568
1569
1570
1571
1572
# File 'lib/scjson/types.rb', line 1568

def initialize(**kwargs)
  @src = kwargs.fetch(:src, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
  @content = kwargs.fetch(:content, [])
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



1565
1566
1567
# File 'lib/scjson/types.rb', line 1565

def content
  @content
end

#other_attributesObject

Returns the value of attribute other_attributes.



1565
1566
1567
# File 'lib/scjson/types.rb', line 1565

def other_attributes
  @other_attributes
end

#srcObject

Returns the value of attribute src.



1565
1566
1567
# File 'lib/scjson/types.rb', line 1565

def src
  @src
end

Class Method Details

.from_hash(data) ⇒ ScriptProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
# File 'lib/scjson/types.rb', line 1577

def self.from_hash(data)
  raise ArgumentError, 'Expected Hash' unless data.is_a?(Hash)

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:src] = normalized.fetch('src', nil)
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  kwargs[:content] = Array(normalized.fetch('content', []))
  new(**kwargs)
end

.from_json(json) ⇒ ScriptProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1591
1592
1593
1594
# File 'lib/scjson/types.rb', line 1591

def self.from_json(json)
  parsed = JSON.parse(json)
  from_hash(parsed)
end

Instance Method Details

#to_hashHash

Convert the object to a Hash suitable for JSON serialization.

Returns:

  • (Hash)


1598
1599
1600
1601
1602
1603
1604
# File 'lib/scjson/types.rb', line 1598

def to_hash
  {
    'src' => @src,
    'other_attributes' => @other_attributes,
    'content' => @content
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1609
1610
1611
# File 'lib/scjson/types.rb', line 1609

def to_json(*opts)
  JSON.generate(to_hash, *opts)
end