Mark properties as optional

This commit is contained in:
Benjamin Neff 2017-07-29 23:14:51 +02:00
parent 963f32de29
commit af7b6485ac
No known key found for this signature in database
GPG key ID: 971464C3F1A90194
8 changed files with 60 additions and 30 deletions

View file

@ -24,7 +24,7 @@ module DiasporaFederation
# @!attribute [r] description
# Description of the event
# @return [String] event description
property :description, :string, default: nil
property :description, :string, optional: true
# @!attribute [r] start
# The start time of the event
@ -34,22 +34,22 @@ module DiasporaFederation
# @!attribute [r] end
# The end time of the event
# @return [String] event end
property :end, :timestamp, default: nil
property :end, :timestamp, optional: true
# @!attribute [r] all_day
# Points if the event is an all day event
# @return [Boolean] is it an all day event
property :all_day, :boolean, default: false
property :all_day, :boolean, optional: true, default: false
# @!attribute [r] timezone
# Timezone to which the event is fixed to
# @return [String] timezone
property :timezone, :string, default: nil
property :timezone, :string, optional: true
# @!attribute [r] location
# Location of the event
# @return [Entities::Location] location
entity :location, Entities::Location, default: nil
entity :location, Entities::Location, optional: true
end
end
end

View file

@ -37,13 +37,13 @@ module DiasporaFederation
# @!attribute [r] text
# @return [String] text
property :text, :string, default: nil
property :text, :string, optional: true
# @!attribute [r] status_message_guid
# Guid of a status message this photo belongs to
# @see StatusMessage#guid
# @return [String] guid
property :status_message_guid, :string, default: nil
property :status_message_guid, :string, optional: true
# @!attribute [r] height
# Photo height

View file

@ -29,7 +29,7 @@ module DiasporaFederation
property :author, :string, xml_name: :diaspora_handle
property :guid, :string
property :created_at, :timestamp, default: -> { Time.now.utc }
property :provider_display_name, :string, default: nil
property :provider_display_name, :string, optional: true
end
end
end

View file

@ -20,7 +20,7 @@ module DiasporaFederation
# @see #full_name
# @see Discovery::HCard#first_name
# @return [String] first name
property :first_name, :string, default: nil
property :first_name, :string, optional: true
# @!attribute [r] last_name
# @deprecated We decided to only use one name field, these should be removed
@ -28,38 +28,38 @@ module DiasporaFederation
# @see #full_name
# @see Discovery::HCard#last_name
# @return [String] last name
property :last_name, :string, default: nil
property :last_name, :string, optional: true
# @!attribute [r] image_url
# @see Discovery::HCard#photo_large_url
# @return [String] url to the big avatar (300x300)
property :image_url, :string, default: nil
property :image_url, :string, optional: true
# @!attribute [r] image_url_medium
# @see Discovery::HCard#photo_medium_url
# @return [String] url to the medium avatar (100x100)
property :image_url_medium, :string, default: nil
property :image_url_medium, :string, optional: true
# @!attribute [r] image_url_small
# @see Discovery::HCard#photo_small_url
# @return [String] url to the small avatar (50x50)
property :image_url_small, :string, default: nil
property :image_url_small, :string, optional: true
property :birthday, :string, default: nil
property :gender, :string, default: nil
property :bio, :string, default: nil
property :location, :string, default: nil
property :birthday, :string, optional: true
property :gender, :string, optional: true
property :bio, :string, optional: true
property :location, :string, optional: true
# @!attribute [r] searchable
# @see Discovery::HCard#searchable
# @return [Boolean] searchable flag
property :searchable, :boolean, default: true
property :searchable, :boolean, optional: true, default: true
# @!attribute [r] public
# Shows whether the profile is visible to everyone or only to contacts
# @return [Boolean] is it public
property :public, :boolean, default: false
property :public, :boolean, optional: true, default: false
property :nsfw, :boolean, default: false
property :tag_string, :string, default: nil
property :nsfw, :boolean, optional: true, default: false
property :tag_string, :string, optional: true
# @return [String] string representation of this object
def to_s

View file

@ -21,7 +21,7 @@ module DiasporaFederation
# @!attribute [r] public
# Has no meaning at the moment
# @return [Boolean] public
property :public, :boolean, default: true # always true? (we only reshare public posts)
property :public, :boolean, optional: true, default: true # always true? (we only reshare public posts)
# @return [String] string representation of this object
def to_s

View file

@ -14,22 +14,22 @@ module DiasporaFederation
# @!attribute [r] photos
# Optional photos attached to the status message
# @return [[Entities::Photo]] photos
entity :photos, [Entities::Photo], default: []
entity :photos, [Entities::Photo], optional: true, default: []
# @!attribute [r] location
# Optional location attached to the status message
# @return [Entities::Location] location
entity :location, Entities::Location, default: nil
entity :location, Entities::Location, optional: true
# @!attribute [r] poll
# Optional poll attached to the status message
# @return [Entities::Poll] poll
entity :poll, Entities::Poll, default: nil
entity :poll, Entities::Poll, optional: true
# @!attribute [r] event
# Optional event attached to the status message
# @return [Entities::Event] event
entity :event, Entities::Event, default: nil
entity :event, Entities::Event, optional: true
# @!attribute [r] public
# Shows whether the status message is visible to everyone or only to some aspects

View file

@ -46,16 +46,20 @@ module DiasporaFederation
# Return array of missing required property names
# @return [Array<Symbol>] missing required property names
def missing_props(args)
class_props.keys - default_props.keys - args.keys
class_props.keys - default_props.keys - optional_props - args.keys
end
def optional_props
@optional_props ||= []
end
# Return a new hash of default values, with dynamic values
# resolved on each call
# @return [Hash] default values
def default_values
default_props.each_with_object({}) {|(name, prop), hash|
hash[name] = prop.respond_to?(:call) ? prop.call : prop
}
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
# @param [Hash] data entity data
@ -111,6 +115,7 @@ module DiasporaFederation
raise InvalidName unless name_valid?(name)
class_props[name] = type
optional_props << name if opts[:optional]
default_props[name] = opts[:default] if opts.has_key? :default
xml_names[name] = determine_xml_name(name, type, opts)

View file

@ -105,6 +105,18 @@ module DiasporaFederation
end
end
describe ".optional_props" do
it "contains an optional property" do
dsl.property :test, :string, optional: true
expect(dsl.optional_props).to include(:test)
end
it "doesn't contain normal properties" do
dsl.property :test, :string
expect(dsl.optional_props).to eq([])
end
end
describe ".default_values" do
it "can accept default values" do
dsl.property :test, :string, default: :foobar
@ -117,6 +129,19 @@ module DiasporaFederation
defaults = dsl.default_values
expect(defaults[:test]).to eq("default")
end
it "contains nil as default value for optional properties" do
dsl.property :test, :string, optional: true
defaults = dsl.default_values
expect(defaults).to include(:test)
expect(defaults[:test]).to be_nil
end
it "contains the default value for optional properties with default value" do
dsl.property :test, :string, optional: true, default: "default"
defaults = dsl.default_values
expect(defaults[:test]).to eq("default")
end
end
describe ".resolv_aliases" do