Class: Cline::Schema

Inherits:
Shale::Mapper
  • Object
show all
Defined in:
lib/cline/schema.rb

Overview

Base class for any Cline domain object that defines some attributes that are serializable in JSON. Handle the following features:

  • Provide Shale attributes interface.
  • Automatically transforms Cline camelCase naming.
  • Keep track of extra attributes to serialize them back if needed.

Internal collapse

Internal collapse

Instance Attribute Details

#extra_attributesHash

Returns Store all extra values from a JSON parse.

Returns:

  • (Hash)

    Store all extra values from a JSON parse



14
15
16
# File 'lib/cline/schema.rb', line 14

def extra_attributes
  @extra_attributes
end

Class Method Details

.as_hash(instance, *args, **kwargs) ⇒ Hash

Get a Hash object from an instance.

Parameters:

  • instance (Schema)

    Object to serialize to a Hash

  • args (Array)

    Remaining arguments to be transferred to Shale

  • kwargs (Hash)

    Remaining kwargs to be transferred to Shale

Returns:

  • (Hash)

    Corresponding hash



57
58
59
60
61
62
# File 'lib/cline/schema.rb', line 57

def as_hash(instance, *args, **kwargs)
  complete_hash_mapping
  hash = super
  hash.merge!(instance.extra_attributes) if instance.extra_attributes
  hash
end

.cast(value) ⇒ Schema?

Cast an input value to this Schema object. Allow the attribute to be initialized directly using its Hash form.

Parameters:

  • value (Schema, Hash, nil)

    The value that could be used to initialize a new instance of this attribute.

Returns:

  • (Schema, nil)

    The corresponding instance, or nil if none.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cline/schema.rb', line 69

def cast(value)
  return nil if value.nil?

  # We expect the value to be either a Hash that can be used to initialize a new instance, or a new instance already initialized.
  if value.is_a?(self)
    value
  elsif value.is_a?(Hash)
    new(**value)
  else
    raise "Unable to cast #{value} into #{name}"
  end
end

.cline_snake_attributes(*attributes) ⇒ Object

Define the attributes that are already in snake case in Cline files

Parameters:

  • attributes (Array<Symbol>)

    List of attributes already in snake case



20
21
22
23
24
# File 'lib/cline/schema.rb', line 20

def cline_snake_attributes(*attributes)
  @snake_attributes ||= []
  @snake_attributes.concat(attributes)
  @snake_attributes.uniq!
end

.of_hash(hash, *args, **kwargs) ⇒ Schema

Parse a Hash object and instantiate the proper instance from it.

Parameters:

  • hash (Hash)

    Data

  • args (Array)

    Remaining arguments to be transferred to Shale

  • kwargs (Hash)

    Remaining kwargs to be transferred to Shale

Returns:

  • (Schema)

    Corresponding instance



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cline/schema.rb', line 32

def of_hash(hash, *args, **kwargs)
  complete_hash_mapping
  known = hash_mapping.keys.keys
  # Separate unknown attributes.
  known_hash = {}
  extra_hash = {}
  hash.each do |key, value|
    if known.include?(key)
      known_hash[key] = value
    else
      extra_hash[key] = value
    end
  end
  # Give Shale the data it knows about, without extra attributes
  instance = super(known_hash, *args, **kwargs)
  instance.extra_attributes = extra_hash unless extra_hash.empty?
  instance
end

Instance Method Details

#==(other) ⇒ Boolean

Equality check

Parameters:

  • other (Object)

    The other to check equality with

Returns:

  • (Boolean)

    True if objects are equal



139
140
141
142
# File 'lib/cline/schema.rb', line 139

def ==(other)
  other.is_a?(Schema) &&
    other.to_hash == to_hash
end

#to_cline_jsonString

Output this object as Cline JSON.

Returns:

  • (String)

    Cline JSON



116
117
118
# File 'lib/cline/schema.rb', line 116

def to_cline_json
  JSON.dump(self.class.as_hash(self))
end

#to_hashHash

Output this object as a Hash.

Returns:

  • (Hash)

    Cline JSON



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cline/schema.rb', line 123

def to_hash
  hash = self.class.attributes.to_h do |attribute|
    value = send(attribute.to_sym)
    [
      attribute,
      value.respond_to?(:to_hash) ? value.to_hash : value
    ]
  end
  hash.merge!(extra_attributes:) if extra_attributes
  hash
end