Class: Portrayal::Schema
- Inherits:
-
Object
- Object
- Portrayal::Schema
- Defined in:
- lib/portrayal/schema.rb
Constant Summary collapse
- NULL =
Object.new.freeze
Instance Attribute Summary collapse
-
#module ⇒ Object
readonly
Returns the value of attribute module.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #add_keyword(name, default) ⇒ Object
- #attributes(obj) ⇒ Object
- #camelize(string) ⇒ Object
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
- #initialize_dup(src) ⇒ Object
- #keywords ⇒ Object
- #render_module_code ⇒ Object
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
7 |
# File 'lib/portrayal/schema.rb', line 7 def initialize; @schema = {}; @module = Module.new end |
Instance Attribute Details
#module ⇒ Object (readonly)
Returns the value of attribute module.
4 5 6 |
# File 'lib/portrayal/schema.rb', line 4 def module @module end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
4 5 6 |
# File 'lib/portrayal/schema.rb', line 4 def schema @schema end |
Instance Method Details
#add_keyword(name, default) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/portrayal/schema.rb', line 18 def add_keyword(name, default) name = name.to_sym @schema.delete(name) # Forcing keyword to be added at the end of the hash. @schema[name] = default.equal?(NULL) ? nil : Portrayal::Default.new(default) @module.module_eval(render_module_code) end |
#attributes(obj) ⇒ Object
9 |
# File 'lib/portrayal/schema.rb', line 9 def attributes(obj); Hash[keywords.map { |k| [k, obj.send(k)] }] end |
#camelize(string) ⇒ Object
10 |
# File 'lib/portrayal/schema.rb', line 10 def camelize(string); string.to_s.gsub(/(?:^|_+)([^_])/) { $1.upcase } end |
#initialize_dup(src) ⇒ Object
12 13 14 15 16 |
# File 'lib/portrayal/schema.rb', line 12 def initialize_dup(src) @schema = src.schema.transform_values(&:dup) @module = src.module.dup super end |
#keywords ⇒ Object
8 |
# File 'lib/portrayal/schema.rb', line 8 def keywords; @schema.keys end |
#render_module_code ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/portrayal/schema.rb', line 25 def render_module_code args, inits, syms, hash, eqls, dups, clones, setters, freezes = '', '', '', '', '', '', '', '', '' @schema.each do |k, default| args << "#{k}:#{default && " self.class.portrayal.schema[:#{k}]"}, " inits << "@#{k} = #{k}.is_a?(::Portrayal::Default) ? #{k}.(self) : #{k}; " syms << ":#{k}, " hash << "#{k}: @#{k}, " eqls << "@#{k} == other.instance_variable_get('@#{k}') && " dups << "@#{k} = src.instance_variable_get('@#{k}').dup; " clones << "@#{k} = src.instance_variable_get('@#{k}').clone; " setters << ":#{k}=, " freezes << "@#{k}.freeze; " end args.chomp!(', ') # key1:, key2: self.class.portrayal.schema[:key2] inits.chomp!('; ') # Assignments in initialize syms.chomp!(', ') # :key1, :key2 hash.chomp!(', ') # key1: @key1, key2: @key2 eqls.chomp!(' && ') # @key1 == other.instance_variable_get('@key1') && dups.chomp!('; ') # @key1 = src.instance_variable_get('@key1').dup; clones.chomp!('; ') # @key1 = src.instance_variable_get('@key1').clone; setters.chomp!(', ') # :key1=, :key2= freezes.chomp!('; ') # @key1.freeze; @key2.freeze <<-RUBY attr_accessor #{syms} protected #{setters} def initialize(#{args}); #{inits} end def hash; [self.class, {#{hash}}].hash end def ==(other); self.class == other.class && #{eqls} end alias eql? == def freeze; #{freezes}; super end def initialize_dup(src); #{dups}; super end def initialize_clone(src); #{clones}; super end def deconstruct public_syms = [#{syms}].select { |s| self.class.public_method_defined?(s) } public_syms.map { |s| public_send(s) } end def deconstruct_keys(keys) filtered_keys = [#{syms}].select {|s| self.class.public_method_defined?(s) } filtered_keys &= keys if Array === keys Hash[filtered_keys.map { |k| [k, public_send(k)] }] end RUBY end |