Module: DiasporaFederation::PropertiesDSL

Included in:
Entity
Defined in:
lib/diaspora_federation/properties_dsl.rb

Overview

Provides a simple DSL for specifying Entity properties during class definition.

Examples:

property :prop
property :optional, default: false
property :dynamic_default, default: -> { Time.now }
property :original_prop, alias: :alias_prop
entity :nested, NestedEntity
entity :multiple, [OtherEntity]

Defined Under Namespace

Classes: InvalidData, InvalidName, InvalidType

Instance Method Summary collapse

Instance Method Details

#class_propsHash

Returns hash of declared entity properties.

Returns:

  • (Hash)

    hash of declared entity properties



16
17
18
# File 'lib/diaspora_federation/properties_dsl.rb', line 16

def class_props
  @class_props ||= {}
end

#default_valuesHash

Return a new hash of default values, with dynamic values resolved on each call

Returns:

  • (Hash)

    default values



59
60
61
62
63
# File 'lib/diaspora_federation/properties_dsl.rb', line 59

def default_values
  optional_props.to_h {|name| [name, nil] }.merge(default_props).transform_values {|prop|
    prop.respond_to?(:call) ? prop.call : prop
  }
end

#entity(name, type, opts = {}) ⇒ Object

Define a property that should contain another Entity or an array of other Entities

Parameters:

  • name (Symbol)

    property name

  • type (Entity, Array<Entity>)

    Entity subclass or Array with exactly one Entity subclass constant inside

  • opts (Hash) (defaults to: {})

    further options

Options Hash (opts):

  • :default (Object, #call)

    a default value, making the property optional

Raises:



40
41
42
43
44
# File 'lib/diaspora_federation/properties_dsl.rb', line 40

def entity(name, type, opts={})
  raise InvalidType unless entity_type_valid?(type)

  define_property name, type, opts
end

#missing_props(args) ⇒ Array<Symbol>

Return array of missing required property names

Returns:

  • (Array<Symbol>)

    missing required property names



48
49
50
# File 'lib/diaspora_federation/properties_dsl.rb', line 48

def missing_props(args)
  class_props.keys - default_props.keys - optional_props - args.keys
end

#optional_propsObject



52
53
54
# File 'lib/diaspora_federation/properties_dsl.rb', line 52

def optional_props
  @optional_props ||= []
end

#property(name, type, opts = {}) ⇒ Object

Define a generic (string-type) property

Parameters:

  • name (Symbol)

    property name

  • type (Symbol)

    property type

  • opts (Hash) (defaults to: {})

    further options

Options Hash (opts):

  • :default (Object, #call)

    a default value, making the property optional

Raises:



26
27
28
29
30
# File 'lib/diaspora_federation/properties_dsl.rb', line 26

def property(name, type, opts={})
  raise InvalidType unless property_type_valid?(type)

  define_property name, type, opts
end

#resolv_aliases(data) ⇒ Hash

Returns hash with resolved aliases.

Parameters:

  • data (Hash)

    entity data

Returns:

  • (Hash)

    hash with resolved aliases



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/diaspora_federation/properties_dsl.rb', line 67

def resolv_aliases(data)
  data.to_h {|name, value|
    if class_prop_aliases.has_key? name
      prop_name = class_prop_aliases[name]
      raise InvalidData, "only use '#{name}' OR '#{prop_name}'" if data.has_key? prop_name

      [prop_name, value]
    else
      [name, value]
    end
  }
end