Module: Philosophal::Properties

Includes:
Types
Defined in:
lib/philosophal/properties.rb,
lib/philosophal/properties/schema.rb

Defined Under Namespace

Classes: Schema

Instance Method Summary collapse

Methods included from Types

#_Any, #_ArrayOf, #_Boolean, #_HashOf

Instance Method Details

#cprop(name, type, default: nil, transform: nil, immutable: false, description: nil, json: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/philosophal/properties.rb', line 10

def cprop(name, type, default: nil, transform: nil, immutable: false, description: nil, json: false)
  default.freeze if default && !(default.is_a?(Proc) || default.frozen?)

  if transform && !(transform.is_a?(Proc) || transform.is_a?(Symbol))
    raise Philosophal::ArgumentError, "transform param must be a Symbol object or a Proc (#{name})."
  end

  unless Philosophal::Types::BooleanType::TRUE_FALSE_SET.include?(immutable)
    raise Philosophal::ArgumentError, 'immutable param must be true or false.'
  end

  if description
    raise Philosophal::ArgumentError, 'description param must be a string.' unless description.is_a?(String)

    description.freeze unless description.frozen?
  end

  if json
    unless json.is_a?(TrueClass) || json.is_a?(String) || json.is_a?(Symbol)
      raise Philosophal::ArgumentError, 'json param must be true or a String or a Symbol.'
    end

    json = json.to_sym if json.is_a?(String)
  end

  property = __philosophal_property_class__.new(name:, type:, default:, transform:, immutable:, description:, json:)

  philosophal_properties << property
  __define_philosophal_methods__(property)
  include(__philosophal_extension__)
end

#jsonObject



66
67
68
69
70
# File 'lib/philosophal/properties.rb', line 66

def json
  return @json if defined?(@json)

  @json = Philosophal::Loaders::JsonLoader.new(self)
end

#philosophal_descriptionsObject Also known as: cprop_descriptions



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/philosophal/properties.rb', line 52

def philosophal_descriptions
  return @philosophal_descriptions if defined?(@philosophal_descriptions)

  @philosophal_descriptions = philosophal_properties.properties_index.values
                                                    .select(&:description)
                                                    .to_h do |property|
    [
      property.name,
      property.description
    ]
  end
end

#philosophal_propertiesObject



42
43
44
45
46
47
48
49
50
# File 'lib/philosophal/properties.rb', line 42

def philosophal_properties
  return @philosophal_properties if defined?(@philosophal_properties)

  @philosophal_properties = if defined?(superclass) && superclass.is_a?(Philosophal::Properties)
                              superclass.philosophal_properties.dup
                            else
                              Philosophal::Properties::Schema.new
                            end
end