Class: SmartProperties::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_properties/property.rb

Constant Summary collapse

MODULE_REFERENCE =
:"@_smart_properties_method_scope"
ALLOWED_DEFAULT_CLASSES =
[Proc, Numeric, String, Range, TrueClass, FalseClass, NilClass, Symbol, Module].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **attrs) ⇒ Property

Returns a new instance of Property.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smart_properties/property.rb', line 17

def initialize(name, **attrs)
  attrs = attrs.dup

  @name      = name.to_sym
  @default   = attrs.delete(:default)
  @converter = attrs.delete(:converts)
  @accepter  = attrs.delete(:accepts)
  @required  = attrs.delete(:required)
  @reader    = attrs.delete(:reader)
  @writable  = attrs.delete(:writable)
  @reader    ||= @name

  @instance_variable_name = :"@#{name}"

  unless ALLOWED_DEFAULT_CLASSES.any? { |cls| @default.is_a?(cls) }
    raise ConfigurationError, "Default attribute value #{@default.inspect} cannot be specified as literal, "\
      "use the syntax `default: -> { ... }` instead."
  end

  unless attrs.empty?
    raise ConfigurationError, "SmartProperties do not support the following configuration options: #{attrs.keys.map { |m| m.to_s }.sort.join(', ')}."
  end
end

Instance Attribute Details

#accepterObject (readonly)

Returns the value of attribute accepter.



8
9
10
# File 'lib/smart_properties/property.rb', line 8

def accepter
  @accepter
end

#converterObject (readonly)

Returns the value of attribute converter.



7
8
9
# File 'lib/smart_properties/property.rb', line 7

def converter
  @converter
end

#instance_variable_nameObject (readonly)

Returns the value of attribute instance_variable_name.



10
11
12
# File 'lib/smart_properties/property.rb', line 10

def instance_variable_name
  @instance_variable_name
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/smart_properties/property.rb', line 6

def name
  @name
end

#readerObject (readonly)

Returns the value of attribute reader.



9
10
11
# File 'lib/smart_properties/property.rb', line 9

def reader
  @reader
end

#writableObject (readonly)

Returns the value of attribute writable.



11
12
13
# File 'lib/smart_properties/property.rb', line 11

def writable
  @writable
end

Class Method Details

.define(scope, name, **options) ⇒ Object



13
14
15
# File 'lib/smart_properties/property.rb', line 13

def self.define(scope, name, **options)
  new(name, **options).tap { |p| p.define(scope) }
end

Instance Method Details

#accepts?(value, scope) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
# File 'lib/smart_properties/property.rb', line 78

def accepts?(value, scope)
  return true unless accepter
  return true if null_object?(value)

  if accepter.respond_to?(:to_proc)
    !!scope.instance_exec(value, &accepter)
  else
    Array(accepter).any? { |accepter| accepter === value }
  end
end

#convert(scope, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/smart_properties/property.rb', line 62

def convert(scope, value)
  return value unless converter
  return value if null_object?(value)

  case converter
  when Symbol
    converter.to_proc.call(value)
  else
    scope.instance_exec(value, &converter)
  end
end

#default(scope) ⇒ Object



74
75
76
# File 'lib/smart_properties/property.rb', line 74

def default(scope)
  @default.kind_of?(Proc) ? scope.instance_exec(&@default) : @default.dup
end

#define(klass) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/smart_properties/property.rb', line 98

def define(klass)
  property = self

  scope =
    if klass.instance_variable_defined?(MODULE_REFERENCE)
      klass.instance_variable_get(MODULE_REFERENCE)
    else
      m = Module.new
      klass.send(:include, m)
      klass.instance_variable_set(MODULE_REFERENCE, m)
      m
    end

  scope.send(:define_method, reader) do
    property.get(self)
  end

  if writable?
    scope.send(:define_method, :"#{name}=") do |value|
      property.set(self, value)
    end
  end
end

#get(scope) ⇒ Object



136
137
138
139
# File 'lib/smart_properties/property.rb', line 136

def get(scope)
  return nil unless scope.instance_variable_defined?(instance_variable_name)
  scope.instance_variable_get(instance_variable_name)
end

#missing?(scope) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/smart_properties/property.rb', line 49

def missing?(scope)
  required?(scope) && !present?(scope)
end

#optional?(scope) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/smart_properties/property.rb', line 45

def optional?(scope)
  !required?(scope)
end

#prepare(scope, value) ⇒ Object

Raises:



89
90
91
92
93
94
95
96
# File 'lib/smart_properties/property.rb', line 89

def prepare(scope, value)
  required = required?(scope)
  raise MissingValueError.new(scope, self) if required && null_object?(value)
  value = convert(scope, value)
  raise MissingValueError.new(scope, self) if required && null_object?(value)
  raise InvalidValueError.new(scope, self, value) unless accepts?(value, scope)
  value
end

#present?(scope) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/smart_properties/property.rb', line 53

def present?(scope)
  !null_object?(get(scope))
end

#required?(scope) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/smart_properties/property.rb', line 41

def required?(scope)
  @required.kind_of?(Proc) ? scope.instance_exec(&@required) : !!@required
end

#set(scope, value) ⇒ Object



122
123
124
# File 'lib/smart_properties/property.rb', line 122

def set(scope, value)
  scope.instance_variable_set(instance_variable_name, prepare(scope, value))
end

#set_default(scope) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/smart_properties/property.rb', line 126

def set_default(scope)
  return false if present?(scope)

  default_value = default(scope)
  return false if null_object?(default_value)

  set(scope, default_value)
  true
end

#to_hObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/smart_properties/property.rb', line 141

def to_h
  {
    accepter: @accepter,
    converter: @converter,
    default: @default,
    instance_variable_name: @instance_variable_name,
    name: @name,
    reader: @reader,
    required: @required
  }
end

#writable?Boolean

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/smart_properties/property.rb', line 57

def writable?
  return true if @writable.nil?
  @writable
end