Class: Nomen::PropertyNature

Inherits:
Object
  • Object
show all
Defined in:
lib/nomen/property_nature.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nomenclature, name, type, options = {}) ⇒ PropertyNature

New item


6
7
8
9
10
11
12
13
14
15
# File 'lib/nomen/property_nature.rb', line 6

def initialize(nomenclature, name, type, options = {})
  @nomenclature = nomenclature
  @name = name.to_sym
  @type = type
  raise "Invalid type: #{@type.inspect}" unless Nomen::PROPERTY_TYPES.include?(@type)
  @fallbacks = options[:fallbacks] if options[:fallbacks]
  @default = options[:default] if options[:default]
  @required = !!options[:required]
  @source = options[:choices] if reference? && options[:choices]
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default


3
4
5
# File 'lib/nomen/property_nature.rb', line 3

def default
  @default
end

#fallbacksObject (readonly)

Returns the value of attribute fallbacks


3
4
5
# File 'lib/nomen/property_nature.rb', line 3

def fallbacks
  @fallbacks
end

#nameObject (readonly)

Returns the value of attribute name


3
4
5
# File 'lib/nomen/property_nature.rb', line 3

def name
  @name
end

#nomenclatureObject (readonly)

Returns the value of attribute nomenclature


3
4
5
# File 'lib/nomen/property_nature.rb', line 3

def nomenclature
  @nomenclature
end

#sourceObject (readonly)

Returns the value of attribute source


3
4
5
# File 'lib/nomen/property_nature.rb', line 3

def source
  @source
end

#typeObject (readonly)

Returns the value of attribute type


3
4
5
# File 'lib/nomen/property_nature.rb', line 3

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object


91
92
93
# File 'lib/nomen/property_nature.rb', line 91

def <=>(other)
  name <=> other.name
end

#choicesObject

Returns list of choices for a given property


66
67
68
69
70
71
72
# File 'lib/nomen/property_nature.rb', line 66

def choices
  if inline_choices?
    return @source || []
  elsif item_reference?
    return @nomenclature.sibling(@source).all.map(&:to_sym)
  end
end

#choices_nomenclatureObject


61
62
63
# File 'lib/nomen/property_nature.rb', line 61

def choices_nomenclature
  @source
end

#human_nameObject Also known as: humanize

Return human name of property


85
86
87
# File 'lib/nomen/property_nature.rb', line 85

def human_name
  "nomenclatures.#{nomenclature.name}.property_natures.#{name}".t(default: ["nomenclatures.#{nomenclature.name}.properties.#{name}".to_sym, "properties.#{name}".to_sym, "enumerize.#{nomenclature.name}.#{name}".to_sym, "labels.#{name}".to_sym, name.humanize])
end

#inline_choices?Boolean

Returns:

  • (Boolean)

45
46
47
# File 'lib/nomen/property_nature.rb', line 45

def inline_choices?
  choice? || choice_list?
end

#item_reference?Boolean

Returns:

  • (Boolean)

49
50
51
# File 'lib/nomen/property_nature.rb', line 49

def item_reference?
  item? || item_list?
end

#list?Boolean

Returns:

  • (Boolean)

57
58
59
# File 'lib/nomen/property_nature.rb', line 57

def list?
  choice_list? || item_list? || string_list?
end

#reference?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/nomen/property_nature.rb', line 53

def reference?
  choice_list? || item_list? || string_list? || choice? || item?
end

#required?Boolean

Returns if property is required

Returns:

  • (Boolean)

41
42
43
# File 'lib/nomen/property_nature.rb', line 41

def required?
  @required
end

#selectionObject


74
75
76
77
78
79
80
81
82
# File 'lib/nomen/property_nature.rb', line 74

def selection
  if inline_choices?
    return choices.collect do |c|
      ["nomenclatures.#{@nomenclature.name}.choices.#{name}.#{c}".t, c]
    end
  elsif item_reference?
    return @nomenclature.sibling(@source).selection
  end
end

#to_xml_attrsObject


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nomen/property_nature.rb', line 23

def to_xml_attrs
  attrs = {}
  attrs[:name] = @name.to_s
  attrs[:type] = @type.to_s
  if @source
    attrs[:choices] = if inline_choices?
                        @source.join(', ')
                      else
                        @source.to_s
                      end
  end
  attrs[:required] = 'true' if @required
  attrs[:fallbacks] = @fallbacks.join(', ') if @fallbacks
  attrs[:default] = @default.to_s if @default
  attrs
end