Class: Mattock::Configurable::FieldMetadata
- Inherits:
-
Object
- Object
- Mattock::Configurable::FieldMetadata
- Defined in:
- lib/mattock/configurable/field-metadata.rb
Constant Summary collapse
- DEFAULT_PROPERTIES =
{ :copiable => true, :proxiable => true, :required => false, :runtime => false, :defaulting => true, }
Instance Attribute Summary collapse
-
#default_value ⇒ Object
Returns the value of attribute default_value.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #immediate_value_on(instance) ⇒ Object
-
#initialize(name, value) ⇒ FieldMetadata
constructor
A new instance of FieldMetadata.
- #inspect ⇒ Object
- #inspect_on(instance, indent = nil) ⇒ Object
- #is(property) ⇒ Object
- #is?(property) ⇒ Boolean
- #is_not(property) ⇒ Object (also: #isnt)
- #is_not?(property) ⇒ Boolean (also: #isnt?)
- #ivar_name ⇒ Object
- #missing_on?(instance) ⇒ Boolean
- #reader_method ⇒ Object
- #runtime_missing_on?(instance) ⇒ Boolean
- #set_on?(instance) ⇒ Boolean
- #unset_on?(instance) ⇒ Boolean
- #validate_property_name(name) ⇒ Object
- #value_on(instance) ⇒ Object
- #writer_method ⇒ Object
Constructor Details
#initialize(name, value) ⇒ FieldMetadata
13 14 15 16 17 |
# File 'lib/mattock/configurable/field-metadata.rb', line 13 def initialize(name, value) @name = name @default_value = value @properties = DEFAULT_PROPERTIES.clone end |
Instance Attribute Details
#default_value ⇒ Object
Returns the value of attribute default_value.
4 5 6 |
# File 'lib/mattock/configurable/field-metadata.rb', line 4 def default_value @default_value end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/mattock/configurable/field-metadata.rb', line 4 def name @name end |
Instance Method Details
#immediate_value_on(instance) ⇒ Object
75 76 77 78 |
# File 'lib/mattock/configurable/field-metadata.rb', line 75 def immediate_value_on(instance) instance.instance_variable_get(ivar_name) #instance.__send__(reader_method) end |
#inspect ⇒ Object
19 20 21 22 23 24 |
# File 'lib/mattock/configurable/field-metadata.rb', line 19 def inspect set_props = DEFAULT_PROPERTIES.keys.find_all do |prop| @properties[prop] end "Field: #{name}: #{default_value.inspect} #{set_props.inspect}" end |
#inspect_on(instance, indent = nil) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/mattock/configurable/field-metadata.rb', line 26 def inspect_on(instance, indent=nil) set_props = DEFAULT_PROPERTIES.keys.find_all do |prop| @properties[prop] end "Field: #{name}: #{value_on(instance).inspect} \n#{indent||""}(default: #{default_value.inspect} immediate: #{immediate_value_on(instance).inspect}) #{set_props.inspect}" end |
#is(property) ⇒ Object
50 51 52 53 54 |
# File 'lib/mattock/configurable/field-metadata.rb', line 50 def is(property) validate_property_name(property) @properties[property] = true self end |
#is?(property) ⇒ Boolean
39 40 41 42 |
# File 'lib/mattock/configurable/field-metadata.rb', line 39 def is?(property) validate_property_name(property) @properties[property] end |
#is_not(property) ⇒ Object Also known as: isnt
56 57 58 59 60 |
# File 'lib/mattock/configurable/field-metadata.rb', line 56 def is_not(property) validate_property_name(property) @properties[property] = false self end |
#is_not?(property) ⇒ Boolean Also known as: isnt?
44 45 46 47 |
# File 'lib/mattock/configurable/field-metadata.rb', line 44 def is_not?(property) validate_property_name(property) !@properties[property] end |
#ivar_name ⇒ Object
63 64 65 |
# File 'lib/mattock/configurable/field-metadata.rb', line 63 def ivar_name "@#{name}" end |
#missing_on?(instance) ⇒ Boolean
104 105 106 107 108 109 110 111 |
# File 'lib/mattock/configurable/field-metadata.rb', line 104 def missing_on?(instance) return false unless is?(:required) if instance.respond_to?(:runtime?) and !instance.runtime? return runtime_missing_on?(instance) else return !set_on?(instance) end end |
#reader_method ⇒ Object
71 72 73 |
# File 'lib/mattock/configurable/field-metadata.rb', line 71 def reader_method name end |
#runtime_missing_on?(instance) ⇒ Boolean
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/mattock/configurable/field-metadata.rb', line 113 def runtime_missing_on?(instance) return false if is?(:runtime) return true unless instance.instance_variable_defined?(ivar_name) value = immediate_value_on(instance) if ProxyValue === value value.field.runtime_missing_on?(value.source) else false end end |
#set_on?(instance) ⇒ Boolean
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mattock/configurable/field-metadata.rb', line 89 def set_on?(instance) return true unless instance.__send__(reader_method).nil? return false unless instance.instance_variable_defined?(ivar_name) value = immediate_value_on(instance) if ProxyValue === value value.field.set_on?(value.source) else true end end |
#unset_on?(instance) ⇒ Boolean
100 101 102 |
# File 'lib/mattock/configurable/field-metadata.rb', line 100 def unset_on?(instance) !set_on?(instance) end |
#validate_property_name(name) ⇒ Object
33 34 35 36 37 |
# File 'lib/mattock/configurable/field-metadata.rb', line 33 def validate_property_name(name) unless DEFAULT_PROPERTIES.has_key?(name) raise "Invalid field property #{name.inspect} - valid are: #{DEFAULT_PROPERTIES.keys.inspect}" end end |
#value_on(instance) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/mattock/configurable/field-metadata.rb', line 80 def value_on(instance) value = immediate_value_on(instance) if ProxyValue === value value.field.value_on(value.source) else value end end |
#writer_method ⇒ Object
67 68 69 |
# File 'lib/mattock/configurable/field-metadata.rb', line 67 def writer_method "#{name}=" end |