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.
Defined Under Namespace
Classes: InvalidData, InvalidName, InvalidType
Instance Method Summary collapse
-
#class_props ⇒ Hash
Hash of declared entity properties.
-
#default_values ⇒ Hash
Return a new hash of default values, with dynamic values resolved on each call.
-
#entity(name, type, opts = {}) ⇒ Object
Define a property that should contain another Entity or an array of other Entities.
-
#missing_props(args) ⇒ Array<Symbol>
Return array of missing required property names.
- #optional_props ⇒ Object
-
#property(name, type, opts = {}) ⇒ Object
Define a generic (string-type) property.
-
#resolv_aliases(data) ⇒ Hash
Hash with resolved aliases.
Instance Method Details
#class_props ⇒ Hash
Returns hash of declared entity properties.
16 17 18 |
# File 'lib/diaspora_federation/properties_dsl.rb', line 16 def class_props @class_props ||= {} end |
#default_values ⇒ Hash
Return a new hash of default values, with dynamic values resolved on each call
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
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
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_props ⇒ Object
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
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.
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 |