Class: SfCli::Sf::Model::ClassDefinition
- Inherits:
-
Object
- Object
- SfCli::Sf::Model::ClassDefinition
- Defined in:
- lib/sf_cli/sf/model/class_definition.rb
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #children_relation_methods ⇒ Object
- #class_methods ⇒ Object
- #define_initialize ⇒ Object
- #define_to_h ⇒ Object
-
#initialize(schema) ⇒ ClassDefinition
constructor
A new instance of ClassDefinition.
- #parent_relation_methods ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(schema) ⇒ ClassDefinition
Returns a new instance of ClassDefinition.
9 10 11 |
# File 'lib/sf_cli/sf/model/class_definition.rb', line 9 def initialize(schema) @schema = Schema.new(schema) end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
7 8 9 |
# File 'lib/sf_cli/sf/model/class_definition.rb', line 7 def schema @schema end |
Instance Method Details
#children_relation_methods ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/sf_cli/sf/model/class_definition.rb', line 96 def children_relation_methods schema.children_relations.each_with_object('') do |r, s| s << <<~EOS def #{r[:name]} @#{r[:name]} end def #{r[:name]}=(records) @#{r[:name]} = records.map{|attributes| #{r[:class_name]}.new(attributes)} end EOS end end |
#class_methods ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sf_cli/sf/model/class_definition.rb', line 64 def class_methods <<~EOS class << self def field_names @field_names ||= #{ schema.field_names } end def parent_relations @parent_relations ||= #{ schema.parent_relations } end def children_relations @children_relations ||= #{ schema.children_relations } end end EOS end |
#define_initialize ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sf_cli/sf/model/class_definition.rb', line 32 def define_initialize <<~EOS def initialize(attributes = {}) attributes.each do |k, v| field_name = k.to_sym if self.class.field_names.include?(field_name) #instance_variable_set ('@' + field_name.to_s).to_sym, v __send__ (field_name.to_s + '='), v elsif self.class.parent_relations.find{|r| r[:name] == field_name} __send__ (field_name.to_s + '='), v elsif self.class.children_relations.find{|r| r[:name] == field_name} __send__ (field_name.to_s + '='), (v.nil? ? [] : v) end end end EOS end |
#define_to_h ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sf_cli/sf/model/class_definition.rb', line 50 def define_to_h <<~EOS def to_h(keys: nil) self.class.field_names.each_with_object({}) do |name, hash| if keys&.instance_of?(Array) hash[name] = __send__(name) if keys.include?(name) else hash[name] = __send__(name) end end end EOS end |
#parent_relation_methods ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sf_cli/sf/model/class_definition.rb', line 82 def parent_relation_methods schema.parent_relations.each_with_object('') do |r, s| s << <<~EOS def #{r[:name]} @#{r[:name]} end def #{r[:name]}=(attributes) @#{r[:name]} = #{r[:class_name]}.new(attributes) end EOS end end |
#to_s ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sf_cli/sf/model/class_definition.rb', line 13 def to_s <<~Klass Class.new do #{ class_methods } field_names.each do |name| attr_accessor name end #{ parent_relation_methods } #{ children_relation_methods } #{ define_initialize } #{ define_to_h } end Klass end |