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 :another_prop, xml_name: :another_name
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



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

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.map {|name| [name, nil] }.to_h.merge(default_props).map {|name, prop|
    [name, prop.respond_to?(:call) ? prop.call : prop]
  }.to_h
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

#find_property_for_xml_name(xml_name) ⇒ Hash

Finds a property by xml_name or name

Parameters:

  • xml_name (String)

    name of the property from the received xml

Returns:

  • (Hash)

    the property data



88
89
90
# File 'lib/diaspora_federation/properties_dsl.rb', line 88

def find_property_for_xml_name(xml_name)
  class_props.keys.find {|name| name.to_s == xml_name || xml_names[name].to_s == xml_name }
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

  • :xml_name (Symbol)

    another name used for xml generation

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
# File 'lib/diaspora_federation/properties_dsl.rb', line 67

def resolv_aliases(data)
  data.map {|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
  }.to_h
end

#xml_namesSymbol

Deprecated.

Returns alias for the xml-generation/parsing

Returns:

  • (Symbol)

    alias for the xml-generation/parsing



81
82
83
# File 'lib/diaspora_federation/properties_dsl.rb', line 81

def xml_names
  @xml_names ||= {}
end